Files
xMQL5/BKPS/14030505/Documents/BKP/Old/Strategies/x-saherelm.xtest.strategy.mq5
T

1047 lines
23 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XSCXTestStrategy
// Description: provides all based classes for use ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Imports ...
//
// Import(s) Helper(s) ...
#include "../Helpers/x-saherelm.xhk.helper.mq5"
#include "../Helpers/x-saherelm.xmc.helper.mq5"
#include "../Helpers/x-saherelm.xtd.helper.mq5"
#include "../Helpers/x-saherelm.xpv.helper.mq5"
#include "../Helpers/x-saherelm.xche.helper.mq5"
#include "../Helpers/x-saherelm.xvlm.helper.mq5"
#include "../Helpers/x-saherelm.xosc.helper.mq5"
#include "../Helpers/x-saherelm.xchma.helper.mq5"
//
// Base Classes ...
#include "../Classes/x-saherelm.xstrategy.class.mq5"
//
// Specific Identifier(s) of XStrategy ...
string XTestStartegyToken = "XTest";
//
// XStrategy Class Implementation(s) ...
class XSCXTestStrategy : public XSCBaseStrategy
{
//
// Public ...
public:
//
// Constructor(s) ...
void XSCXTestStrategy(
//
// Base ...
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period, // Trading TimeFrame
double _volume, // Voluem
double _r2r, // Risk/Reward Ratio
int _slippage, // Trader Slippage
long _magicNumber, // Trader Magic Number
bool _ignoreSignalExecution = false, // Ignore Signal Execution
bool _useTPSLAsPoint = false, // Converts TP and SL by Provided Points
bool _forceMaxTPSLAsPoint = false, // Force Max TP and SL by Provided Points
double _tpPoint = 0, // TP As Point
double _slPoint = 0, // SL As Point
bool _ignoreTP = false, // TP set to 0
bool _ignoreSL = false, // SL set to 0
bool _allowLong = true,
bool _allowShort = true,
int _maxAllowedLongs = 0,
int _maxAllowedShorts = 0 //
) : XSCBaseStrategy(_symbol,
_period,
_volume,
_r2r,
_slippage,
_magicNumber,
_ignoreSignalExecution,
_useTPSLAsPoint,
_forceMaxTPSLAsPoint,
_tpPoint,
_slPoint,
_ignoreTP,
_ignoreSL,
_allowLong,
_allowShort,
_maxAllowedLongs,
_maxAllowedShorts //
)
{
//
SetAlertPrefix(GetTag());
//
PrepareRequirements();
}
//
// Deconstructor ...
void ~XSCXTestStrategy()
{
//
delete mcHelper;
delete pvHelper;
delete tdHelper;
delete hkHelper;
delete oscHelper;
delete vlmHelper;
delete cheHelper;
delete chmaHelper;
}
//
// Getter / Setter (s) ...
//
// Overrides ...
//
// Customize Strategy Identifier ...
string GetTag() override
{
return XTestStartegyToken;
}
//
// Check Conditions For Signal ...
bool HasSignal(
XSignal &signal //
) override
{
//
bool result = false;
//
signal.Clean();
//
double sl = 0;
double tp = 0;
double risk = 0;
double entry = 0;
double reward = 0;
double r2r = R2R();
ENUM_POSITION_TYPE type = POSITION_TYPE_BUY;
ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET;
//
bool hasLong = false;
bool hasShort = false;
//
// Retrieve Conditions ...
//
// XMC ...
XMCConditions mcConditions;
result = mcHelper.GetConditions(mcConditions);
if (!result)
{
return result;
}
//
// XPV ...
XPVConditions pvConditions;
result = pvHelper.GetConditions(pvConditions);
if (!result)
{
return result;
}
//
// XTD ...
XTDConditions tdConditions;
result = tdHelper.GetConditions(tdConditions);
if (!result)
{
return result;
}
//
// XHK ...
XHKConditions hkConditions;
result = hkHelper.GetConditions(hkConditions);
if (!result)
{
return result;
}
//
// XVLM ...
XVLMConditions vlmConditions;
result = vlmHelper.GetConditions(vlmConditions);
if (!result)
{
return result;
}
//
// XCHMA ...
XCHMAConditions chmaConditions;
result = chmaHelper.GetConditions(chmaConditions);
if (!result)
{
return result;
}
//
// XCHE ...
XCHEConditions cheConditions;
result = cheHelper.GetConditions(cheConditions);
if (!result)
{
return result;
}
//
double atr = oscHelper.GetATR(1);
//
double rsis[];
ArraySetAsSeries(rsis, true);
oscHelper.CopyRSI(
0,
3,
rsis //
);
//
// Start Calculations ...
//
double points = GetPoints(mSymbol);
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
// Retrieve Bars ...
//
// Last Bar ...
XOHCL cBar;
result = cBar.Init(
mSymbol,
mPeriod,
cIndex //
);
if (!result)
{
return result;
}
//
// Previous Bar of Last Bar ...
XOHCL pBar;
result = cBar.GetPreviousBar(pBar);
if (!result)
{
return result;
}
//
// Last Swing Low Bar ...
XOHCL lastSwingLow;
result = cBar.FindLastSwingLow(lastSwingLow);
if (!result)
{
return result;
}
//
// Last Swing High Bar ...
XOHCL lastSwingHigh;
result = cBar.FindLastSwingHigh(lastSwingHigh);
if (!result)
{
return result;
}
//
// Retrieve Highest High(s) and Lowest Low(s) ...
//
// Highest High ...
double hh5 = cBar.FindHighest(
5,
MODE_HIGH //
);
//
// Lowest Low ...
double ll5 = cBar.FindLowest(
5,
MODE_LOW //
);
//
// Check Support Exists or Find ...
bool hasSupport = support.IsValid();
if (!hasSupport)
{
//
hasSupport =
//
cBar.HasSupport(support)
//
;
//
if (hasSupport)
{
//
DrawSupport(
support,
ChartID() //
);
//
supportTime = cBar.time;
}
}
//
// Check Resistance Exists or Find ...
bool hasResistance = resistance.IsValid();
if (!hasResistance)
{
//
hasResistance =
//
cBar.HasResistance(resistance)
//
;
//
if (hasResistance)
{
//
DrawResistance(
resistance,
ChartID() //
);
//
resistanceTime = cBar.time;
}
}
//
// For Trend ...
XOHCL tmpHSW[];
XOHCL tmpLSW[];
//
// Detect Bullish/Bearish Patterns and Trends ...
//
// Bullish ...
bool hasBullishPattern = HasBullishPattern(cBar);
bool isBullishTrend = cBar.HasBullishTrend(
tmpHSW,
tmpLSW,
true //
);
//
// Bearish ...
bool hasBearishPattern = HasBearishPattern(cBar);
bool isBearishTrend = cBar.HasBearishTrend(
tmpHSW,
tmpLSW,
false //
);
//
// Detect XCHMA based Conditions ...
//
// Bullish ...
//
bool isHCrossedOver =
//
(
//
chmaConditions.isHFastCrossedOverSlow &&
chmaConditions.isLFastOverSlow &&
chmaConditions.isMFastOverSlow &&
chmaConditions.isSFastOverSlow
//
)
//
;
//
bool isLCrossedOver =
//
(
//
chmaConditions.isLFastCrossedOverSlow &&
chmaConditions.isHFastOverSlow &&
chmaConditions.isMFastOverSlow &&
chmaConditions.isSFastOverSlow
//
)
//
;
//
bool isMCrossedOver =
//
(
//
chmaConditions.isMFastCrossedOverSlow &&
chmaConditions.isHFastOverSlow &&
chmaConditions.isLFastOverSlow &&
chmaConditions.isSFastOverSlow
//
)
//
;
//
bool isSCrossedOver =
//
(
//
chmaConditions.isSFastCrossedOverSlow &&
chmaConditions.isHFastOverSlow &&
chmaConditions.isLFastOverSlow &&
chmaConditions.isMFastOverSlow
//
)
//
;
//
// Bullish XCHMA Conditions ...
bool isCrossedOver =
//
isHCrossedOver
//
||
//
isLCrossedOver
//
||
//
isMCrossedOver
//
||
//
isSCrossedOver
//
;
//
// Bearish ...
//
bool isHCrossedUnder =
//
(
//
chmaConditions.isHFastCrossedUnderSlow &&
chmaConditions.isLFastUnderSlow &&
chmaConditions.isMFastUnderSlow &&
chmaConditions.isSFastUnderSlow
//
)
//
;
//
bool isLCrossedUnder =
//
(
//
chmaConditions.isLFastCrossedUnderSlow &&
chmaConditions.isHFastUnderSlow &&
chmaConditions.isMFastUnderSlow &&
chmaConditions.isSFastUnderSlow
//
)
//
;
//
bool isMCrossedUnder =
//
(
//
chmaConditions.isMFastCrossedUnderSlow &&
chmaConditions.isHFastUnderSlow &&
chmaConditions.isLFastUnderSlow &&
chmaConditions.isSFastUnderSlow
//
)
//
;
//
bool isSCrossedUnder =
//
(
//
chmaConditions.isSFastCrossedUnderSlow &&
chmaConditions.isHFastUnderSlow &&
chmaConditions.isLFastUnderSlow &&
chmaConditions.isMFastUnderSlow
//
)
//
;
//
// Bullish XCHMA Conditions ...
bool isCrossedUnder =
//
isHCrossedUnder
//
||
//
isLCrossedUnder
//
||
//
isMCrossedUnder
//
||
//
isSCrossedUnder
//
;
//
// XCHE Conditions ...
//
bool isCHESwitchedToStrongBullish = cheConditions.isStrongSwitchedToBullish;
//
bool isCHESwitchedToStrongBearish = cheConditions.isStrongSwitchedToBearish;
//
// Signalling ...
// Looking for Signals Based on Conditions ...
//
// Long ...
if (isCrossedOver)
{
//
hasLong =
//
(isBullishTrend ||
hasBullishPattern)
//
&&
//
chmaConditions.isFastOverSlow &&
chmaConditions.isCLoseOverFast &&
chmaConditions.isFastSupport &&
chmaConditions.isSlowSupport &&
cheConditions.isStrongBullish
//
;
//
if (hasLong)
{
//
type = POSITION_TYPE_BUY;
sl = cheConditions.long2[1];
}
}
//
// Long ...
if (isCHESwitchedToStrongBullish)
{
//
hasLong =
//
// isBullishTrend &&
// cBar.IsBullish() &&
hasBullishPattern &&
hkConditions.isSMHKBullish &&
chmaConditions.isFastOverSlow
//
;
//
if (hasLong)
{
//
type = POSITION_TYPE_BUY;
sl = cheConditions.long1[1];
}
}
//
// Short ...
if (isCrossedUnder)
{
//
hasShort =
//
(isBearishTrend ||
hasBearishPattern)
//
&&
//
chmaConditions.isFastUnderSlow &&
chmaConditions.isCloseUnderFast &&
chmaConditions.isFastResistance &&
chmaConditions.isSlowResistance &&
cheConditions.isStrongBearish
//
;
//
if (hasShort)
{
//
type = POSITION_TYPE_SELL;
sl = cheConditions.short2[1];
}
}
//
// Short ...
if (isCHESwitchedToStrongBearish)
{
//
hasShort =
//
// isBearishTrend &&
// cBar.IsBearish() &&
hasBearishPattern &&
hkConditions.isSMHKBearish &&
chmaConditions.isFastUnderSlow
//
;
//
if (hasLong)
{
//
type = POSITION_TYPE_BUY;
sl = cheConditions.long1[1];
}
}
//
// Preparing Signals for Execution ...
//
result = hasLong || hasShort;
if (!result)
{
//
CheckSupportValidation();
CheckResistanceValidation();
return result;
}
//
if (hasSupport)
{
//
supportTime = NULL;
isSupportTested = false;
RemoveSupport(support);
support.Clean();
}
//
if (hasResistance)
{
//
resistanceTime = NULL;
isResistanceTested = false;
RemoveResistance(resistance);
resistance.Clean();
}
//
// Retrieve Entry Price based on Position Type ...
entry = GetEntry(mSymbol, type);
//
// Calculate TP and SL ...
// based on Strategy ...
CalculateTPSLByPrice(
sl,
tp,
type,
entry,
r2r //
);
//
// Preparing Signal ...
result = signal.Prepare(
mSymbol,
GetTag(),
mPeriod,
type,
mode,
entry,
mVolume,
sl,
tp //
);
//
return result;
}
//
// Protected ...
protected:
//
// Props ...
//
XOHCL support;
XOHCL resistance;
//
// XMC ...
XMCInputs mcInputs;
XSCXMCHelper *mcHelper;
//
// XTD ...
XTDInputs tdInputs;
XSCXTDHelper *tdHelper;
//
// XPV ...
XPVInputs pvInputs;
XSCXPVHelper *pvHelper;
//
// XOSC ...
XOSCInputs oscInputs;
XSCXOSCHelper *oscHelper;
//
// XVLM ...
XVLMInputs vlmInputs;
XSCXVLMHelper *vlmHelper;
//
// XHK ...
XHKInputs hkInputs;
XSCXHKHelper *hkHelper;
//
// XCHMA ...
XCHMAInputs chmaInputs;
XSCXCHMAHelper *chmaHelper;
//
// XCHE ...
XCHEInputs cheInputs;
XSCXCHEHelper *cheHelper;
//
// Private ...
private:
//
// Props ...
//
void PrepareRequirements()
{
//
support.Clean();
supportTime = NULL;
isSupportTested = false;
//
resistance.Clean();
resistanceTime = NULL;
isResistanceTested = false;
//
// Prepare XTD ...
tdInputs.Default();
//
tdHelper = new XSCXTDHelper();
tdHelper.Init(
mSymbol,
mPeriod,
tdInputs //
);
//
// Preparing XHK ...
hkInputs.Default();
hkInputs.smoothingLength = 14;
//
hkInputs.drawHikenAshi = false;
hkInputs.drawSmoothedHikenAshi = true;
//
hkHelper = new XSCXHKHelper();
hkHelper.Init(
mSymbol,
mPeriod,
hkInputs //
);
//
// Preparing XMC ...
mcInputs.Default();
//
mcInputs.showSar = false;
mcInputs.showFastMa = false;
mcInputs.showSlowMa = false;
mcInputs.showVerifierMa = false;
//
mcHelper = new XSCXMCHelper();
mcHelper.Init(
mSymbol,
mPeriod,
mcInputs //
);
//
// Preparing XPV ...
pvInputs.Default();
//
pvInputs.showPeaksAndVales = false;
pvInputs.showLevels = false;
pvInputs.showConsolidations = false;
pvInputs.showFibo1Levels = false;
pvInputs.showFibo2Levels = false;
pvInputs.showFibo3Levels = false;
pvInputs.showFibo4Levels = false;
pvInputs.showFibo5Levels = false;
//
pvHelper = new XSCXPVHelper();
pvHelper.Init(
mSymbol,
mPeriod,
pvInputs //
);
//
// Preparing XOSC ...
oscInputs.Default();
oscHelper = new XSCXOSCHelper();
oscHelper.Init(
mSymbol,
mPeriod,
oscInputs //
);
//
// Preparing XVLM ...
vlmInputs.Default();
vlmHelper = new XSCXVLMHelper();
vlmHelper.Init(
mSymbol,
mPeriod,
vlmInputs //
);
//
chmaInputs.Default();
//
chmaInputs.showFast = true;
chmaInputs.showSlow = true;
chmaInputs.showCCFast = false;
chmaInputs.showCCSlow = false;
chmaInputs.showSCFast = false;
chmaInputs.showSCSlow = false;
chmaInputs.showMCFast = false;
chmaInputs.showMCSlow = false;
chmaInputs.showLCFast = false;
chmaInputs.showLCSlow = false;
chmaInputs.showHCFast = false;
chmaInputs.showHCSlow = false;
//
chmaHelper = new XSCXCHMAHelper();
chmaHelper.Init(
mSymbol,
mPeriod,
chmaInputs //
);
//
cheInputs.Default();
//
cheInputs.showLongExit1Line = true;
cheInputs.showShortExit1Line = true;
cheInputs.showLongExit2Line = true;
cheInputs.showShortExit2Line = true;
//
cheHelper = new XSCXCHEHelper();
cheHelper.Init(
mSymbol,
mPeriod,
cheInputs //
);
}
//
bool isSupportTested;
datetime supportTime;
void CheckSupportValidation()
{
//
if (!isSupportTested)
{
//
if (support.IsValid() && IsValid(supportTime))
{
//
int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(supportTime))) / PeriodSeconds(mPeriod));
if (age >= 50)
{
//
isSupportTested = false;
supportTime = NULL;
RemoveSupport(support);
support.Clean();
}
}
return;
}
//
XOHCL cBar;
bool isInited = cBar.Init(
mSymbol,
mPeriod,
1 //
);
if (!isInited)
{
return;
}
//
double points = GetPoints(mSymbol);
//
// Find Price Distance ...
double highDiff = MathAbs(cBar.high - support.high);
double lowDiff = MathAbs(cBar.low - support.high);
//
bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points;
if (!isBreaked)
{
return;
}
//
isSupportTested = false;
RemoveSupport(support);
supportTime = NULL;
support.Clean();
}
//
bool isResistanceTested;
datetime resistanceTime;
void CheckResistanceValidation()
{
//
if (!isResistanceTested)
{
//
if (resistance.IsValid() && IsValid(resistanceTime))
{
//
int age = (int)(((int)(TimeToSeconds(TimeCurrent()) - TimeToSeconds(resistanceTime))) / PeriodSeconds(mPeriod));
if (age >= 50)
{
//
isResistanceTested = false;
resistanceTime = NULL;
RemoveResistance(resistance);
resistance.Clean();
}
}
return;
}
//
XOHCL cBar;
bool isInited = cBar.Init(
mSymbol,
mPeriod,
1 //
);
if (!isInited)
{
return;
}
//
double points = GetPoints(mSymbol);
//
// Find Price Distance ...
double highDiff = MathAbs(cBar.high - resistance.low);
double lowDiff = MathAbs(cBar.low - resistance.low);
//
bool isBreaked = MathMax(highDiff, lowDiff) >= 100 * points;
if (!isBreaked)
{
return;
}
//
isResistanceTested = false;
RemoveResistance(resistance);
resistanceTime = NULL;
resistance.Clean();
}
};
//