This commit is contained in:
2024-05-25 22:20:20 +03:30
parent b8515c9d05
commit 1f661d4de3
6 changed files with 1072 additions and 512 deletions
+268 -143
View File
@@ -283,7 +283,7 @@ public:
//
// Handl Management Actions ...
// if returns true, it is going to prevent for processing forward ...
bool HandleStateManagement() override
bool HandleStateManagement(XSignal &signals[]) override
{
//
bool result = false;
@@ -297,7 +297,7 @@ public:
}
//
HandleEQMState();
HandleEQMState(signals);
//
HandleHedging(positions);
@@ -344,9 +344,7 @@ public:
string comment = "EQM Hedge ...";
int closed = mTrader.Close(
positions,
comment
//
);
comment);
if (closed > 0)
{
//
@@ -358,14 +356,14 @@ public:
//
for (int i = 0; i < positionsCount; i++)
{
RemoveSLTrail(positions[i].ticket);
RemoveTrail(positions[i].ticket);
}
}
}
}
//
void HandleEQMState()
void HandleEQMState(XSignal &signals[])
{
//
bool equityReached = !mTrader.CheckEquityForTrade();
@@ -378,8 +376,75 @@ public:
}
//
Print("EQM Enabled: " + (equityReached ? "Max Equity Reached ..." : maxAllowedReached ? "Max Allowed Positions Reached ..."
: ""));
string eqmMessage = "EQM Enabled: " + "\n";
if (equityReached)
{
eqmMessage += "- Max Equity Reached ..." + "\n";
}
if (maxAllowedReached)
{
eqmMessage += "- Max Allowed Positions Reached ...";
}
mAlert.Alert(eqmMessage);
//
int descriptorsCount = CountDescriptors();
if (descriptorsCount <= 0)
{
return;
}
//
XSignal supportSignals[];
for (int i = 0; i < descriptorsCount; i++)
{
//
X121ProviderDescriptor iDescriptor = mDescriptors[i];
//
XSignal iSupportSignals[];
int iSupportSignalsCount = iDescriptor
.provider
.GenerateEQMSupportSignals(
iSupportSignals //
);
if (iSupportSignalsCount <= 0)
{
continue;
}
//
Copy(
iSupportSignals,
supportSignals,
false //
);
}
//
int supportSignalsCount = ArraySize(supportSignals);
if (supportSignalsCount <= 0)
{
return;
}
//
ENUM_X_SIGNAL_EXECUTION_RESULT states[];
int executedSupports = mTrader.ExecuteSignals(
supportSignals,
states,
ORDER_TIME_GTC,
NULL,
false // Ignore Policies ...
);
if (executedSupports > 0)
{
//
string message = "EQM Execute (" + ToString(executedSupports) + ") Support Signals ...";
//
mAlert.Alert(message);
}
}
//
@@ -389,8 +454,8 @@ public:
// TODO:
// Make them Configurable ...
int levelsOnClose = 6;
int requiredAgeForSLTrail = 3;
int requiredDistanceForSLTrail = 10;
int requiredAgeForSLTrail = 0;
int requiredDistanceForSLTrail = 30;
//
int positionsCount = ArraySize(positions);
@@ -409,10 +474,7 @@ public:
//
bool canManageState =
//
iPosition.provider == ToString(X786) ||
iPosition.provider == ToString(X121) ||
iPosition.provider == ToString(X110) ||
iPosition.provider == ToString(X92)
true
//
;
@@ -475,7 +537,8 @@ public:
iPosition.type,
iPosition.profit,
iPosition.sl,
sl);
sl //
);
if (!isInit)
{
continue;
@@ -510,43 +573,190 @@ public:
//
double priceDistanceInPoint = PriceToPoint((iPosition.profit - trail.profit) / iPosition.volume, iPosition.symbol);
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
if (priceDistance < requiredDistanceForSLTrail)
if (priceDistance < (requiredDistanceForSLTrail / 2))
{
//
// int positionAge = iPosition.GetAge(iPosition.period);
if (trail.level >= levelsOnClose)
{
//
string comment = "EQM Close In Profit ...";
bool isClosed = mTrader.Close(
iPosition.ticket,
comment //
);
if (isClosed)
{
//
RemoveSLTrail(iPosition.ticket);
// //
// // int positionAge = iPosition.GetAge(iPosition.period);
// if (trail.level >= levelsOnClose)
// {
// //
// string comment = "EQM Close In Profit ...";
// bool isClosed = mTrader.ClosePositionPack(
// iPosition.ticket,
// iPosition.symbol,
// iPosition.period,
// comment,
// true,
// GetMinProfitPerTrade(),
// GetMinProfitPerVolumeFactor() //
// );
// if (isClosed)
// {
// //
// RemoveTrail(iPosition.ticket);
//
string message = "EQM Close (" + ToString(iPosition.ticket) + ") In Profit ...";
mAlert.Alert(message);
}
}
// //
// string message = "EQM Close (" + ToString(iPosition.ticket) + ") In Profit ...";
// mAlert.Alert(message);
// }
// }
continue;
}
//
bool isLong = IsLong(iPosition.type);
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double selectedValue =
//
trail.level == 2
trail.level == 1 || (isLong
? iPosition.sl < iPosition.entry
: iPosition.sl > iPosition.entry)
? iPosition.entry
: iPosition.sl
//
;
//
double sl =
isLong
? selectedValue + slDiff
: selectedValue - slDiff;
//
// Update Trail Values ...
bool isUpdated = trail.Update(
sl,
iPosition.profit //
);
if (!isUpdated)
{
continue;
}
}
//
if (trail.IsValid())
{
//
string comment = trail.GenerateTag();
bool isModified = mTrader.Modify(
trail.ticket,
trail.after,
iPosition.tp,
comment //
);
if (isModified)
{
//
RemoveTrail(trail.ticket);
//
AddSLTrail(trail);
//
string message = "Position (" + ToString(trail.ticket) + ") Trailed SL on Level: " + ToString(trail.level);
mAlert.Alert(message);
}
}
}
else if (iPosition.profit < 0)
{
//
XTrail trail;
bool isTrailedBefore = GetSLTrail(
iPosition.ticket,
trail //
);
//
if (!isTrailedBefore)
{
//
// Add First Time ...
//
// Check Position Age ...
int iPositionAge = iPosition.GetAge(iPosition.period);
if (iPositionAge < requiredAgeForSLTrail)
{
continue;
}
//
// Check Position Profit Distance ...
double priceDistanceInPoint = PriceToPoint(MathAbs(iPosition.profit) / iPosition.volume, iPosition.symbol);
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
if (priceDistance < requiredDistanceForSLTrail)
{
continue;
}
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double sl =
IsLong(iPosition.type)
? iPosition.sl + slDiff
: iPosition.sl - slDiff;
//
bool isInit = trail.Init(
iPosition.ticket,
iPosition.type,
iPosition.profit,
iPosition.sl,
sl //
);
if (!isInit)
{
continue;
}
}
else
{
//
// Update Exists ...
//
// Check Profit ...
bool isProfitPassed = iPosition.profit > trail.profit;
if (!isProfitPassed)
{
continue;
}
//
// Check Age Distance from Last ...
int age = iBarShift(
iPosition.symbol,
iPosition.period,
trail.time,
false //
);
if (age < requiredAgeForSLTrail)
{
continue;
}
//
double priceDistanceInPoint = PriceToPoint((MathAbs(iPosition.profit) - MathAbs(trail.profit)) / iPosition.volume, iPosition.symbol);
double priceDistance = PointToPrice(priceDistanceInPoint, iPosition.symbol);
//
double slDiff = PointToPrice(priceDistance, iPosition.symbol);
//
double selectedValue =
//
iPosition.sl
//
;
//
double sl =
IsLong(iPosition.type)
@@ -579,7 +789,7 @@ public:
if (isModified)
{
//
RemoveSLTrail(trail.ticket);
RemoveTrail(trail.ticket);
//
AddSLTrail(trail);
@@ -622,6 +832,8 @@ public:
// Cleaning Source ...
Clean(signals);
//
//
// Loop through Signals for Management ...
for (int i = 0; i < tmpsCount; i++)
@@ -652,10 +864,15 @@ public:
NULL, // All Periods ...
iSignal.type //
);
if (positionsCount < 2)
if (positionsCount <= 0)
{
//
// First Signal in a While of Specific Provider ...
//
iSignal.volume *= 2;
//
AddRef(
iSignal,
signals //
@@ -664,112 +881,20 @@ public:
//
continue;
}
// else
// {
// //
// // not First Signal ...
else if (positionsCount < 2)
{
//
// First Signal in a While of Specific Provider ...
// //
// // Calculate Provider Specified Open Positions Profit ...
// double profits = CalculatePositionsProfit(positions);
// if (profits > 0)
// {
//
AddRef(
iSignal,
signals //
);
// //
// AddRef(
// iSignal,
// signals //
// );
// // //
// // // Find Max In Profit of Same Position ...
// // Clean(positions);
// // positionsCount = mTrader.GetInProfitPositions(
// // positions,
// // iSignal.symbol,
// // iSignal.provider,
// // NULL, // All Periods ...
// // iSignal.type,
// // X_POSITION_SELECT_MAX // Max In Profit ...
// // );
// // if (positionsCount <= 0)
// // {
// // continue;
// // }
// // //
// // string comment = "EQM Close Max Profit ...";
// // bool isClosed = mTrader.Close(
// // positions[0].ticket,
// // comment //
// // );
// // if (isClosed)
// // {
// // //
// // RemoveTrail(positions[0].ticket);
// // //
// // string message = "EQM Close Max In Profit (" + ToString(positions[0].ticket) + ") for new Signal ...";
// // //
// // mAlert.Alert(message);
// // //
// // AddRef(
// // iSignal,
// // signals //
// // );
// // //
// // continue;
// // }
// }
// else
// {
// // //
// // // Find Max In Profit of Same Position ...
// // Clean(positions);
// // positionsCount = mTrader.GetInProfitPositions(
// // positions,
// // iSignal.symbol,
// // iSignal.provider,
// // NULL, // All Periods ...
// // iSignal.type,
// // X_POSITION_SELECT_MAX // Max In Profit ...
// // );
// // if (positionsCount <= 0)
// // {
// // continue;
// // }
// // //
// // string comment = "EQM Close Max Profit ...";
// // bool isClosed = mTrader.Close(
// // positions[0].ticket,
// // comment //
// // );
// // if (isClosed)
// // {
// // //
// // RemoveTrail(positions[0].ticket);
// // //
// // string message = "EQM Close Max In Profit (" + ToString(positions[0].ticket) + ") for new Signal ...";
// // //
// // mAlert.Alert(message);
// // //
// // AddRef(
// // iSignal,
// // signals //
// // );
// // //
// // continue;
// // }
// }
// }
//
continue;
}
}
//