remove old experts after backing Up and also implement HE Hedging ...

This commit is contained in:
2024-09-16 14:22:14 +03:30
parent 88ea5ec928
commit 2f3bf0f34a
5 changed files with 584 additions and 2055 deletions
@@ -418,6 +418,69 @@ struct X121TradeData
//
};
//
// Model a Position and it's Protected Positions ...
struct XProtectedPosition
{
//
XPosition main; // Main Position ...
X121TradeData data; // Trade Handler Data ...
//
XPosition supports[]; // Support Positions ...
/**
* Count Supported Positions
*/
int CountSupports()
{
return ArraySize(supports);
}
/**
* Extract All Positions into Specified Array
* of Positions ...
*/
int ExtractPositions(
XPosition &positions[] //
)
{
//
int result = 0;
//
Clean(positions);
//
if (main.IsValid())
{
//
AddRef(
main,
positions //
);
}
//
int supportsCount = CountSupports();
if (IsValidSize(supportsCount))
{
//
Copy(
supports,
positions,
false //
);
}
//
result = ArraySize(positions);
//
return result;
}
};
//
// Implementation ...
@@ -980,6 +1043,107 @@ class X121SCTradeHandler : public XSCBaseAlert
mMaxDrawdownPercentForOpenTrades = value;
}
//
// Hedge Props ...
//
bool AllowHedge()
{
return mAllowHedge;
}
//
void AllowHedge(bool value)
{
mAllowHedge = value;
}
//
double HedgeMinVolumeStep()
{
return mHedgeMinVolumeStep;
}
//
// Min: 0.01
// Max 0.1
void HedgeMinVolumeStep(double value)
{
//
if (value < 0.01)
{
value = 0.01;
}
//
if (value > 0.1)
{
value = 0.1;
}
//
mHedgeMinVolumeStep = value;
}
//
int MinimumOpenPositionsForHEHedge()
{
return mMinimumOpenPositionsForHEHedge;
}
//
void MinimumOpenPositionsForHEHedge(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mMinimumOpenPositionsForHEHedge = value;
}
//
double HedgeHEMinProfitPerVolumeStep()
{
return mHedgeHEMinProfitPerVolumeStep;
}
//
// Min: 0.01
void HedgeHEMinProfitPerVolumeStep(double value)
{
//
if (value < 0.01)
{
value = 0.01;
}
//
mHedgeHEMinProfitPerVolumeStep = value;
}
//
double HedgeBEMinProfitPerVolumeStep()
{
return mHedgeBEMinProfitPerVolumeStep;
}
//
// Min: 0.01
void HedgeBEMinProfitPerVolumeStep(double value)
{
//
if (value < 0.01)
{
value = 0.01;
}
//
mHedgeBEMinProfitPerVolumeStep = value;
}
//
// Read Only Props ...
@@ -1019,6 +1183,168 @@ class X121SCTradeHandler : public XSCBaseAlert
return drawdownPercent;
}
//
// Position Retrievers ...
/**
* Retrieve Positions as XProtetedPosition structure
* @param positions: Argument 1
* @return ( int )
*/
int GetPositions(
XProtectedPosition &positions[] //
)
{
//
int result = 0;
//
Clean(positions);
//
// Retrieve Positions from Trader Class ...
XPosition allPositions[];
int allPositionsCount = mTrader.GetPositions(
allPositions,
NULL, // All Symbols ...
NULL, // All Providers ...
NULL, // All Periods ...
X_POSITION_TYPE_ALL, // Long and Short ...
false, // Filter By Magic ...
true // Force Clean ...
);
if (!IsValidSize(allPositionsCount))
{
return result;
}
//
// Filter Support Positions ...
XPosition mainPositions[];
XPosition supportPositions[];
for (int i = 0; i < allPositionsCount; i++)
{
//
XPosition iPosition = allPositions[i];
//
// Check Validation ...
if (!iPosition.IsValid())
{
continue;
}
//
// Check Position Support or not ...
bool isSupport = IsSupport(iPosition.comment);
if (isSupport)
{
//
AddRef(
iPosition,
supportPositions //
);
continue;
}
//
AddRef(
iPosition,
mainPositions //
);
}
//
// Check Main Positions Count ...
int mainPositionsCount = ArraySize(mainPositions);
if (!IsValidSize(mainPositionsCount))
{
return result;
}
//
int supportPositionsCount = ArraySize(supportPositions);
//
for (int i = 0; i < mainPositionsCount; i++)
{
//
XPosition iPosition = mainPositions[i];
//
XProtectedPosition iProtected;
iProtected.main = iPosition;
Clean(iProtected.supports);
//
// Loop Through Support Positions To Find iPositions Support ...
if (IsValidSize(supportPositionsCount))
{
//
for (int j = 0; j < supportPositionsCount; j++)
{
//
XPosition jSupport = supportPositions[j];
//
ulong supportParentTicket = ExtractSupportedTicket(jSupport.comment);
bool isSupport = supportParentTicket > 0;
if (isSupport &&
supportParentTicket == iPosition.ticket)
{
//
AddRef(
jSupport,
iProtected.supports //
);
}
//
ulong eqmSupportParentTicket = ExtractEQMSupportedTicket(jSupport.comment);
bool isEQMSupport = eqmSupportParentTicket > 0;
if (isEQMSupport &&
eqmSupportParentTicket == iPosition.ticket)
{
//
AddRef(
jSupport,
iProtected.supports //
);
}
}
}
//
// Findout X121TradeData ...
int iIDX = -1;
bool hasItem = HasItem(
iPosition.ticket,
iIDX //
);
if (hasItem && IsValidIndex(iIDX))
{
iProtected.data = mData[iIDX];
}
//
AddRef(
iProtected,
positions //
);
}
//
Clean(allPositions);
Clean(mainPositions);
Clean(supportPositions);
//
result = ArraySize(positions);
//
return result;
}
//
// Trade Handling Functions ...
@@ -1275,6 +1601,9 @@ class X121SCTradeHandler : public XSCBaseAlert
);
}
//
// Position Execute and Sync Functions ...
/**
* this Method Synchronize
* all Exists Positions and parse them
@@ -1480,13 +1809,46 @@ class X121SCTradeHandler : public XSCBaseAlert
return result;
}
//
// Protection Functions ...
/**
* Handle Positiona Protections
*/
void HandleProtection()
{
//
// TODO: Implement this ...
// Retrieve Positions ...
XProtectedPosition pPositions[];
int pPositionsCount = GetPositions(
pPositions //
);
//
// Validate Count ...
if (!IsValidSize(pPositionsCount))
{
return;
}
//
bool isHedged = HandleHedge(pPositions);
if (isHedged)
{
//
// Renew Data ...
Clean(pPositions);
int pPositionsCount = GetPositions(
pPositions //
);
//
// Validate Count ...
if (!IsValidSize(pPositionsCount))
{
return;
}
}
}
//
@@ -1499,6 +1861,202 @@ class X121SCTradeHandler : public XSCBaseAlert
//
X121TradeData mData[]; // Hold Trade Data
//
// Hedge Related Functions ...
/**
* Handle Hedging On Positions ...
*/
bool HandleHedge(XProtectedPosition &positions[])
{
//
bool result = false;
//
// Check Hedging is Enabled Or Not ...
result = AllowHedge();
if (!result)
{
return result;
}
//
// Check Minimum Volume Step is Provided or not ...
double minVolumeStep = HedgeMinVolumeStep();
result = minVolumeStep > 0;
if (!result)
{
return result;
}
//
// Check Positions ...
int positionsCount = ArraySize(positions);
result = IsValidSize(positionsCount);
if (!result)
{
return result;
}
//
// Calculate Required Info ...
double swaps = 0; // Summary of Positions Swaps ...
double profits = 0; // Summary of Positions and Supports Profits ...
double volumes = 0; // Summary of Positions and Supports Volumes ...
int supportsCount = 0; // Number of Protecting Positions ...
double commissions = 0; // Summary of Positions and Supports Commissions ...
XPosition flatPositions[]; // Flat Positions ...
for (int i = 0; i < positionsCount; i++)
{
//
XProtectedPosition iProtected = positions[i];
//
XPosition iPositions[];
int iPositionsCount = iProtected.ExtractPositions(iPositions);
if (IsValidSize(iPositionsCount))
{
//
Copy(
iPositions,
flatPositions,
false //
);
}
//
// Collect Main Positions Data ...
swaps += iProtected.main.swap;
profits += iProtected.main.profit;
volumes += iProtected.main.volume;
commissions += mTrader.GetPositionCommission(iProtected.main.ticket);
//
int iSupportsCount = iProtected.CountSupports();
supportsCount += iSupportsCount;
//
if (IsValidSize(iSupportsCount))
{
//
// Collectiong Supports Positions Data ...
for (int j = 0; j < iSupportsCount; j++)
{
//
XPosition jSupport = iProtected.supports[j];
//
swaps += jSupport.swap;
profits += jSupport.profit;
volumes += jSupport.volume;
//
commissions += mTrader.GetPositionCommission(jSupport.ticket);
}
}
}
//
// First Step is Handle HE Hedge ...
//
int minOpenPositions = MinimumOpenPositionsForHEHedge();
double heMinProfitPerVolumeStep = HedgeHEMinProfitPerVolumeStep();
//
// Check HE Hedge Conditions ...
bool allowHEHedge = minOpenPositions > 0 && heMinProfitPerVolumeStep > 0;
if (allowHEHedge)
{
//
// Implement HE Hedge ...
//
// Check Main Positions Count and also not Support Positions ...
result = supportsCount == 0 &&
positionsCount >= minOpenPositions;
if (!result)
{
return result;
}
//
// Check Profits ...
result = profits > 0;
if (!result)
{
return result;
}
//
// Calculate Required Profit ...
double volumeSteps = volumes / minVolumeStep;
double minRequiredProfit = (volumeSteps * heMinProfitPerVolumeStep) +
commissions + (-1 * swaps);
//
// Check Profits Satisfied Required Profit's For Hedge or not ...
result = profits >= minRequiredProfit;
if (!result)
{
return result;
}
//
int flatPositionsCount = ArraySize(flatPositions);
result = IsValidSize(flatPositionsCount);
if (!result)
{
return result;
}
//
string comment = "EQM HE Hedge ...";
int closed = mTrader.Close(
flatPositions,
comment //
);
//
result = closed > 0;
if (result)
{
//
string message = "EQM HE Hedge Close (" + ToString(flatPositionsCount) + ") Positions by: " + ToString(profits);
Alert(message);
}
//
return result;
}
//
// Check BE Hedge Conditions ...
bool allowBEHedge = positionsCount >= 1 &&
supportsCount >= 1;
if (allowBEHedge)
{
//
// Implement BE Hedge ...
//
// Check BE Conditions ...
result = supportsCount > 0;
if (!result)
{
return result;
}
//
//
return result;
}
//
return result;
}
//
// Private ...
private:
@@ -1525,6 +2083,17 @@ class X121SCTradeHandler : public XSCBaseAlert
bool mUseMaxAllowedTradesPerSymbol; // Use Max Allowed Trades Per Symbol
double mMaxDrawdownPercentForOpenTrades; // Max Drawdown Percent for Open Trades
//
// Hedging Properties ...
bool mAllowHedge; // Allow Protector to Hedge Positions
double mHedgeMinVolumeStep; // Minimum Volume Step For Hedge
int mMinimumOpenPositionsForHEHedge; // Minimum Open Positions For HE Hedge
double mHedgeHEMinProfitPerVolumeStep; // Minimum Required Profit For HE Hedge Per Volume Step
double mHedgeBEMinProfitPerVolumeStep; // Minimum Required Profit For BE Hedge Per Volume Step
//
// Common Functions ...
//
// Count Data ...
int Count()