complete grid mechanism ...

This commit is contained in:
2024-06-14 18:33:38 +03:30
parent a78cf432ac
commit 001c5f687e
3 changed files with 279 additions and 29 deletions
+254 -12
View File
@@ -817,12 +817,63 @@ public:
mUseSupport = value;
}
//
bool UseGrid()
{
return mUseGrid;
}
//
void UseGrid(bool value)
{
mUseGrid = value;
}
//
double GridDistance()
{
return mGridDistance;
}
//
void GridDistance(double value)
{
//
if (value < 0)
{
value = 0;
}
//
mGridDistance = value;
}
//
double GridVolumeMultiplier()
{
return mGridVolumeMultiplier;
}
//
void GridVolumeMultiplier(double value)
{
//
if (value < 0)
{
value = 0;
}
//
mGridVolumeMultiplier = value;
}
//
// Overrides ...
//
// Customize Token ...
string GetToken() override
string
GetToken() override
{
return GetSpecificToken(this);
}
@@ -1003,7 +1054,7 @@ public:
//
// Parse Conditions for Signal ...
bool cHasSignal = signalGenerator.HasSignal(
cHasSignal = signalGenerator.HasSignal(
cConditions,
cSignal,
cPusher,
@@ -1066,7 +1117,7 @@ public:
//
// Parse Conditions for Signal ...
bool sHasSignal = signalGenerator.HasSignal(
sHasSignal = signalGenerator.HasSignal(
sConditions,
sSignal,
sPusher,
@@ -1128,7 +1179,7 @@ public:
//
// Parse Conditions for Signal ...
bool mHasSignal = signalGenerator.HasSignal(
mHasSignal = signalGenerator.HasSignal(
mConditions,
mSignal,
mPusher,
@@ -1191,7 +1242,7 @@ public:
//
// Parse Conditions for Signal ...
bool lHasSignal = signalGenerator.HasSignal(
lHasSignal = signalGenerator.HasSignal(
lConditions,
lSignal,
lPusher,
@@ -1254,7 +1305,7 @@ public:
//
// Parse Conditions for Signal ...
bool hHasSignal = signalGenerator.HasSignal(
hHasSignal = signalGenerator.HasSignal(
hConditions,
hSignal,
hPusher,
@@ -1750,6 +1801,13 @@ public:
}
}
}
//
bool hasSignal = cHasSignal || sHasSignal || mHasSignal || lHasSignal || hHasSignal;
if (hasSignal)
{
Print("Signal Recieved ...");
}
}
//
@@ -1891,6 +1949,10 @@ public:
// Handle Close All Positions on Specific Time ...
DoEQMCloseOnSpecificTime();
//
// Do EQM Grid if Provided ...
DoEQMGrid();
//
// if Returns true, Signal Execution failed ...
return result;
@@ -1986,7 +2048,6 @@ private:
double mVolume; // Volume
double mTPPoint; // TP Point
double mSLPoint; // SL Point
bool mUseSupport; // Use Support Signals
bool mIgnoreTP; // Ignore Calculated TP
bool mIgnoreSL; // Ignore Calculated SL
int mMaxAllowedTrades; // Max Alloed Positions
@@ -1995,6 +2056,12 @@ private:
int mReuiredSignalVerifications; // Required Verifications for Validate Signals
bool mGetVerificationFromOtherTimeFrames; // Get Verifications from Other Time Frames
//
bool mUseSupport; // Use Support Signals
bool mUseGrid; // Use Grid Signals
double mGridDistance; // Grid Distance
double mGridVolumeMultiplier; // Grid Volume Multiplier
//
XSignalInfo mSignalInfos[];
XSignalInfoCollector *mSignalCollector;
@@ -2098,11 +2165,20 @@ private:
//
if (IsValid(deal.provider))
{
result = FindSignalInfoIndex(
deal.symbol,
deal.provider,
deal.period //
);
//
ulong supTicket = ExtractSupportedTicket(deal.comment);
ulong supTicket1 = ExtractEQMSupportedTicket(deal.comment);
//
if (supTicket <= 0 && supTicket1 <= 0)
{
//
result = FindSignalInfoIndex(
deal.symbol,
deal.provider,
deal.period //
);
}
}
else
{
@@ -2167,6 +2243,30 @@ private:
mSignalInfos[infoIDX].ticket = deal.positionId;
mSignalInfos[infoIDX].commission = deal.commission;
mSignalInfos[infoIDX].signal.positionId = deal.positionId;
//
bool isLong = IsLong(mSignalInfos[infoIDX].signal.type);
double sEntry = mSignalInfos[infoIDX].signal.entry;
//
// Setting Grid Data ...
if (mUseGrid)
{
//
mSignalInfos[infoIDX].ResetGrid();
//
double gDistancePrice = PointToPrice(
mGridDistance,
mSignalInfos[infoIDX].signal.symbol //
);
//
mSignalInfos[infoIDX].gVolume = mSignalInfos[infoIDX].volume * mGridVolumeMultiplier;
mSignalInfos[infoIDX].gEntry = isLong
? sEntry - gDistancePrice
: sEntry + gDistancePrice;
}
}
else if (deal.entry == DEAL_ENTRY_OUT)
{
@@ -2527,6 +2627,148 @@ private:
}
}
//
// Do Gridding Mechanism ...
void DoEQMGrid()
{
//
if (!mUseGrid)
{
return;
}
//
int infosCount = CountSignalInfos();
if (!IsValidSize(infosCount))
{
return;
}
//
for (int i = 0; i < infosCount; i++)
{
//
// Check Signal Executed or not ...
if (mSignalInfos[i].ticket <= 0)
{
continue;
}
//
// Check Specific Position not a Support Position ...
XPosition position;
bool hasPosition = mTrader.GetPosition(
mSignalInfos[i].ticket,
position //
);
if (!hasPosition)
{
continue;
}
//
// Check Position not a Support or EQM Support ...
ulong supParent = 0;
supParent = ExtractSupportedTicket(position.comment);
if (supParent > 0)
{
continue;
}
supParent = ExtractEQMSupportedTicket(position.comment);
if (supParent > 0)
{
continue;
}
//
// Check Grid Data ...
if (mSignalInfos[i].gEntry <= 0 || mSignalInfos[i].gVolume <= 0)
{
continue;
}
//
// Retrieve Type of Position ...
bool isLong = IsLong(mSignalInfos[i].signal.type);
//
// Retrieve Cureent Entry Price based on Signal ...
double cEntry = GetEntry(
mSignalInfos[i].symbol,
mSignalInfos[i].signal.type //
);
//
// Check Price Distance passed or not ...
bool isPriceDistancePassedForGrid =
isLong
? cEntry <= mSignalInfos[i].gEntry
: cEntry >= mSignalInfos[i].gEntry;
if (!isPriceDistancePassedForGrid)
{
continue;
}
//
// Prepare Grid Signal ...
XSignal gSignal;
bool isPrepared = gSignal.Prepare(
mSignalInfos[i].signal.symbol,
mSignalInfos[i].signal.provider,
mSignalInfos[i].signal.period,
mSignalInfos[i].signal.type,
X_ORDER_MODE_MARKET,
mSignalInfos[i].gEntry,
mSignalInfos[i].gVolume,
0, // SL ...
0 // TP ...
);
if (!isPrepared)
{
continue;
}
//
// Add Support Tag ...
string comment = GenerateSupportTag(mSignalInfos[i].ticket);
gSignal.comment = comment;
//
// Try to Execute Grid Signal ...
ENUM_X_SIGNAL_EXECUTION_RESULT gState;
int isExecuted = mTrader.ExecuteSignal(
gSignal,
gState,
ORDER_TIME_GTC,
NULL,
false // Ignore Policies ...
);
if (isExecuted)
{
//
// Update Grid State of Signal Info ...
//
double gDistancePrice = PointToPrice(
mGridDistance,
mSignalInfos[i].signal.symbol //
);
//
mSignalInfos[i].gVolume *= mGridVolumeMultiplier;
mSignalInfos[i].gEntry = isLong
? mSignalInfos[i].gEntry - gDistancePrice
: mSignalInfos[i].gEntry + gDistancePrice;
//
string message = XEQMSupportToken + " Execute Grid ...";
//
Alert(message);
}
}
}
//
// Handle Close Trades on Specific Time ...
void DoEQMCloseOnSpecificTime()