remove old experts after backing Up and also implement HE Hedging ...
This commit is contained in:
@@ -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 ...
|
// Implementation ...
|
||||||
|
|
||||||
@@ -980,6 +1043,107 @@ class X121SCTradeHandler : public XSCBaseAlert
|
|||||||
mMaxDrawdownPercentForOpenTrades = value;
|
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 ...
|
// Read Only Props ...
|
||||||
|
|
||||||
@@ -1019,6 +1183,168 @@ class X121SCTradeHandler : public XSCBaseAlert
|
|||||||
return drawdownPercent;
|
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 ...
|
// Trade Handling Functions ...
|
||||||
|
|
||||||
@@ -1275,6 +1601,9 @@ class X121SCTradeHandler : public XSCBaseAlert
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Position Execute and Sync Functions ...
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this Method Synchronize
|
* this Method Synchronize
|
||||||
* all Exists Positions and parse them
|
* all Exists Positions and parse them
|
||||||
@@ -1480,13 +1809,46 @@ class X121SCTradeHandler : public XSCBaseAlert
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Protection Functions ...
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle Positiona Protections
|
* Handle Positiona Protections
|
||||||
*/
|
*/
|
||||||
void HandleProtection()
|
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
|
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 ...
|
||||||
private:
|
private:
|
||||||
@@ -1525,6 +2083,17 @@ class X121SCTradeHandler : public XSCBaseAlert
|
|||||||
bool mUseMaxAllowedTradesPerSymbol; // Use Max Allowed Trades Per Symbol
|
bool mUseMaxAllowedTradesPerSymbol; // Use Max Allowed Trades Per Symbol
|
||||||
double mMaxDrawdownPercentForOpenTrades; // Max Drawdown Percent for Open Trades
|
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 ...
|
// Count Data ...
|
||||||
int Count()
|
int Count()
|
||||||
|
|||||||
@@ -438,6 +438,20 @@ bool InitialEA()
|
|||||||
eaTradeHandler.UseMaxAllowedTradesPerSymbol(eaUseMaxAllowedTradesPerSymbol);
|
eaTradeHandler.UseMaxAllowedTradesPerSymbol(eaUseMaxAllowedTradesPerSymbol);
|
||||||
eaTradeHandler.MaxDrawdownPercentForOpenTrades(eaMaxDrawdownPercentForOpenTrades);
|
eaTradeHandler.MaxDrawdownPercentForOpenTrades(eaMaxDrawdownPercentForOpenTrades);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure Position Protector ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure Hedging ...
|
||||||
|
// TODO: Configure it using Inputs ...
|
||||||
|
eaTradeHandler.AllowHedge(true);
|
||||||
|
eaTradeHandler.HedgeMinVolumeStep(0.01);
|
||||||
|
eaTradeHandler.MinimumOpenPositionsForHEHedge(2);
|
||||||
|
eaTradeHandler.HedgeHEMinProfitPerVolumeStep(0.2);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure Strategies ...
|
||||||
|
|
||||||
//
|
//
|
||||||
// Single Symbol ...
|
// Single Symbol ...
|
||||||
if (!eaMultiSymbol)
|
if (!eaMultiSymbol)
|
||||||
|
|||||||
@@ -1,966 +0,0 @@
|
|||||||
///////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// SaherElm IT Center XTest MQL5 Expert Advisor
|
|
||||||
// -------------------------------------------------
|
|
||||||
// Name: XCHCHEEA
|
|
||||||
// Description: an Exper Advisor which used XCHSetup
|
|
||||||
// to Analyse Market ...
|
|
||||||
//
|
|
||||||
// Maintainer:
|
|
||||||
// ------------
|
|
||||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
||||||
//
|
|
||||||
//////////////////////////////////////////////////////x
|
|
||||||
//
|
|
||||||
// Global Properties ...
|
|
||||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
||||||
#property link "https://saherelm.ir"
|
|
||||||
#property version "1.00"
|
|
||||||
#property description "SaherElm XCHCHEEA"
|
|
||||||
#property strict
|
|
||||||
|
|
||||||
//
|
|
||||||
#define ShortName "XCHCHEEA"
|
|
||||||
|
|
||||||
//
|
|
||||||
// Imports ...
|
|
||||||
#include "../Helpers/x-saherelm.xct.helper.mq5"
|
|
||||||
#include "../Helpers/x-saherelm.xcc.helper.mq5"
|
|
||||||
|
|
||||||
//
|
|
||||||
#include "../Parsers/x-saherelm.xch.che.parser.mq5"
|
|
||||||
#include "../Strategies/x-saherelm.xch.che.strategy.mq5"
|
|
||||||
|
|
||||||
//
|
|
||||||
// Inputs ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Common ...
|
|
||||||
input group "Common";
|
|
||||||
input long eaMagicNumber = 78692110; // Magic Number
|
|
||||||
input int eaSlippage = 10; // Slippgae
|
|
||||||
input string eaLogSuffix = ""; // Log Suffix
|
|
||||||
|
|
||||||
//
|
|
||||||
// Symbols ...
|
|
||||||
input group "Symbols";
|
|
||||||
input bool eaMultiSymbol = false; // Use Multi Symbol
|
|
||||||
input bool eaUseMaxAllowedTradesPerSymbol = true; // Use Max Allowed Trades Per Symbol
|
|
||||||
input string eaSymbols = "EURUSDb,GBPUSDb,EURGBPb,USDJPYb,EURJPYb,USDCHFb,EURCHFb,AUDUSDb,EURAUDb,NZDUSDb,EURNZDb,USDCADb,EURCADb,XAUUSDb,XAGUSDb,BRENT,DowJones30"; // Trading Symbols
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management ...
|
|
||||||
input group "Risk Management";
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Risk / Reward Calculation ...
|
|
||||||
input group "Risk/Reward";
|
|
||||||
input double eaR2R = 1.5; // Risk to Reward Ratio for Dynamic TP SL
|
|
||||||
input bool eaUseFixedTPSLPoints = true; // Use Fixed TP SL in Points
|
|
||||||
input bool eaIgnoreTP = false; // Ignore TP
|
|
||||||
input bool eaIgnoreSL = false; // Ignore SL
|
|
||||||
input double eaTPPoint = 200; // Static TP Point
|
|
||||||
input double eaSLPoint = 200; // Static SL Point
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Volume Calculation ...
|
|
||||||
input group "Volume";
|
|
||||||
input bool eaUseDynamicVolume = false; // Use Dynamic Volume
|
|
||||||
input double eaDynamicVolumeStep = 0.01; // Increase Volume Step
|
|
||||||
input double eaDynamicVolumeBalanceFactor = 200; // Balance Factor for Generate Dynamic Volume
|
|
||||||
input double eaVolume = 0.02; // Static Volume
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trade Management ...
|
|
||||||
input group "Trade Management";
|
|
||||||
input bool eaAllowLong = true; // Allow Long Positions
|
|
||||||
input bool eaAllowShort = true; // Allow Short Positions
|
|
||||||
input int eaMaxAllowedLongs = 2; // Max Allowed Long Positions
|
|
||||||
input int eaMaxAllowedShorts = 2; // Max Allowed Short Positions
|
|
||||||
input double eaMaxDrawdownPercentForOpenTrades = 3; // Max Drawdown Percent for Open Trades
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Hedging ...
|
|
||||||
input group "Hedging";
|
|
||||||
input bool eaAllowHedging = false; // Allow Hedging Trades
|
|
||||||
input int eaMinTradesForHedge = 2; // Minimum Open Trades for Hedging
|
|
||||||
input double eaHedgeVolumeFactor = 0.01; // Volume Factor for Hedging
|
|
||||||
input double eaMinimumProfitPerVolumeForHedging = 0.5; // Minimum Required Profit per Volume for Hedging
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trailling Stop ...
|
|
||||||
input group "Trailling Stop";
|
|
||||||
input bool eaAllowTrailStop = false; // Allow Trail Stop
|
|
||||||
input double eaStartTrailInPoint = 50; // Trail Start After Profit (Point)
|
|
||||||
input double eaTrailStepInPoint = 25; // Trail Size in each Step (Point)
|
|
||||||
input bool eaIgnoreTPAfterTrail = true; // Ignore TP after Trail
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trades Recovery ...
|
|
||||||
input group "Trades Recovery";
|
|
||||||
input bool eaAllowRecovery = false; // Allow Recover Trades
|
|
||||||
input double eaRecoveryTPPoint = 150; // Recovery TP (Point)
|
|
||||||
input double eaRecoverySLPoint = 200; // Recovery SL (Point)
|
|
||||||
input int eaMaxAllowedRecoveryTrades = 0; // Max Allowed Recovery Trades
|
|
||||||
input double eaRecoveryVolumeMultiplier = 2; // Recovery Volume Multiplier
|
|
||||||
input double eaMaxAllowedRecoveryTradesVolume = 0.1; // Max Allowed Recovery Volume
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert ...
|
|
||||||
input group "Alert";
|
|
||||||
input bool eAEnableAlerts = true; // Enable Alerts
|
|
||||||
input bool eALogAlerts = true; // Log Alerts
|
|
||||||
input bool eAMailAlerts = false; // Mail Alerts
|
|
||||||
input bool eAPushAlerts = false; // Push Alerts
|
|
||||||
input bool eATerminalAlerts = false; // Terminal Alerts
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Report ...
|
|
||||||
input group "Reports";
|
|
||||||
input bool eaAlertNewMonths = false; // Alert New Month
|
|
||||||
input bool eaAlertNewWeeks = false; // Alert New Weeks
|
|
||||||
input bool eaAlertNewDays = true; // Alert New Days
|
|
||||||
input bool eaAlertNewHours = false; // Alert New Hours
|
|
||||||
input bool eaAlertRawSignals = false; // Alert Raw Signals
|
|
||||||
input bool eaAlertStrongSignals = false; // Alert Strong Signals
|
|
||||||
|
|
||||||
//
|
|
||||||
// Variables ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Style nad Time Instances ...
|
|
||||||
XSCXCTHelper *mCTHelper;
|
|
||||||
XSCXCCHelper *mCCHelper;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Registred Strategies ...
|
|
||||||
XSCBaseStrategy *strategies[];
|
|
||||||
|
|
||||||
//
|
|
||||||
// Trader Instance Class ...
|
|
||||||
XSCTrade *eaTrader;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert Class ...
|
|
||||||
XSCAlert *eaAlert;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Tracker ...
|
|
||||||
XTimeTracker eaTimeTracker;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Trade Handler Instance Class ...
|
|
||||||
XSCXTradeHandler *eaTradeHandler;
|
|
||||||
|
|
||||||
//
|
|
||||||
string mTag = "";
|
|
||||||
|
|
||||||
//
|
|
||||||
int forceSignalsCount = 0;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Event Handlers ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialization ...
|
|
||||||
int OnInit()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (!ValidateInputs())
|
|
||||||
{
|
|
||||||
return INIT_PARAMETERS_INCORRECT;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!InitialEA())
|
|
||||||
{
|
|
||||||
return INIT_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
mTag =
|
|
||||||
!IsValid(eaLogSuffix)
|
|
||||||
? ShortName
|
|
||||||
: ShortName + "_" + eaLogSuffix;
|
|
||||||
|
|
||||||
//
|
|
||||||
drawPrefix = mTag;
|
|
||||||
|
|
||||||
//
|
|
||||||
// EventSetTimer(1);
|
|
||||||
// EventSetMillisecondTimer(100);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert Initialization ...
|
|
||||||
string msg = "Initialized Successfully ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Init Succeed ...
|
|
||||||
return INIT_SUCCEEDED;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// DeInitialization ...
|
|
||||||
void OnDeinit(const int reason)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
|
|
||||||
// REASON_REMOVE 1 Program removed from a chart
|
|
||||||
// REASON_RECOMPILE 2 Program recompiled
|
|
||||||
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
|
|
||||||
// REASON_CHARTCLOSE 4 Chart closed
|
|
||||||
// REASON_PARAMETERS 5 Inputs changed by a user
|
|
||||||
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
|
|
||||||
// REASON_TEMPLATE 7 Another chart template applied
|
|
||||||
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
|
|
||||||
// REASON_CLOSE 9 Terminal closed
|
|
||||||
|
|
||||||
//
|
|
||||||
string msg = "MaxSameTimeTrades: " + ToString(eaTradeHandler.MaxSameTimeTrades()) + "\n" +
|
|
||||||
"ForceSignalsCount: " + ToString(forceSignalsCount);
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert De Initialization Succeeded ...
|
|
||||||
msg = "De Initialized Successfully ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// De Initialize XSampleEA Providers ...
|
|
||||||
delete mCTHelper;
|
|
||||||
delete mCCHelper;
|
|
||||||
|
|
||||||
//
|
|
||||||
delete eaAlert;
|
|
||||||
delete eaTrader;
|
|
||||||
delete eaTradeHandler;
|
|
||||||
|
|
||||||
//
|
|
||||||
// EventKillTimer();
|
|
||||||
|
|
||||||
//
|
|
||||||
Clean(strategies);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// On Tick Handler ...
|
|
||||||
void OnTick()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
HandleTradeState();
|
|
||||||
HandleTimeReport();
|
|
||||||
HandleStrategiesOnTick();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle On Trade Event ...
|
|
||||||
void OnTrade()
|
|
||||||
{
|
|
||||||
eaTrader.HandleOnTrade();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// On Timer ...
|
|
||||||
void OnTimer()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// HandleTradeState();
|
|
||||||
// HandleTimeReport();
|
|
||||||
// HandleStrategiesOnTick();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Custom Functions ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Validate Inputs ...
|
|
||||||
bool ValidateInputs()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
//
|
|
||||||
string errMessage = "";
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isCommonValid = (
|
|
||||||
//
|
|
||||||
eaSlippage > 0 &&
|
|
||||||
eaMagicNumber > 0
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isCommonValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "common configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isSymbolsValid = (
|
|
||||||
//
|
|
||||||
eaMultiSymbol
|
|
||||||
? IsValid(eaSymbols)
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isSymbolsValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "symbols configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isRiskRewardValid = (
|
|
||||||
//
|
|
||||||
(eaUseFixedTPSLPoints
|
|
||||||
? eaTPPoint > 0 ||
|
|
||||||
eaSLPoint > 0
|
|
||||||
: eaR2R > 0) &&
|
|
||||||
!(eaIgnoreTP &&
|
|
||||||
eaIgnoreSL)
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isRiskRewardValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "risk/reward configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isVolumeValid = (
|
|
||||||
//
|
|
||||||
eaUseDynamicVolume
|
|
||||||
? eaDynamicVolumeStep >= 0.01 &&
|
|
||||||
eaDynamicVolumeBalanceFactor > 0
|
|
||||||
: eaVolume >= 0.01
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isVolumeValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "volume configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isTradeManagementValid = (
|
|
||||||
//
|
|
||||||
!(!eaAllowLong && !eaAllowShort) &&
|
|
||||||
eaMaxAllowedLongs >= 0 &&
|
|
||||||
eaMaxAllowedShorts >= 0
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isTradeManagementValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "trade management configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isHedgingValid = (
|
|
||||||
//
|
|
||||||
eaAllowHedging
|
|
||||||
? eaMinTradesForHedge >= 1 &&
|
|
||||||
eaHedgeVolumeFactor >= 0.01 &&
|
|
||||||
eaMinimumProfitPerVolumeForHedging >= 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isHedgingValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "hedging configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isTraillingValid = (
|
|
||||||
//
|
|
||||||
eaAllowTrailStop
|
|
||||||
? eaStartTrailInPoint > 0 &&
|
|
||||||
eaTrailStepInPoint > 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isTraillingValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "trailling configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isRecoveryValid = (
|
|
||||||
//
|
|
||||||
eaAllowRecovery
|
|
||||||
? eaRecoveryTPPoint > 0 &&
|
|
||||||
eaRecoverySLPoint > 0 &&
|
|
||||||
eaMaxAllowedRecoveryTrades >= 0 &&
|
|
||||||
eaRecoveryVolumeMultiplier > 0 &&
|
|
||||||
eaMaxAllowedRecoveryTradesVolume >= 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isRecoveryValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "recovery configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
result =
|
|
||||||
//
|
|
||||||
// Common ...
|
|
||||||
isCommonValid &&
|
|
||||||
//
|
|
||||||
// Symbols ...
|
|
||||||
isSymbolsValid &&
|
|
||||||
//
|
|
||||||
// Risk/Reward ...
|
|
||||||
isRiskRewardValid &&
|
|
||||||
//
|
|
||||||
// Volume ...
|
|
||||||
isVolumeValid &&
|
|
||||||
//
|
|
||||||
// Trade Management ...
|
|
||||||
isTradeManagementValid &&
|
|
||||||
//
|
|
||||||
// Hedging ...
|
|
||||||
isHedgingValid &&
|
|
||||||
//
|
|
||||||
// Trailling ...
|
|
||||||
isTraillingValid &&
|
|
||||||
//
|
|
||||||
// Recovery ...
|
|
||||||
isRecoveryValid
|
|
||||||
//
|
|
||||||
;
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
Print("Errors: \n", errMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialize all Requirements ...
|
|
||||||
bool InitialEA()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Timer ...
|
|
||||||
XCTInputs ctInputs;
|
|
||||||
ctInputs.Default();
|
|
||||||
mCTHelper = new XSCXCTHelper();
|
|
||||||
result = mCTHelper.Init(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
ctInputs //
|
|
||||||
);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Styles ...
|
|
||||||
XCCInputs ccInputs;
|
|
||||||
ccInputs.Default();
|
|
||||||
mCCHelper = new XSCXCCHelper();
|
|
||||||
result = mCCHelper.Init(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
ccInputs //
|
|
||||||
);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Instance of Alert Class ...
|
|
||||||
eaAlert = new XSCAlert();
|
|
||||||
eaAlert.SetPrefix(mTag);
|
|
||||||
eaAlert.SetLogAlerts(eALogAlerts);
|
|
||||||
eaAlert.SetMailAlerts(eAMailAlerts);
|
|
||||||
eaAlert.SetPushAlerts(eAPushAlerts);
|
|
||||||
eaAlert.SetEnableAlerts(eAEnableAlerts);
|
|
||||||
eaAlert.SetTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Trader Instance ...
|
|
||||||
eaTrader = new XSCTrade(
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber //
|
|
||||||
);
|
|
||||||
eaTrader.AddOnStopLossEventHandler(OnStopLossTriggered);
|
|
||||||
eaTrader.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
|
|
||||||
eaTrader.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialize Trade Handler ...
|
|
||||||
eaTradeHandler = new XSCXTradeHandler(eaTrader);
|
|
||||||
eaTradeHandler.SaveTrades(true);
|
|
||||||
eaTradeHandler.SaveSignals(true);
|
|
||||||
eaTradeHandler.SaveConditions(true);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
eaTradeHandler.SetAlertPrefix(mTag);
|
|
||||||
eaTradeHandler.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
eaTradeHandler.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
eaTradeHandler.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
eaTradeHandler.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
eaTradeHandler.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Trade Management ...
|
|
||||||
eaTradeHandler.AllowLong(eaAllowLong);
|
|
||||||
eaTradeHandler.AllowShort(eaAllowShort);
|
|
||||||
eaTradeHandler.MaxAllowedLongs(eaMaxAllowedLongs);
|
|
||||||
eaTradeHandler.MaxAllowedShorts(eaMaxAllowedShorts);
|
|
||||||
eaTradeHandler.UseMaxAllowedTradesPerSymbol(eaUseMaxAllowedTradesPerSymbol);
|
|
||||||
eaTradeHandler.MaxDrawdownPercentForOpenTrades(eaMaxDrawdownPercentForOpenTrades);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Hedging ...
|
|
||||||
eaTradeHandler.AllowHedging(eaAllowHedging);
|
|
||||||
eaTradeHandler.HedgeVolumeFactor(eaHedgeVolumeFactor);
|
|
||||||
eaTradeHandler.MinTradesForHedge(eaMinTradesForHedge);
|
|
||||||
eaTradeHandler.HedgeMinProfitPerVolumeFactor(eaMinimumProfitPerVolumeForHedging);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Trailing ...
|
|
||||||
eaTradeHandler.AllowTrailStops(eaAllowTrailStop);
|
|
||||||
eaTradeHandler.TrailBasedOnProfit(true);
|
|
||||||
eaTradeHandler.TrailStartInPoint(eaStartTrailInPoint);
|
|
||||||
eaTradeHandler.TrailStepInPoint(eaTrailStepInPoint);
|
|
||||||
eaTradeHandler.IgnoreTPOnTrail(eaIgnoreTPAfterTrail);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Recovery ...
|
|
||||||
eaTradeHandler.AllowRecover(eaAllowRecovery);
|
|
||||||
eaTradeHandler.ForcePressuresInRecovery(false);
|
|
||||||
eaTradeHandler.RecoveryTPPoint(eaRecoveryTPPoint);
|
|
||||||
eaTradeHandler.RecoverySLPoint(eaRecoverySLPoint);
|
|
||||||
eaTradeHandler.MaxAllowedRecover(eaMaxAllowedRecoveryTrades);
|
|
||||||
eaTradeHandler.RecoveryVolumeMultiplier(eaRecoveryVolumeMultiplier);
|
|
||||||
eaTradeHandler.MaxAllowedRecoveryVolume(eaMaxAllowedRecoveryTradesVolume);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Single Symbol ...
|
|
||||||
if (!eaMultiSymbol)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Create Class Instance ...
|
|
||||||
XSCXCHCHEStrategy *xchcheStrategy;
|
|
||||||
xchcheStrategy = new XSCXCHCHEStrategy(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
PERIOD_H1, // Analyzing Period
|
|
||||||
eaVolume,
|
|
||||||
eaR2R, // R2R ...
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber,
|
|
||||||
true, // Ignore Signal Execution
|
|
||||||
eaUseFixedTPSLPoints, // Use TP SL Point
|
|
||||||
false, // Use Max
|
|
||||||
eaTPPoint, // TP
|
|
||||||
eaSLPoint, // SL
|
|
||||||
eaIgnoreTP,
|
|
||||||
eaIgnoreSL,
|
|
||||||
eaAllowLong,
|
|
||||||
eaAllowShort,
|
|
||||||
eaMaxAllowedLongs, // Max Longs
|
|
||||||
eaMaxAllowedShorts // Max Shorts
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configuere Strategy ...
|
|
||||||
xchcheStrategy.DrawImportantAreas(false);
|
|
||||||
xchcheStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
xchcheStrategy.SetAlertPrefix(mTag);
|
|
||||||
xchcheStrategy.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
xchcheStrategy.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
xchcheStrategy.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
xchcheStrategy.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
xchcheStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
RegisterStrategy(xchcheStrategy);
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// Multi Symbol ...
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Parse Symbols ...
|
|
||||||
string symbols[];
|
|
||||||
int symbolsCount = SplitContent(
|
|
||||||
symbols,
|
|
||||||
eaSymbols //
|
|
||||||
);
|
|
||||||
result = IsValidSize(symbolsCount);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
for (int i = 0; i < symbolsCount; i++)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Select Symbol ...
|
|
||||||
string iSymbol = symbols[i];
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Class Instance ...
|
|
||||||
XSCXCHCHEStrategy *iStrategy;
|
|
||||||
iStrategy = new XSCXCHCHEStrategy(
|
|
||||||
iSymbol,
|
|
||||||
_Period,
|
|
||||||
PERIOD_H1, // Analyzing Period
|
|
||||||
eaVolume,
|
|
||||||
eaR2R, // R2R ...
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber,
|
|
||||||
true, // Ignore Signal Execution
|
|
||||||
eaUseFixedTPSLPoints, // Use TP SL Point
|
|
||||||
false, // Use Max
|
|
||||||
eaTPPoint, // TP
|
|
||||||
eaSLPoint, // SL
|
|
||||||
eaIgnoreTP,
|
|
||||||
eaIgnoreSL,
|
|
||||||
eaAllowLong,
|
|
||||||
eaAllowShort,
|
|
||||||
eaMaxAllowedLongs, // Max Longs
|
|
||||||
eaMaxAllowedShorts // Max Shorts
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configuere Strategy ...
|
|
||||||
iStrategy.DrawImportantAreas(false);
|
|
||||||
iStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
iStrategy.SetAlertPrefix(mTag);
|
|
||||||
iStrategy.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
iStrategy.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
iStrategy.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
iStrategy.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
iStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
RegisterStrategy(iStrategy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
void RegisterStrategy(XSCBaseStrategy *strategy)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (strategy == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
ArrayResize(
|
|
||||||
strategies,
|
|
||||||
ArraySize(strategies) + 1 //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
strategies[ArraySize(strategies) - 1] = strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Call All Registered Strategis Tick Handler Functions ...
|
|
||||||
void HandleStrategiesOnTick()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
int count = ArraySize(strategies);
|
|
||||||
if (!IsValidSize(count))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Call Tick Handler Function ...
|
|
||||||
strategies[i].HandleTick();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Manage Trading States ...
|
|
||||||
void HandleTradeState()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Implement All Trade States here ...
|
|
||||||
eaTradeHandler.UpdateData();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Do Protection Senarios ...
|
|
||||||
eaTradeHandler.HandleProtection();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Reporting ...
|
|
||||||
void HandleTimeReport()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Monthly Report ....
|
|
||||||
if (eaAlertNewMonths &&
|
|
||||||
eaTimeTracker.IsNewMonth())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Month ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Weekly Report ....
|
|
||||||
if (eaAlertNewWeeks &&
|
|
||||||
eaTimeTracker.IsNewWeek())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Week ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Daily Report ....
|
|
||||||
if (eaAlertNewDays &&
|
|
||||||
eaTimeTracker.IsNewDay())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Day ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Hourly Report ....
|
|
||||||
if (eaAlertNewHours &&
|
|
||||||
eaTimeTracker.IsNewHour())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Hour ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// SIGNAL EVENT Handlers ...
|
|
||||||
//
|
|
||||||
|
|
||||||
//
|
|
||||||
// All Signals from Strategies Comming Here for Execution or
|
|
||||||
// Analyzing ...
|
|
||||||
void OnSignalRecieved(
|
|
||||||
XSignal &signal,
|
|
||||||
XCHStrategyConditions &conditions //
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Check Signal and Conditions Validations ...
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!signal.IsValid())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!conditions.IsValid())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Notify When a Raw (Unfiltered) Signal Recieved
|
|
||||||
// if it's Provided ...
|
|
||||||
if (eaAlertRawSignals)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "Raw " + (IsLong(signal.type) ? "Long" : "Short") + " Signal Recieved: (" + signal.provider + "," + signal.symbol + "," + ToString(signal.period) + ") ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Prepare Signal Dynamic Volume
|
|
||||||
// if Provided ...
|
|
||||||
if (eaUseDynamicVolume &&
|
|
||||||
eaDynamicVolumeStep > 0 &&
|
|
||||||
eaDynamicVolumeBalanceFactor > 0)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
double dVolume = eaTrader.GetDynamicVolume(
|
|
||||||
signal.symbol,
|
|
||||||
eaDynamicVolumeBalanceFactor,
|
|
||||||
eaDynamicVolumeStep //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
if (dVolume > 0)
|
|
||||||
{
|
|
||||||
signal.volume = dVolume;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
eaTradeHandler.UpdateSignal(signal);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Since Signal Filtering done based on
|
|
||||||
// Strategy and it's not related to Public
|
|
||||||
// TradeHandler for Seperation of Concern issue,
|
|
||||||
// we do this here ...
|
|
||||||
XCHCHEStartegyConditionsParser parser;
|
|
||||||
bool isFiltered = parser.IsFiltered(
|
|
||||||
signal,
|
|
||||||
conditions //
|
|
||||||
);
|
|
||||||
if (isFiltered)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
eaTradeHandler.RemoveSignal(signal);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// //
|
|
||||||
// // Here we Can Increase Signal Volume if
|
|
||||||
// // it is not force Filtered ...
|
|
||||||
// bool isForceFiltered = parser.IsFiltered(
|
|
||||||
// signal,
|
|
||||||
// conditions,
|
|
||||||
// true //
|
|
||||||
// );
|
|
||||||
// if (!isForceFiltered)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// // TODO:
|
|
||||||
// // Implement Strong Signals Conditions
|
|
||||||
// // Senarios ...
|
|
||||||
// forceSignalsCount++;
|
|
||||||
|
|
||||||
// //
|
|
||||||
// // Alert Froce Strong Signals
|
|
||||||
// // if Provided ...
|
|
||||||
// if (eaAlertStrongSignals)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// string msg = "Foce Strong " + (IsLong(signal.type) ? "Long" : "Short") + " Signal Recieved: (" + signal.provider + "," + signal.symbol + "," + ToString(signal.period) + ") ...";
|
|
||||||
// eaAlert.Alert(msg);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
//
|
|
||||||
// Attach Conditions Summary to Signal ...
|
|
||||||
string conditionsSummary = conditions.GenerateSummary(
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
false //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
signal.conditions = parser.GenerateSignalConditions(
|
|
||||||
signal,
|
|
||||||
conditions //
|
|
||||||
); // conditionsSummary;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Execute Signal Using Trade Handler ...
|
|
||||||
ENUM_X_SIGNAL_EXECUTION_RESULT state;
|
|
||||||
bool isExecuted = eaTradeHandler.ExecuteSignal(
|
|
||||||
signal,
|
|
||||||
state //
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle StopLoss Triggered Positions ...
|
|
||||||
void OnStopLossTriggered(const XDeal &deal)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(deal);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle Take Profit Triggered Positions ...
|
|
||||||
void OnTakeProfitTriggered(const XDeal &deal)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(deal);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle Force Close a Position ...
|
|
||||||
void OnPositionForceClosed(
|
|
||||||
const ulong ticket,
|
|
||||||
const XPosition &position,
|
|
||||||
const string comment //
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(
|
|
||||||
ticket,
|
|
||||||
position,
|
|
||||||
comment //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Report Account Balance ...
|
|
||||||
void ReportBalance()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (!IsRunningOnTestMode())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
string msg = "Balance: " + ToString(eaTrader.mAccount.GetBalance());
|
|
||||||
eaAlert.Log(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
@@ -1,974 +0,0 @@
|
|||||||
///////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// SaherElm IT Center XTest MQL5 Expert Advisor
|
|
||||||
// -------------------------------------------------
|
|
||||||
// Name: XCHEA
|
|
||||||
// Description: an Exper Advisor which used XCHSetup
|
|
||||||
// to Analyse Market ...
|
|
||||||
//
|
|
||||||
// Maintainer:
|
|
||||||
// ------------
|
|
||||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
||||||
//
|
|
||||||
//////////////////////////////////////////////////////x
|
|
||||||
//
|
|
||||||
// Global Properties ...
|
|
||||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
||||||
#property link "https://saherelm.ir"
|
|
||||||
#property version "1.00"
|
|
||||||
#property description "SaherElm XCHEA"
|
|
||||||
#property strict
|
|
||||||
|
|
||||||
//
|
|
||||||
#define ShortName "XCHEA"
|
|
||||||
|
|
||||||
//
|
|
||||||
// Imports ...
|
|
||||||
#include "../Helpers/x-saherelm.xct.helper.mq5"
|
|
||||||
#include "../Helpers/x-saherelm.xcc.helper.mq5"
|
|
||||||
|
|
||||||
//
|
|
||||||
#include "../Parsers/x-saherelm.xch.parser.mq5"
|
|
||||||
#include "../Strategies/x-saherelm.xch.strategy.mq5"
|
|
||||||
|
|
||||||
//
|
|
||||||
// Inputs ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Common ...
|
|
||||||
input group "Common";
|
|
||||||
input long eaMagicNumber = 78692110; // Magic Number
|
|
||||||
input int eaSlippage = 10; // Slippgae
|
|
||||||
input string eaLogSuffix = ""; // Log Suffix
|
|
||||||
|
|
||||||
//
|
|
||||||
// Symbols ...
|
|
||||||
input group "Symbols";
|
|
||||||
input bool eaMultiSymbol = false; // Use Multi Symbol
|
|
||||||
input bool eaUseMaxAllowedTradesPerSymbol = true; // Use Max Allowed Trades Per Symbol
|
|
||||||
input string eaSymbols = "EURUSDb,GBPUSDb,EURGBPb,USDJPYb,EURJPYb,USDCHFb,EURCHFb,AUDUSDb,EURAUDb,NZDUSDb,EURNZDb,USDCADb,EURCADb,XAUUSDb,XAGUSDb,BRENT,DowJones30"; // Trading Symbols
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management ...
|
|
||||||
input group "Risk Management";
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Risk / Reward Calculation ...
|
|
||||||
input group "Risk/Reward";
|
|
||||||
input double eaR2R = 1.5; // Risk to Reward Ratio for Dynamic TP SL
|
|
||||||
input bool eaUseFixedTPSLPoints = true; // Use Fixed TP SL in Points
|
|
||||||
input bool eaIgnoreTP = false; // Ignore TP
|
|
||||||
input bool eaIgnoreSL = false; // Ignore SL
|
|
||||||
input double eaTPPoint = 200; // Static TP Point
|
|
||||||
input double eaSLPoint = 200; // Static SL Point
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Volume Calculation ...
|
|
||||||
input group "Volume";
|
|
||||||
input bool eaUseDynamicVolume = false; // Use Dynamic Volume
|
|
||||||
input double eaDynamicVolumeStep = 0.01; // Increase Volume Step
|
|
||||||
input double eaDynamicVolumeBalanceFactor = 200; // Balance Factor for Generate Dynamic Volume
|
|
||||||
input double eaVolume = 0.02; // Static Volume
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trade Management ...
|
|
||||||
input group "Trade Management";
|
|
||||||
input bool eaAllowLong = true; // Allow Long Positions
|
|
||||||
input bool eaAllowShort = true; // Allow Short Positions
|
|
||||||
input int eaMaxAllowedLongs = 2; // Max Allowed Long Positions
|
|
||||||
input int eaMaxAllowedShorts = 2; // Max Allowed Short Positions
|
|
||||||
input double eaMaxDrawdownPercentForOpenTrades = 3; // Max Drawdown Percent for Open Trades
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Hedging ...
|
|
||||||
input group "Hedging";
|
|
||||||
input bool eaAllowHedging = false; // Allow Hedging Trades
|
|
||||||
input int eaMinTradesForHedge = 2; // Minimum Open Trades for Hedging
|
|
||||||
input double eaHedgeVolumeFactor = 0.01; // Volume Factor for Hedging
|
|
||||||
input double eaMinimumProfitPerVolumeForHedging = 0.5; // Minimum Required Profit per Volume for Hedging
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trailling Stop ...
|
|
||||||
input group "Trailling Stop";
|
|
||||||
input bool eaAllowTrailStop = false; // Allow Trail Stop
|
|
||||||
input double eaStartTrailInPoint = 50; // Trail Start After Profit (Point)
|
|
||||||
input double eaTrailStepInPoint = 25; // Trail Size in each Step (Point)
|
|
||||||
input bool eaIgnoreTPAfterTrail = true; // Ignore TP after Trail
|
|
||||||
|
|
||||||
//
|
|
||||||
// Risk Management > Trades Recovery ...
|
|
||||||
input group "Trades Recovery";
|
|
||||||
input bool eaAllowRecovery = false; // Allow Recover Trades
|
|
||||||
input double eaRecoveryTPPoint = 150; // Recovery TP (Point)
|
|
||||||
input double eaRecoverySLPoint = 200; // Recovery SL (Point)
|
|
||||||
input int eaMaxAllowedRecoveryTrades = 0; // Max Allowed Recovery Trades
|
|
||||||
input double eaRecoveryVolumeMultiplier = 2; // Recovery Volume Multiplier
|
|
||||||
input double eaMaxAllowedRecoveryTradesVolume = 0.1; // Max Allowed Recovery Volume
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert ...
|
|
||||||
input group "Alert";
|
|
||||||
input bool eAEnableAlerts = true; // Enable Alerts
|
|
||||||
input bool eALogAlerts = true; // Log Alerts
|
|
||||||
input bool eAMailAlerts = false; // Mail Alerts
|
|
||||||
input bool eAPushAlerts = false; // Push Alerts
|
|
||||||
input bool eATerminalAlerts = false; // Terminal Alerts
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Report ...
|
|
||||||
input group "Reports";
|
|
||||||
input bool eaAlertNewMonths = false; // Alert New Month
|
|
||||||
input bool eaAlertNewWeeks = false; // Alert New Weeks
|
|
||||||
input bool eaAlertNewDays = true; // Alert New Days
|
|
||||||
input bool eaAlertNewHours = false; // Alert New Hours
|
|
||||||
input bool eaAlertRawSignals = false; // Alert Raw Signals
|
|
||||||
input bool eaAlertStrongSignals = false; // Alert Strong Signals
|
|
||||||
|
|
||||||
//
|
|
||||||
// Variables ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Style nad Time Instances ...
|
|
||||||
XSCXCTHelper *mCTHelper;
|
|
||||||
XSCXCCHelper *mCCHelper;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Registred Strategies ...
|
|
||||||
XSCBaseStrategy *strategies[];
|
|
||||||
|
|
||||||
//
|
|
||||||
// Trader Instance Class ...
|
|
||||||
XSCTrade *eaTrader;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert Class ...
|
|
||||||
XSCAlert *eaAlert;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Tracker ...
|
|
||||||
XTimeTracker eaTimeTracker;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Trade Handler Instance Class ...
|
|
||||||
XSCXTradeHandler *eaTradeHandler;
|
|
||||||
|
|
||||||
//
|
|
||||||
string mTag = "";
|
|
||||||
|
|
||||||
//
|
|
||||||
int forceSignalsCount = 0;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Event Handlers ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialization ...
|
|
||||||
int OnInit()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (!ValidateInputs())
|
|
||||||
{
|
|
||||||
return INIT_PARAMETERS_INCORRECT;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!InitialEA())
|
|
||||||
{
|
|
||||||
return INIT_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
mTag =
|
|
||||||
!IsValid(eaLogSuffix)
|
|
||||||
? ShortName
|
|
||||||
: ShortName + "_" + eaLogSuffix;
|
|
||||||
|
|
||||||
//
|
|
||||||
drawPrefix = mTag;
|
|
||||||
|
|
||||||
//
|
|
||||||
// EventSetTimer(1);
|
|
||||||
// EventSetMillisecondTimer(100);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert Initialization ...
|
|
||||||
string msg = "Initialized Successfully ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Init Succeed ...
|
|
||||||
return INIT_SUCCEEDED;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// DeInitialization ...
|
|
||||||
void OnDeinit(const int reason)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
|
|
||||||
// REASON_REMOVE 1 Program removed from a chart
|
|
||||||
// REASON_RECOMPILE 2 Program recompiled
|
|
||||||
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
|
|
||||||
// REASON_CHARTCLOSE 4 Chart closed
|
|
||||||
// REASON_PARAMETERS 5 Inputs changed by a user
|
|
||||||
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
|
|
||||||
// REASON_TEMPLATE 7 Another chart template applied
|
|
||||||
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
|
|
||||||
// REASON_CLOSE 9 Terminal closed
|
|
||||||
|
|
||||||
//
|
|
||||||
string msg = "MaxSameTimeTrades: " + ToString(eaTradeHandler.MaxSameTimeTrades()) + "\n" +
|
|
||||||
"ForceSignalsCount: " + ToString(forceSignalsCount);
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Alert De Initialization Succeeded ...
|
|
||||||
msg = "De Initialized Successfully ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
|
|
||||||
//
|
|
||||||
// De Initialize XSampleEA Providers ...
|
|
||||||
delete mCTHelper;
|
|
||||||
delete mCCHelper;
|
|
||||||
|
|
||||||
//
|
|
||||||
delete eaAlert;
|
|
||||||
delete eaTrader;
|
|
||||||
delete eaTradeHandler;
|
|
||||||
|
|
||||||
//
|
|
||||||
// EventKillTimer();
|
|
||||||
|
|
||||||
//
|
|
||||||
Clean(strategies);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// On Tick Handler ...
|
|
||||||
void OnTick()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
HandleTradeState();
|
|
||||||
HandleTimeReport();
|
|
||||||
HandleStrategiesOnTick();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle On Trade Event ...
|
|
||||||
void OnTrade()
|
|
||||||
{
|
|
||||||
eaTrader.HandleOnTrade();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// On Timer ...
|
|
||||||
void OnTimer()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// HandleTradeState();
|
|
||||||
// HandleTimeReport();
|
|
||||||
// HandleStrategiesOnTick();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Custom Functions ...
|
|
||||||
|
|
||||||
//
|
|
||||||
// Validate Inputs ...
|
|
||||||
bool ValidateInputs()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
//
|
|
||||||
string errMessage = "";
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isCommonValid = (
|
|
||||||
//
|
|
||||||
eaSlippage > 0 &&
|
|
||||||
eaMagicNumber > 0
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isCommonValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "common configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isSymbolsValid = (
|
|
||||||
//
|
|
||||||
eaMultiSymbol
|
|
||||||
? IsValid(eaSymbols)
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isSymbolsValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "symbols configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isRiskRewardValid = (
|
|
||||||
//
|
|
||||||
(eaUseFixedTPSLPoints
|
|
||||||
? eaTPPoint > 0 ||
|
|
||||||
eaSLPoint > 0
|
|
||||||
: eaR2R > 0) &&
|
|
||||||
!(eaIgnoreTP &&
|
|
||||||
eaIgnoreSL)
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isRiskRewardValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "risk/reward configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isVolumeValid = (
|
|
||||||
//
|
|
||||||
eaUseDynamicVolume
|
|
||||||
? eaDynamicVolumeStep >= 0.01 &&
|
|
||||||
eaDynamicVolumeBalanceFactor > 0
|
|
||||||
: eaVolume >= 0.01
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isVolumeValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "volume configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isTradeManagementValid = (
|
|
||||||
//
|
|
||||||
!(!eaAllowLong && !eaAllowShort) &&
|
|
||||||
eaMaxAllowedLongs >= 0 &&
|
|
||||||
eaMaxAllowedShorts >= 0
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isTradeManagementValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "trade management configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isHedgingValid = (
|
|
||||||
//
|
|
||||||
eaAllowHedging
|
|
||||||
? eaMinTradesForHedge >= 1 &&
|
|
||||||
eaHedgeVolumeFactor >= 0.01 &&
|
|
||||||
eaMinimumProfitPerVolumeForHedging >= 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isHedgingValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "hedging configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isTraillingValid = (
|
|
||||||
//
|
|
||||||
eaAllowTrailStop
|
|
||||||
? eaStartTrailInPoint > 0 &&
|
|
||||||
eaTrailStepInPoint > 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isTraillingValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "trailling configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
bool isRecoveryValid = (
|
|
||||||
//
|
|
||||||
eaAllowRecovery
|
|
||||||
? eaRecoveryTPPoint > 0 &&
|
|
||||||
eaRecoverySLPoint > 0 &&
|
|
||||||
eaMaxAllowedRecoveryTrades >= 0 &&
|
|
||||||
eaRecoveryVolumeMultiplier > 0 &&
|
|
||||||
eaMaxAllowedRecoveryTradesVolume >= 0
|
|
||||||
: true
|
|
||||||
//
|
|
||||||
);
|
|
||||||
if (!isRecoveryValid)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
errMessage += "recovery configurations error;" + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
result =
|
|
||||||
//
|
|
||||||
// Common ...
|
|
||||||
isCommonValid &&
|
|
||||||
//
|
|
||||||
// Symbols ...
|
|
||||||
isSymbolsValid &&
|
|
||||||
//
|
|
||||||
// Risk/Reward ...
|
|
||||||
isRiskRewardValid &&
|
|
||||||
//
|
|
||||||
// Volume ...
|
|
||||||
isVolumeValid &&
|
|
||||||
//
|
|
||||||
// Trade Management ...
|
|
||||||
isTradeManagementValid &&
|
|
||||||
//
|
|
||||||
// Hedging ...
|
|
||||||
isHedgingValid &&
|
|
||||||
//
|
|
||||||
// Trailling ...
|
|
||||||
isTraillingValid &&
|
|
||||||
//
|
|
||||||
// Recovery ...
|
|
||||||
isRecoveryValid
|
|
||||||
//
|
|
||||||
;
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
Print("Errors: \n", errMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialize all Requirements ...
|
|
||||||
bool InitialEA()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Timer ...
|
|
||||||
XCTInputs ctInputs;
|
|
||||||
ctInputs.Default();
|
|
||||||
mCTHelper = new XSCXCTHelper();
|
|
||||||
result = mCTHelper.Init(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
ctInputs //
|
|
||||||
);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Bar Styles ...
|
|
||||||
XCCInputs ccInputs;
|
|
||||||
ccInputs.Default();
|
|
||||||
mCCHelper = new XSCXCCHelper();
|
|
||||||
result = mCCHelper.Init(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
ccInputs //
|
|
||||||
);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Instance of Alert Class ...
|
|
||||||
eaAlert = new XSCAlert();
|
|
||||||
eaAlert.SetPrefix(mTag);
|
|
||||||
eaAlert.SetLogAlerts(eALogAlerts);
|
|
||||||
eaAlert.SetMailAlerts(eAMailAlerts);
|
|
||||||
eaAlert.SetPushAlerts(eAPushAlerts);
|
|
||||||
eaAlert.SetEnableAlerts(eAEnableAlerts);
|
|
||||||
eaAlert.SetTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Trader Instance ...
|
|
||||||
eaTrader = new XSCTrade(
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber //
|
|
||||||
);
|
|
||||||
eaTrader.AddOnStopLossEventHandler(OnStopLossTriggered);
|
|
||||||
eaTrader.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
|
|
||||||
eaTrader.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Initialize Trade Handler ...
|
|
||||||
eaTradeHandler = new XSCXTradeHandler(eaTrader);
|
|
||||||
eaTradeHandler.SaveTrades(true);
|
|
||||||
eaTradeHandler.SaveSignals(true);
|
|
||||||
eaTradeHandler.SaveConditions(true);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
eaTradeHandler.SetAlertPrefix(mTag);
|
|
||||||
eaTradeHandler.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
eaTradeHandler.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
eaTradeHandler.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
eaTradeHandler.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
eaTradeHandler.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Trade Management ...
|
|
||||||
eaTradeHandler.AllowLong(eaAllowLong);
|
|
||||||
eaTradeHandler.AllowShort(eaAllowShort);
|
|
||||||
eaTradeHandler.MaxAllowedLongs(eaMaxAllowedLongs);
|
|
||||||
eaTradeHandler.MaxAllowedShorts(eaMaxAllowedShorts);
|
|
||||||
eaTradeHandler.UseMaxAllowedTradesPerSymbol(eaUseMaxAllowedTradesPerSymbol);
|
|
||||||
eaTradeHandler.MaxDrawdownPercentForOpenTrades(eaMaxDrawdownPercentForOpenTrades);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Hedging ...
|
|
||||||
eaTradeHandler.AllowHedging(eaAllowHedging);
|
|
||||||
eaTradeHandler.HedgeVolumeFactor(eaHedgeVolumeFactor);
|
|
||||||
eaTradeHandler.MinTradesForHedge(eaMinTradesForHedge);
|
|
||||||
eaTradeHandler.HedgeMinProfitPerVolumeFactor(eaMinimumProfitPerVolumeForHedging);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Trailing ...
|
|
||||||
eaTradeHandler.AllowTrailStops(eaAllowTrailStop);
|
|
||||||
eaTradeHandler.TrailBasedOnProfit(true);
|
|
||||||
eaTradeHandler.TrailStartInPoint(eaStartTrailInPoint);
|
|
||||||
eaTradeHandler.TrailStepInPoint(eaTrailStepInPoint);
|
|
||||||
eaTradeHandler.IgnoreTPOnTrail(eaIgnoreTPAfterTrail);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Recovery ...
|
|
||||||
eaTradeHandler.AllowRecover(eaAllowRecovery);
|
|
||||||
eaTradeHandler.ForcePressuresInRecovery(false);
|
|
||||||
eaTradeHandler.RecoveryTPPoint(eaRecoveryTPPoint);
|
|
||||||
eaTradeHandler.RecoverySLPoint(eaRecoverySLPoint);
|
|
||||||
eaTradeHandler.MaxAllowedRecover(eaMaxAllowedRecoveryTrades);
|
|
||||||
eaTradeHandler.RecoveryVolumeMultiplier(eaRecoveryVolumeMultiplier);
|
|
||||||
eaTradeHandler.MaxAllowedRecoveryVolume(eaMaxAllowedRecoveryTradesVolume);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Single Symbol ...
|
|
||||||
if (!eaMultiSymbol)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Create Class Instance ...
|
|
||||||
XSCXCHStrategy *xchStrategy;
|
|
||||||
xchStrategy = new XSCXCHStrategy(
|
|
||||||
_Symbol,
|
|
||||||
_Period,
|
|
||||||
PERIOD_H1, // Analyzing Period
|
|
||||||
eaVolume,
|
|
||||||
eaR2R, // R2R ...
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber,
|
|
||||||
true, // Ignore Signal Execution
|
|
||||||
eaUseFixedTPSLPoints, // Use TP SL Point
|
|
||||||
false, // Use Max
|
|
||||||
eaTPPoint, // TP
|
|
||||||
eaSLPoint, // SL
|
|
||||||
eaIgnoreTP,
|
|
||||||
eaIgnoreSL,
|
|
||||||
eaAllowLong,
|
|
||||||
eaAllowShort,
|
|
||||||
eaMaxAllowedLongs, // Max Longs
|
|
||||||
eaMaxAllowedShorts // Max Shorts
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configuere Strategy ...
|
|
||||||
xchStrategy.EnableXCHM(true);
|
|
||||||
xchStrategy.EnableXCHHK(true);
|
|
||||||
xchStrategy.EnableXCHSTR(true);
|
|
||||||
xchStrategy.EnableXCHCHE(true);
|
|
||||||
xchStrategy.DrawImportantAreas(false);
|
|
||||||
xchStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
xchStrategy.SetAlertPrefix(mTag);
|
|
||||||
xchStrategy.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
xchStrategy.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
xchStrategy.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
xchStrategy.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
xchStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
RegisterStrategy(xchStrategy);
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// Multi Symbol ...
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Parse Symbols ...
|
|
||||||
string symbols[];
|
|
||||||
int symbolsCount = SplitContent(
|
|
||||||
symbols,
|
|
||||||
eaSymbols //
|
|
||||||
);
|
|
||||||
result = IsValidSize(symbolsCount);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
for (int i = 0; i < symbolsCount; i++)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Select Symbol ...
|
|
||||||
string iSymbol = symbols[i];
|
|
||||||
|
|
||||||
//
|
|
||||||
// Create Class Instance ...
|
|
||||||
XSCXCHStrategy *iStrategy;
|
|
||||||
iStrategy = new XSCXCHStrategy(
|
|
||||||
iSymbol,
|
|
||||||
_Period,
|
|
||||||
PERIOD_H1, // Analyzing Period
|
|
||||||
eaVolume,
|
|
||||||
eaR2R, // R2R ...
|
|
||||||
eaSlippage,
|
|
||||||
eaMagicNumber,
|
|
||||||
true, // Ignore Signal Execution
|
|
||||||
eaUseFixedTPSLPoints, // Use TP SL Point
|
|
||||||
false, // Use Max
|
|
||||||
eaTPPoint, // TP
|
|
||||||
eaSLPoint, // SL
|
|
||||||
eaIgnoreTP,
|
|
||||||
eaIgnoreSL,
|
|
||||||
eaAllowLong,
|
|
||||||
eaAllowShort,
|
|
||||||
eaMaxAllowedLongs, // Max Longs
|
|
||||||
eaMaxAllowedShorts // Max Shorts
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configuere Strategy ...
|
|
||||||
iStrategy.EnableXCHM(true);
|
|
||||||
iStrategy.EnableXCHHK(true);
|
|
||||||
iStrategy.EnableXCHSTR(true);
|
|
||||||
iStrategy.EnableXCHCHE(true);
|
|
||||||
iStrategy.DrawImportantAreas(false);
|
|
||||||
iStrategy.AddXCHSignalEventHandler(OnSignalRecieved);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure Alerts ...
|
|
||||||
iStrategy.SetAlertPrefix(mTag);
|
|
||||||
iStrategy.SetAlertEnableAlerts(eAEnableAlerts);
|
|
||||||
iStrategy.SetAlertLogAlerts(eALogAlerts);
|
|
||||||
iStrategy.SetAlertMailAlerts(eAMailAlerts);
|
|
||||||
iStrategy.SetAlertPushAlerts(eAPushAlerts);
|
|
||||||
iStrategy.SetAlertTerminalAlerts(eATerminalAlerts);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
RegisterStrategy(iStrategy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Register Strategy ...
|
|
||||||
void RegisterStrategy(XSCBaseStrategy *strategy)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (strategy == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
ArrayResize(
|
|
||||||
strategies,
|
|
||||||
ArraySize(strategies) + 1 //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
strategies[ArraySize(strategies) - 1] = strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Call All Registered Strategis Tick Handler Functions ...
|
|
||||||
void HandleStrategiesOnTick()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
int count = ArraySize(strategies);
|
|
||||||
if (!IsValidSize(count))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Call Tick Handler Function ...
|
|
||||||
strategies[i].HandleTick();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Manage Trading States ...
|
|
||||||
void HandleTradeState()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Implement All Trade States here ...
|
|
||||||
eaTradeHandler.UpdateData();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Do Protection Senarios ...
|
|
||||||
eaTradeHandler.HandleProtection();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Time Reporting ...
|
|
||||||
void HandleTimeReport()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Monthly Report ....
|
|
||||||
if (eaAlertNewMonths &&
|
|
||||||
eaTimeTracker.IsNewMonth())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Month ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Weekly Report ....
|
|
||||||
if (eaAlertNewWeeks &&
|
|
||||||
eaTimeTracker.IsNewWeek())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Week ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Daily Report ....
|
|
||||||
if (eaAlertNewDays &&
|
|
||||||
eaTimeTracker.IsNewDay())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Day ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Hourly Report ....
|
|
||||||
if (eaAlertNewHours &&
|
|
||||||
eaTimeTracker.IsNewHour())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "New Hour ...";
|
|
||||||
|
|
||||||
//
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// SIGNAL EVENT Handlers ...
|
|
||||||
//
|
|
||||||
|
|
||||||
//
|
|
||||||
// All Signals from Strategies Comming Here for Execution or
|
|
||||||
// Analyzing ...
|
|
||||||
void OnSignalRecieved(
|
|
||||||
XSignal &signal,
|
|
||||||
XCHStrategyConditions &conditions //
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Check Signal and Conditions Validations ...
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!signal.IsValid())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
if (!conditions.IsValid())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Notify When a Raw (Unfiltered) Signal Recieved
|
|
||||||
// if it's Provided ...
|
|
||||||
if (eaAlertRawSignals)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
string msg = "Raw " + (IsLong(signal.type) ? "Long" : "Short") + " Signal Recieved: (" + signal.provider + "," + signal.symbol + "," + ToString(signal.period) + ") ...";
|
|
||||||
eaAlert.Alert(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Prepare Signal Dynamic Volume
|
|
||||||
// if Provided ...
|
|
||||||
if (eaUseDynamicVolume &&
|
|
||||||
eaDynamicVolumeStep > 0 &&
|
|
||||||
eaDynamicVolumeBalanceFactor > 0)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
double dVolume = eaTrader.GetDynamicVolume(
|
|
||||||
signal.symbol,
|
|
||||||
eaDynamicVolumeBalanceFactor,
|
|
||||||
eaDynamicVolumeStep //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
if (dVolume > 0)
|
|
||||||
{
|
|
||||||
signal.volume = dVolume;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
eaTradeHandler.UpdateSignal(signal);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Since Signal Filtering done based on
|
|
||||||
// Strategy and it's not related to Public
|
|
||||||
// TradeHandler for Seperation of Concern issue,
|
|
||||||
// we do this here ...
|
|
||||||
XCHStartegyConditionsParser parser;
|
|
||||||
bool isFiltered = parser.IsFiltered(
|
|
||||||
signal,
|
|
||||||
conditions //
|
|
||||||
);
|
|
||||||
if (isFiltered)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
eaTradeHandler.RemoveSignal(signal);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// //
|
|
||||||
// // Here we Can Increase Signal Volume if
|
|
||||||
// // it is not force Filtered ...
|
|
||||||
// bool isForceFiltered = parser.IsFiltered(
|
|
||||||
// signal,
|
|
||||||
// conditions,
|
|
||||||
// true //
|
|
||||||
// );
|
|
||||||
// if (!isForceFiltered)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// // TODO:
|
|
||||||
// // Implement Strong Signals Conditions
|
|
||||||
// // Senarios ...
|
|
||||||
// forceSignalsCount++;
|
|
||||||
|
|
||||||
// //
|
|
||||||
// // Alert Froce Strong Signals
|
|
||||||
// // if Provided ...
|
|
||||||
// if (eaAlertStrongSignals)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// string msg = "Foce Strong " + (IsLong(signal.type) ? "Long" : "Short") + " Signal Recieved: (" + signal.provider + "," + signal.symbol + "," + ToString(signal.period) + ") ...";
|
|
||||||
// eaAlert.Alert(msg);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
//
|
|
||||||
// Attach Conditions Summary to Signal ...
|
|
||||||
string conditionsSummary = conditions.GenerateSummary(
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
false //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
signal.conditions = parser.GenerateSignalConditions(
|
|
||||||
signal,
|
|
||||||
conditions //
|
|
||||||
); // conditionsSummary;
|
|
||||||
|
|
||||||
//
|
|
||||||
// Execute Signal Using Trade Handler ...
|
|
||||||
ENUM_X_SIGNAL_EXECUTION_RESULT state;
|
|
||||||
bool isExecuted = eaTradeHandler.ExecuteSignal(
|
|
||||||
signal,
|
|
||||||
state //
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle StopLoss Triggered Positions ...
|
|
||||||
void OnStopLossTriggered(const XDeal &deal)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(deal);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle Take Profit Triggered Positions ...
|
|
||||||
void OnTakeProfitTriggered(const XDeal &deal)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(deal);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Handle Force Close a Position ...
|
|
||||||
void OnPositionForceClosed(
|
|
||||||
const ulong ticket,
|
|
||||||
const XPosition &position,
|
|
||||||
const string comment //
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Finish ...
|
|
||||||
eaTradeHandler.Finish(
|
|
||||||
ticket,
|
|
||||||
position,
|
|
||||||
comment //
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
ReportBalance();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Report Account Balance ...
|
|
||||||
void ReportBalance()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
if (!IsRunningOnTestMode())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
string msg = "Balance: " + ToString(eaTrader.mAccount.GetBalance());
|
|
||||||
eaAlert.Log(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Decision_Candles_Colored.mq5 |
|
|
||||||
//| Copyright 2021, MetaQuotes Ltd. |
|
|
||||||
//| https://www.mql5.com |
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
#property copyright "Mohamed"
|
|
||||||
#property link "https://www.mql5.com/en/code/50992"
|
|
||||||
#property version "1.10"
|
|
||||||
#property strict
|
|
||||||
#property indicator_chart_window
|
|
||||||
#property indicator_buffers 8
|
|
||||||
#property indicator_plots 2
|
|
||||||
|
|
||||||
#property indicator_type1 DRAW_CANDLES
|
|
||||||
#property indicator_color1 clrBlue, clrAqua
|
|
||||||
#property indicator_style1 STYLE_SOLID
|
|
||||||
#property indicator_width1 1
|
|
||||||
|
|
||||||
#property indicator_type2 DRAW_CANDLES
|
|
||||||
#property indicator_color2 clrRed, clrDarkOrange
|
|
||||||
#property indicator_style2 STYLE_SOLID
|
|
||||||
#property indicator_width2 1
|
|
||||||
|
|
||||||
input double dcpercent = 50; // Decision Candle Body/range Percentage
|
|
||||||
|
|
||||||
double buff0[];
|
|
||||||
double buff1[];
|
|
||||||
double buff2[];
|
|
||||||
double buff3[];
|
|
||||||
|
|
||||||
double buff4[];
|
|
||||||
double buff5[];
|
|
||||||
double buff6[];
|
|
||||||
double buff7[];
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Custom indicator initialization function |
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
int OnInit()
|
|
||||||
{
|
|
||||||
//--- indicator buffers mapping
|
|
||||||
|
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, "DCandles");
|
|
||||||
|
|
||||||
SetIndexBuffer(0, buff0, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(1, buff1, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(2, buff2, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(3, buff3, INDICATOR_DATA);
|
|
||||||
|
|
||||||
SetIndexBuffer(4, buff4, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(5, buff5, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(6, buff6, INDICATOR_DATA);
|
|
||||||
SetIndexBuffer(7, buff7, INDICATOR_DATA);
|
|
||||||
|
|
||||||
for(int in = 0; in < 8; in++)PlotIndexSetInteger(in, PLOT_SHOW_DATA, false);
|
|
||||||
|
|
||||||
//---
|
|
||||||
return(INIT_SUCCEEDED);
|
|
||||||
}
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
//| Custom indicator iteration function |
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
int OnCalculate(const int rates_total,
|
|
||||||
const int prev_calculated,
|
|
||||||
const datetime &time[],
|
|
||||||
const double &open[],
|
|
||||||
const double &high[],
|
|
||||||
const double &low[],
|
|
||||||
const double &close[],
|
|
||||||
const long &tick_volume[],
|
|
||||||
const long &volume[],
|
|
||||||
const int &spread[])
|
|
||||||
{
|
|
||||||
//---
|
|
||||||
int start = prev_calculated;
|
|
||||||
|
|
||||||
if(start >= rates_total)start = rates_total - 1;
|
|
||||||
if(start < 0)start = 0;
|
|
||||||
|
|
||||||
for(int i = start; i < rates_total; i++)
|
|
||||||
{
|
|
||||||
buff0[i] = EMPTY_VALUE;
|
|
||||||
buff1[i] = EMPTY_VALUE;
|
|
||||||
buff2[i] = EMPTY_VALUE;
|
|
||||||
buff3[i] = EMPTY_VALUE;
|
|
||||||
buff4[i] = EMPTY_VALUE;
|
|
||||||
buff5[i] = EMPTY_VALUE;
|
|
||||||
buff6[i] = EMPTY_VALUE;
|
|
||||||
buff7[i] = EMPTY_VALUE;
|
|
||||||
|
|
||||||
bool DC = (high[i] - low[i]) * dcpercent / 100 <= fabs(close[i] - open[i]);
|
|
||||||
bool up = DC && close[i] > open[i];
|
|
||||||
bool dn = DC && close[i] < open[i];
|
|
||||||
|
|
||||||
if(up)
|
|
||||||
{
|
|
||||||
buff0[i] = open[i];
|
|
||||||
buff1[i] = high[i];
|
|
||||||
buff2[i] = low[i];
|
|
||||||
buff3[i] = close[i];
|
|
||||||
}
|
|
||||||
if(dn)
|
|
||||||
{
|
|
||||||
buff4[i] = open[i];
|
|
||||||
buff5[i] = high[i];
|
|
||||||
buff6[i] = low[i];
|
|
||||||
buff7[i] = close[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- return value of prev_calculated for next call
|
|
||||||
return(rates_total);
|
|
||||||
}
|
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user