Files
MQL5Data/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5
T
2025-01-10 18:48:58 +03:30

1171 lines
28 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XCX121SMCStrategy
// 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 ...
//
#include "../Classes/x-121.smc.base.strategy.class.mq5"
#include "../Classes/x-121.smc.market.cycle.helper.class.mq5"
//
#include "../Signals/x-121.smc.test.signal.lib.mq5"
#include "../Signals/x-121.smc.xobgpv.signal.lib.mq5"
#include "../Signals/x-121.smc.xobspv.signal.lib.mq5"
//
// Definitions ...
//
// XStrategy Class Implementation(s) ...
class XCX121SMCStrategy : public XCX121SMCBaseStrategy
{
//
// Public ...
public:
//
//
// Constructor(s) ...
void XCX121SMCStrategy(
//
// Base ...
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period // Trading TimeFrame
)
: XCX121SMCBaseStrategy(_symbol, _period)
{
InitialHelpers();
}
//
// Deconstructur ...
void ~XCX121SMCStrategy()
{
}
//
// Getter(s) / Setter(s) ...
/**
* Get Max Allowed Required POI(s) to Find ...
*
* @return ( int )
*/
int MaxAllowedRequiredPOIs()
{
return mMaxAllowedRequiredPOIs;
}
/**
* Set Max Allowed Required POI(s) to Find ...
*
* @param value: number of Required POIs
*/
void MaxAllowedRequiredPOIs(int value)
{
//
if (value < 10)
{
value = 10;
}
//
mMaxAllowedRequiredPOIs = value;
}
//
// Overrides Actions ...
/**
* Destroy All Class Implementations ...
*/
void Destroy() override
{
//
delete mTriggerCycleHelper;
delete mDecisionCycleHelper;
delete mAnalyseCycleHelper;
delete mVerificationCycleHelper;
delete mConsolidationCycleHelper;
delete mVisionCycleHelper;
}
/**
* Check Conditions For Signal ...
*/
bool HasSignal(
XSignal &signal,
X121SMCStrategyConditions &conditions //
) override
{
//
bool result = false;
//
signal.Clean();
conditions.Clean();
//
int pushers = 0;
//
double sl = 0;
double tp = 0;
double risk = 0;
double entry = 0;
double reward = 0;
double r2r = R2R();
string provider = "";
double volume = Volume();
ENUM_POSITION_TYPE type = POSITION_TYPE_BUY;
ENUM_X_ORDER_MODES mode = X_ORDER_MODE_MARKET;
//
bool hasLong = false;
bool hasShort = false;
//
double selectedSL = 0;
//
// Start Calculations ...
//
// Retrieve Common Data ...
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
datetime cTime = TimeCurrent();
//
// Required Value For SL/TP Calculations ...
double points = GetPoints(symbol);
double pip = GetPipPrice(symbol);
double pip2 = 2 * pip;
//
conditions.time = cTime;
conditions.symbol = symbol;
conditions.period = period;
//
// Check Spread for Signalling Conditions ...
bool isSpreadPassed = IsSpreadPass();
result = isSpreadPassed;
if (!result)
{
//
conditions.Clean();
return result;
}
//
// Detect Signal Conditions ...
bool hasSignal = false;
//
bool useTestSignal = false;
bool useXOBGPVSignal = false;
bool useXOBSPVSignal = true;
//
bool hasTestSignal = false;
bool hasXOBGPVSignal = false;
bool hasXOBSPVSignal = false;
//
// Test Signal ...
if (useTestSignal && !hasSignal)
{
//
hasTestSignal = DetectTestSignal(conditions);
hasSignal = hasTestSignal;
}
//
// XOBGPV Signal ...
if (useXOBGPVSignal && !hasSignal)
{
//
hasXOBGPVSignal = DetectXOBGPVSignal(conditions);
hasSignal = hasXOBGPVSignal;
}
//
// XOBSPV Signal ...
if (useXOBSPVSignal && !hasSignal)
{
//
hasXOBSPVSignal = DetectXOBSPVSignal(conditions);
hasSignal = hasXOBSPVSignal;
}
//
result = hasSignal;
if (!result)
{
//
conditions.Clean();
return result;
}
//
// Combine all Signals Conditions ...
selectedSL = conditions.sl;
provider = conditions.provider;
//
hasLong =
//
IsBullish(conditions.signalDir)
//
;
//
hasShort =
//
IsBearish(conditions.signalDir)
//
;
//
result = hasLong ||
hasShort;
//
// Rmove Signal Condition ...
if (result)
{
//
// TYPE ...
type =
hasLong
? POSITION_TYPE_BUY
: POSITION_TYPE_SELL;
ENUM_X_POSITION_TYPES xType =
hasLong
? X_POSITION_TYPE_LONG
: X_POSITION_TYPE_SHORT;
//
entry = GetEntry(
conditions.symbol,
type //
);
//
double slAddition = 0;
sl =
hasLong
? selectedSL - slAddition
: selectedSL + slAddition;
//
// RISK Reward ...
risk =
hasLong
? entry - sl
: sl - entry;
reward = risk * r2r;
tp =
hasLong
? entry + reward
: entry - reward;
//
signal.sl = sl;
signal.tp = tp;
signal.type = type;
signal.mode = mode;
signal.entry = entry;
signal.volume = volume;
signal.symbol = symbol;
signal.period = period;
signal.pushers = pushers;
signal.provider = provider;
signal.time = TimeCurrent();
signal.target = conditions.target;
//
bool canReplaceTarget =
signal.target > 0 &&
(hasLong
? signal.target > signal.tp
: signal.target < signal.tp);
if (canReplaceTarget)
{
//
signal.tp = conditions.target;
signal.target = tp;
}
//
result = PrepareSignal(signal);
//
if (result)
{
//
// TODO: Remove this ...
// XCSignalObject *iObj;
// mPOIDrawer.DrawSignal(signal, iObj);
// Print("");
}
//
hasLong = false;
hasShort = false;
}
//
return result;
}
/**
* Customize Strategy Identifier ...
*/
string GetTag() override
{
return X121SMCStrategyToken;
}
//
// Protected ...
protected:
//
//
// Props ...
//
XC121SMCCycleHelper *mTriggerCycleHelper;
XC121SMCCycleHelper *mDecisionCycleHelper;
XC121SMCCycleHelper *mAnalyseCycleHelper;
XC121SMCCycleHelper *mVerificationCycleHelper;
XC121SMCCycleHelper *mConsolidationCycleHelper;
XC121SMCCycleHelper *mVisionCycleHelper;
//
// Actions ...
//
// Signalling Functions ...
//
// Test Signal ...
X121SMCStrategyTestSignalConditions mTestConditions;
bool DetectTestSignal(
X121SMCStrategyConditions &conditions //
)
{
//
bool result = false;
//
bool isBullish = false;
bool isBearish = false;
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
datetime cTime = TimeCurrent();
//
ENUM_X_121_SMC_PROVIDERS provider = X_121_SMC_PROVIDER_XTEST;
string providerStr = ToString(provider);
//
// Detect Setup Conditions ...
datetime setupTime = mTestConditions.setupTime;
bool isSetuped = mTestConditions.IsSetuped();
ENUM_X_DIRECTION setupDir = mTestConditions.dir;
if (!isSetuped)
{
//
// Detect Signal Setup ...
isSetuped = DetectX121SMCTestSiganlSetup(
symbol,
period,
mTestConditions,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
setupDir = mTestConditions.dir;
setupTime = mTestConditions.setupTime;
}
//
result = isSetuped;
if (!result)
{
return result;
}
//
// Detect Trigger Conditions ...
bool canTrigger = mTestConditions.CanTrigger();
datetime triggerTime = mTestConditions.triggerTime;
if (!canTrigger)
{
//
// Detect Signal Trigger ...
canTrigger = DetectX121SMCTestSiganlTrigger(
mTestConditions,
symbol,
period,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
result = canTrigger;
if (!result)
{
return result;
}
//
triggerTime = mTestConditions.triggerTime;
}
//
// Check Signal Direction ...
//
isBullish = IsBullish(setupDir);
isBearish = IsBearish(setupDir);
result = isBullish ||
isBearish;
if (!result)
{
return result;
}
//
// Issues Signal on Conditions ...
conditions.signalDir = setupDir;
conditions.provider = providerStr;
conditions.sl = mTestConditions.sl;
conditions.target = mTestConditions.target;
//
mTestConditions.Clean();
//
return result;
}
//
// XOBGPV Signal ...
bool DetectXOBGPVSignal(
X121SMCStrategyConditions &conditions //
)
{
//
bool result = false;
//
bool isBullish = false;
bool isBearish = false;
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
XOHCL zBar;
result = zBar.Init(
symbol,
period,
zIndex //
);
if (!result)
{
return result;
}
//
XOHCL cBar;
result = cBar.Init(
symbol,
period,
cIndex //
);
if (!result)
{
return result;
}
//
XOHCL pBar;
result = pBar.Init(
symbol,
period,
pIndex //
);
if (!result)
{
return result;
}
//
double cLL = cBar.FindLowest(7, MODE_LOW);
double cHH = cBar.FindHighest(7, MODE_HIGH);
//
datetime cTime = TimeCurrent();
//
ENUM_X_121_SMC_PROVIDERS provider = X_121_SMC_PROVIDER_XOBGPV;
string providerStr = ToString(provider);
//
// Detect Setup Conditions ...
datetime setupTime = mXOBGPVConditions.setupTime;
bool isSetuped = mXOBGPVConditions.IsSetuped();
ENUM_X_DIRECTION setupDir = mXOBGPVConditions.dir;
if (!isSetuped)
{
//
// Detect Signal Setup ...
isSetuped = DetectX121SMCXOBGPVSiganlSetup(
symbol,
period,
mXOBGPVConditions,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
setupDir = mXOBGPVConditions.dir;
setupTime = mXOBGPVConditions.setupTime;
//
if (isSetuped)
{
//
int items = AddX121SMCXOBGPVConditionsIfNotExists(
mXOBGPVConditions,
mXOBGPVConditionsCollection //
);
//
mXOBGPVConditions.Clean();
}
}
//
int conditionsCount = ArraySize(mXOBGPVConditionsCollection);
result = IsValidSize(conditionsCount);
if (!result)
{
return result;
}
//
// Looking For Conditions ...
int selectedIndex = 0;
X121SMCStrategyXOBGPVSignalConditions mTmpConditions[];
Copy(
mXOBGPVConditionsCollection,
mTmpConditions //
);
X121SMCStrategyXOBGPVSignalConditions mConditions;
for (int i = 0; i < conditionsCount; i++)
{
//
// Update Conditions ...
mTmpConditions[i].UpdateOrderBlocks();
//
// Retrieve Required Data ...
ENUM_X_DIRECTION iDir = mTmpConditions[i].dir;
bool isIBullish = IsBullish(iDir);
double iUpper = 0;
double iLower = 0;
bool iHasBoundary = mTmpConditions[i].fvgOB.GetBoundary(
iUpper,
iLower //
);
if (!iHasBoundary)
{
continue;
}
//
// Draw Conditions ...
//
XFVGOrderBlock iFVGOB = mTmpConditions[i].fvgOB;
XCBaseObject *drawnObjects[];
mPOIDrawer.DrawFVGOrderBlock(
iFVGOB,
drawnObjects //
);
AddObjectsIfNotExists(drawnObjects);
//
// Check Broken Order Blocks and Remove them ...
bool isBreaked =
isBullish
? (cBar.high < iLower ||
pBar.high < iLower)
: (cBar.low > iUpper ||
cBar.low > iUpper);
if (isBreaked)
{
//
ArrayRemove(
mXOBGPVConditionsCollection,
i,
1 //
);
//
continue;
}
//
// Check Triggers ...
bool canTrigger = mTmpConditions[i].CanTrigger();
datetime triggerTime = mTmpConditions[i].triggerTime;
if (!canTrigger)
{
//
// Detect Signal Trigger ...
canTrigger = DetectX121SMCXOBGPVSiganlTrigger(
mTmpConditions[i],
symbol,
period,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
result = canTrigger;
if (!result)
{
continue;
}
//
triggerTime = mTmpConditions[i].triggerTime;
}
//
if (canTrigger)
{
//
selectedIndex = i;
mConditions = mTmpConditions[i];
break;
}
}
//
result = IsValid(mConditions.setupTime) &&
HasDirection(mConditions.dir);
if (!result)
{
return result;
}
//
// Remove Selected Index from Main Collection ...
ArrayRemove(
mXOBGPVConditionsCollection,
selectedIndex,
1 //
);
//
// Check Signal Direction ...
isBullish = IsBullish(mConditions.dir);
isBearish = IsBearish(mConditions.dir);
result = isBullish ||
isBearish;
if (!result)
{
return result;
}
//
// Issues Signal on Conditions ...
conditions.sl = mConditions.sl;
conditions.provider = providerStr;
conditions.signalDir = mConditions.dir;
conditions.target = mConditions.target;
//
return result;
}
//
// XOBSPV Signal ...
bool DetectXOBSPVSignal(
X121SMCStrategyConditions &conditions //
)
{
//
bool result = false;
//
bool isBullish = false;
bool isBearish = false;
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
XOHCL zBar;
result = zBar.Init(
symbol,
period,
zIndex //
);
if (!result)
{
return result;
}
//
XOHCL cBar;
result = cBar.Init(
symbol,
period,
cIndex //
);
if (!result)
{
return result;
}
//
XOHCL pBar;
result = pBar.Init(
symbol,
period,
pIndex //
);
if (!result)
{
return result;
}
//
double cLL = cBar.FindLowest(7, MODE_LOW);
double cHH = cBar.FindHighest(7, MODE_HIGH);
//
datetime cTime = TimeCurrent();
//
ENUM_X_121_SMC_PROVIDERS provider = X_121_SMC_PROVIDER_XOBSPV;
string providerStr = ToString(provider);
//
// Detect Setup Conditions ...
datetime setupTime = mXOBSPVConditions.setupTime;
bool isSetuped = mXOBSPVConditions.IsSetuped();
ENUM_X_DIRECTION setupDir = mXOBSPVConditions.dir;
if (!isSetuped)
{
//
// Detect Signal Setup ...
isSetuped = DetectX121SMCXOBSPVSiganlSetup(
symbol,
period,
mXOBSPVConditions,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
setupDir = mXOBSPVConditions.dir;
setupTime = mXOBSPVConditions.setupTime;
//
if (isSetuped)
{
//
int items = AddX121SMCXOBSPVConditionsIfNotExists(
mXOBSPVConditions,
mXOBSPVConditionsCollection //
);
//
mXOBSPVConditions.Clean();
}
}
//
int conditionsCount = ArraySize(mXOBSPVConditionsCollection);
result = IsValidSize(conditionsCount);
if (!result)
{
return result;
}
//
// Looking For Conditions ...
int selectedIndex = 0;
X121SMCStrategyXOBSPVSignalConditions mTmpConditions[];
Copy(
mXOBSPVConditionsCollection,
mTmpConditions //
);
X121SMCStrategyXOBSPVSignalConditions mConditions;
for (int i = 0; i < conditionsCount; i++)
{
//
// Update Conditions ...
// mTmpConditions[i].UpdateOrderBlocks();
//
// Retrieve Required Data ...
// ENUM_X_DIRECTION iDir = mTmpConditions[i].dir;
// bool isIBullish = IsBullish(iDir);
// double iUpper = 0;
// double iLower = 0;
// bool iHasBoundary = mTmpConditions[i].fvgOB.GetBoundary(
// iUpper,
// iLower //
// );
// if (!iHasBoundary)
// {
// continue;
// }
//
// Draw Conditions ...
//
// XFVGOrderBlock iFVGOB = mTmpConditions[i].fvgOB;
// XCBaseObject *drawnObjects[];
// mPOIDrawer.DrawFVGOrderBlock(
// iFVGOB,
// drawnObjects //
// );
// AddObjectsIfNotExists(drawnObjects);
//
// Check Broken Order Blocks and Remove them ...
// bool isBreaked =
// isBullish
// ? (cBar.high < iLower ||
// pBar.high < iLower)
// : (cBar.low > iUpper ||
// cBar.low > iUpper);
// if (isBreaked)
// {
// //
// ArrayRemove(
// mXOBSPVConditionsCollection,
// i,
// 1 //
// );
// //
// continue;
// }
//
// Check Triggers ...
bool canTrigger = mTmpConditions[i].CanTrigger();
datetime triggerTime = mTmpConditions[i].triggerTime;
if (!canTrigger)
{
//
// Detect Signal Trigger ...
canTrigger = DetectX121SMCXOBSPVSiganlTrigger(
mTmpConditions[i],
symbol,
period,
mTriggerCycleHelper,
mDecisionCycleHelper,
mAnalyseCycleHelper,
mVerificationCycleHelper,
mConsolidationCycleHelper,
mVisionCycleHelper //
);
//
result = canTrigger;
if (!result)
{
continue;
}
//
triggerTime = mTmpConditions[i].triggerTime;
}
//
if (canTrigger)
{
//
selectedIndex = i;
mConditions = mTmpConditions[i];
break;
}
}
//
result = IsValid(mConditions.setupTime) &&
HasDirection(mConditions.dir);
if (!result)
{
return result;
}
//
// Remove Selected Index from Main Collection ...
ArrayRemove(
mXOBSPVConditionsCollection,
selectedIndex,
1 //
);
//
// Check Signal Direction ...
isBullish = IsBullish(mConditions.dir);
isBearish = IsBearish(mConditions.dir);
result = isBullish ||
isBearish;
if (!result)
{
return result;
}
//
// Issues Signal on Conditions ...
conditions.sl = mConditions.sl;
conditions.provider = providerStr;
conditions.signalDir = mConditions.dir;
conditions.target = mConditions.target;
//
return result;
}
//
// Private ...
private:
//
// Props ...
//
int mMaxAllowedRequiredPOIs; // Max Allowed Required POI(s) to Find ...
/**
* Initial Required Indicators Helper ...
*/
void InitialHelpers()
{
//
string symbol = GetSymbol();
ENUM_TIMEFRAMES period = GetPeriod();
//
if (mMaxAllowedRequiredPOIs <= 0)
{
mMaxAllowedRequiredPOIs = 50;
}
//
// Short ...
ENUM_X_PERIOD_METHOD scMethod = X_PERIOD_AUTO; // How to Find Period
ENUM_TIMEFRAMES scPeriod = NULL; // Time Period
//
// Medium ...
ENUM_X_PERIOD_METHOD mcMethod = X_PERIOD_AUTO; // How to Find Period
ENUM_TIMEFRAMES mcPeriod = NULL; // Time Period
//
// Long ...
ENUM_X_PERIOD_METHOD lcMethod = X_PERIOD_AUTO; // How to Find Period
ENUM_TIMEFRAMES lcPeriod = NULL; // Time Period
//
// Hind ...
ENUM_X_PERIOD_METHOD hcMethod = X_PERIOD_AUTO; // How to Find Period
ENUM_TIMEFRAMES hcPeriod = NULL; // Time Period
//
// XPV ...
X121Inputs x121Inputs;
//
x121Inputs.Default();
//
x121Inputs.showRSI = true;
//
bool isInited = false;
//
// XPV ...
//
x121Inputs.xpvInputs.scMethod = scMethod;
x121Inputs.xpvInputs.scPeriod = scPeriod;
x121Inputs.xpvInputs.mcMethod = mcMethod;
x121Inputs.xpvInputs.mcPeriod = mcPeriod;
x121Inputs.xpvInputs.lcMethod = lcMethod;
x121Inputs.xpvInputs.lcPeriod = lcPeriod;
x121Inputs.xpvInputs.hcMethod = hcMethod;
x121Inputs.xpvInputs.hcPeriod = hcPeriod;
//
x121Inputs.xpvInputs.showSar = true;
x121Inputs.xpvInputs.showPeaks = true;
x121Inputs.xpvInputs.showVales = true;
//
// XATR ...
x121Inputs.xatrInputs.showATRUpper = true;
x121Inputs.xatrInputs.showATRLower = true;
//
// XSTR ...
x121Inputs.xstrInputs.showStr = true;
x121Inputs.xstrInputs.showStrUpper = true;
x121Inputs.xstrInputs.showStrLower = true;
//
// XDON ...
x121Inputs.xdonInputs.showOpen = false;
x121Inputs.xdonInputs.showClose = false;
x121Inputs.xdonInputs.showLow = false;
x121Inputs.xdonInputs.showHigh = false;
//
// XVWAP ...
x121Inputs.xvwapInputs.showVWapFast = true;
x121Inputs.xvwapInputs.showVWapMedium = true;
x121Inputs.xvwapInputs.showVWapSlow = true;
//
// Initialize Cycle Helpers ...
//
int requiredPOIS = MaxAllowedRequiredPOIs();
//
mTriggerCycleHelper = new XC121SMCCycleHelper();
isInited = mTriggerCycleHelper.Init(
symbol,
PERIOD_M1,
x121Inputs,
requiredPOIS //
);
//
mDecisionCycleHelper = new XC121SMCCycleHelper();
isInited = mDecisionCycleHelper.Init(
symbol,
PERIOD_M5,
x121Inputs,
requiredPOIS //
);
//
mAnalyseCycleHelper = new XC121SMCCycleHelper();
isInited = mAnalyseCycleHelper.Init(
symbol,
PERIOD_M15,
x121Inputs,
requiredPOIS //
);
//
mVerificationCycleHelper = new XC121SMCCycleHelper();
isInited = mVerificationCycleHelper.Init(
symbol,
PERIOD_M30,
x121Inputs,
requiredPOIS //
);
//
mConsolidationCycleHelper = new XC121SMCCycleHelper();
isInited = mConsolidationCycleHelper.Init(
symbol,
PERIOD_H1,
x121Inputs,
requiredPOIS //
);
//
mVisionCycleHelper = new XC121SMCCycleHelper();
isInited = mVisionCycleHelper.Init(
symbol,
PERIOD_H4,
x121Inputs,
requiredPOIS //
);
}
//
};
//