last works ...

This commit is contained in:
2024-06-09 04:20:08 +03:30
parent 52e4d1316d
commit a868160ba2
4 changed files with 487 additions and 65 deletions
+211 -56
View File
@@ -401,10 +401,14 @@ struct XProtectedPosition
//
// Volume ...
// For Volume Calculating we have to use Recovery Level ...
// double mVolume =
// lastVolume == 0
// ? volume * 1
// : lastVolume * 1;
double mVolume =
lastVolume == 0
? volume * 3
: lastVolume * 3;
canSameDirectionRecover
? volume
: volume * 2;
//
result = signal.Prepare(
@@ -538,10 +542,9 @@ public:
//
// Initial Class ...
bool
Init(
bool Init(
XSCTrade *trader,
double mMinRequiredProfitPerTrade = 0.35,
double mMinRequiredProfitPerTrade = 0.5,
double mMinRequiredProfitPerTradeVolumeFactor = 0.01 //
)
{
@@ -645,63 +648,80 @@ public:
{
//
int protectedsCount = ArraySize(protecteds);
if (!IsValidSize(protectedsCount))
{
return;
}
//
// Loop through Exists ...
for (int i = 0; i < protectedsCount; i++)
if (IsValidSize(protectedsCount))
{
//
// Do Protection ...
HandlePositionsProtecting(protecteds[i]);
}
//
// Handle Hedging All Positions ...
bool allowHedge = AllowHedge();
if (!allowHedge)
{
return;
}
//
XPosition positions[];
int positionsCount = mTrader.GetPositions(positions);
if (!IsValidSize(positions) || positionsCount <= 1)
{
return;
}
//
double profit = SpecifiedCalculatePositionsProfit(positions);
double requiredProfit = SpecifiedCalculateRequiredProfitForHedge(
positions,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
bool canHedge = SpecifiedIsPositionsReadyForHedge(
positions,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
if (!canHedge)
{
return;
// Loop through Exists ...
for (int i = 0; i < protectedsCount; i++)
{
//
// Do Protection ...
HandlePositionsProtecting(protecteds[i]);
}
}
//
string comment = XEQMSupportToken + " Hege ...";
ENUM_X_SIGNAL_EXECUTION_RESULT states[];
int closed = mTrader.Close(
positions,
comment //
);
if (IsValidSize(closed))
//
// Handle Hedging All Positions ...
bool allowHedge = AllowHedge();
if (allowHedge)
{
Clean(protecteds);
//
XPosition positions[];
int positionsCount = mTrader.GetPositions(positions);
if (!IsValidSize(positions) || positionsCount <= 1)
{
//
// Here We Can Close if Still Positions is EQM Support ...
bool canClosePosition =
//
positionsCount == 1 &&
positions[0].provider == XEQMSupportToken &&
positions[0].profit > (-1 * positions[0].swap) + (minRequiredProfitPerTrade * (positions[0].volume / minRequiredProfitPerTradeVolumeFactor))
//
;
if (canClosePosition)
{
//
bool isClosed = mTrader.Close(
positions[0].ticket,
comment //
);
}
return;
}
//
double profit = SpecifiedCalculatePositionsProfit(positions);
double requiredProfit = SpecifiedCalculateRequiredProfitForHedge(
positions,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
bool canHedge = SpecifiedIsPositionsReadyForHedge(
positions,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
if (!canHedge)
{
//
ProtectMargin();
return;
}
//
ENUM_X_SIGNAL_EXECUTION_RESULT states[];
int closed = mTrader.Close(
positions,
comment //
);
if (IsValidSize(closed))
{
Clean(protecteds);
}
}
}
@@ -916,6 +936,11 @@ protected:
string comment = GenerateEQMSupportTag(item.ticket);
signal.comment = comment;
//
// Remove Support Signal TP and SL ...
signal.tp = 0;
signal.sl = 0;
//
ENUM_X_SIGNAL_EXECUTION_RESULT state;
bool isExecuted = mTrader.ExecuteSignal(
@@ -968,6 +993,136 @@ protected:
return result;
}
//
// Protecting Margin by Free Coveraged Positions ...
void ProtectMargin()
{
//
double freeMargin = mTrader.mAccount.GetFreeMargin();
//
double balance = mTrader.mAccount.GetBalance();
double equity = mTrader.mAccount.GetEquity();
//
double selectedBalance = MathMin(balance, equity);
//
// Retrieve All Positions ...
XPosition positions[];
int positionsCount = mTrader.GetPositions(positions);
if (!IsValidSize(positionsCount))
{
return;
}
//
bool canForceHedging =
positionsCount >= 25 ||
freeMargin <= selectedBalance / 2;
if (!canForceHedging)
{
return;
}
//
// First Check Hedging By Half of Required Profit ...
double profit = SpecifiedCalculatePositionsProfit(positions);
double requiredProfit = SpecifiedCalculateRequiredProfitForHedge(
positions,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
bool canHedge =
//
profit > requiredProfit / 2
//
;
if (canHedge)
{
//
string comment = XEQMSupportToken + " Hege ...";
int closed = mTrader.Close(
positions,
comment //
);
//
return;
}
//
// TODO: Enable this if we want ...
return;
//
// Retrieve In Drawdown Positions ...
XPosition inDPositions[];
int inDPositionsCount = ExtractInDrawdownPositions(
positions,
inDPositions ///
);
if (!IsValidSize(inDPositionsCount))
{
return;
}
//
// Retrieve In Profit Positions ...
XPosition inPPositions[];
int inPPositionsCount = ExtractInProfitPositions(
positions,
inPPositions //
);
if (!IsValidSize(inPPositionsCount))
{
return;
}
//
// if we can't Hedge all Positions ...
// now we are looking to pair Positions for hedge ...
// - Select Max In Drawdown Position;
// - Looking for Coverage it inside In Profit Positions;
// - Clease All of them ...
int maxInDIDX = FindMaxDrawdownIndex(inDPositions);
if (maxInDIDX < 0)
{
return;
}
//
string comment = XEQMSupportToken + " Force Hege ...";
//
XPosition maxInDCoverages[];
int maxInDCoveragesCount = FindCoverageDrawdownPosition(
inDPositions[maxInDIDX],
inPPositions,
maxInDCoverages,
minRequiredProfitPerTrade,
minRequiredProfitPerTradeVolumeFactor //
);
if (!IsValidSize(maxInDCoveragesCount))
{
return;
}
//
bool isClosedMaxInD = mTrader.Close(
inDPositions[maxInDIDX].ticket,
comment //
);
if (isClosedMaxInD)
{
//
int closed = mTrader.Close(
maxInDCoverages,
comment //
);
}
}
//
// Protected Collection Management ...
+2 -2
View File
@@ -523,6 +523,7 @@ class XSCTrade : public XSCBase
public:
//
// Props ...
XSCAccount *mAccount; // Account Info Provider
//
// Constructors ...
@@ -3182,7 +3183,7 @@ public:
//
// Tools ...
//
// Retrieve Last Open Position Ticket ...
ulong GetLastOpenPositionTicket()
@@ -3501,7 +3502,6 @@ private:
double mMaxAllowedDrawdownFactor; // Max Allowed Drawdown Factor
//
XSCAccount *mAccount; // Account Info Provider
XSCTradeBase *mTrader; // Base CTrade Manipulated Instance ...
//