766 lines
19 KiB
Plaintext
766 lines
19 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.xtwpv.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()
|
|
{
|
|
}
|
|
|
|
//
|
|
// 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 useXTWPVSignal = true;
|
|
|
|
//
|
|
bool hasTestSignal = false;
|
|
bool hasXTWPVSignal = false;
|
|
|
|
//
|
|
// Test Signal ...
|
|
if (useTestSignal && !hasSignal)
|
|
{
|
|
//
|
|
hasTestSignal = DetectTestSignal(conditions);
|
|
hasSignal = hasTestSignal;
|
|
}
|
|
|
|
//
|
|
// XTWPV Signal ...
|
|
if (useXTWPVSignal && !hasSignal)
|
|
{
|
|
//
|
|
hasXTWPVSignal = DetectXTWPVSignal(conditions);
|
|
hasSignal = hasXTWPVSignal;
|
|
}
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
//
|
|
// XTWPV Signal ...
|
|
X121SMCStrategyXTWPVSignalConditions mXTWPVConditions;
|
|
bool DetectXTWPVSignal(
|
|
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_XTWPV;
|
|
string providerStr = ToString(provider);
|
|
|
|
//
|
|
// Detect Setup Conditions ...
|
|
datetime setupTime = mXTWPVConditions.setupTime;
|
|
bool isSetuped = mXTWPVConditions.IsSetuped();
|
|
ENUM_X_DIRECTION setupDir = mXTWPVConditions.dir;
|
|
if (!isSetuped)
|
|
{
|
|
//
|
|
// Detect Signal Setup ...
|
|
isSetuped = DetectX121SMCXTWPVSiganlSetup(
|
|
symbol,
|
|
period,
|
|
mXTWPVConditions,
|
|
mTriggerCycleHelper,
|
|
mDecisionCycleHelper,
|
|
mAnalyseCycleHelper,
|
|
mVerificationCycleHelper,
|
|
mConsolidationCycleHelper,
|
|
mVisionCycleHelper //
|
|
);
|
|
|
|
//
|
|
setupDir = mXTWPVConditions.dir;
|
|
setupTime = mXTWPVConditions.setupTime;
|
|
}
|
|
|
|
//
|
|
// Handle Required Drawings ...
|
|
bool hasPivot = mXTWPVConditions.HasPivot();
|
|
bool hasPriceInsideZone = mXTWPVConditions.HasPriceInsideZones();
|
|
bool hasConsolidationZone = mXTWPVConditions.HasConsolidationZone();
|
|
bool canDraw = hasPivot ||
|
|
hasPriceInsideZone ||
|
|
hasConsolidationZone;
|
|
if (canDraw)
|
|
{
|
|
//
|
|
ClearDraws();
|
|
|
|
//
|
|
// Draw Consolidation Zone ...
|
|
if (hasConsolidationZone)
|
|
{
|
|
//
|
|
XCConsolidationZoneObject *iObj;
|
|
bool isCreated = mPOIDrawer.CreateConsolidationZone(
|
|
mXTWPVConditions.consolidationZone,
|
|
iObj //
|
|
);
|
|
if (isCreated)
|
|
{
|
|
AddObjectIfNotExists(iObj);
|
|
}
|
|
}
|
|
|
|
//
|
|
// Draw Pivot Zone ...
|
|
if (hasPivot)
|
|
{
|
|
//
|
|
XCBaseObject *drawnObjects[];
|
|
int drawnObjectsCount = DrawXTWPVPivotZone(
|
|
mXTWPVConditions,
|
|
mPOIDrawer,
|
|
drawnObjects,
|
|
false, // Draw State ...
|
|
false, // Draw Ticks Zone ...
|
|
true, // Ignore SwingHighs ...
|
|
true, // Ignore SwingLows ...
|
|
true, // Ignore SupportZones ...
|
|
true, // Ignore ResistanceZones ...
|
|
true, // Ignore SupplyZones ...
|
|
true, // Ignore DemandZones ...
|
|
true, // Ignore BullishOrderBlocks ...
|
|
true, // Ignore BearishOrderBlocks ...
|
|
true, // Ignore BullishFairValueGaps ...
|
|
true, // Ignore BearishFairValueGaps ...
|
|
true, // Ignore BullishRejectionBars ...
|
|
true, // Ignore BearishRejectionBars ...
|
|
true, // Ignore BullishMomentumBars ...
|
|
true // Ignore BearishMomentumBars ...
|
|
);
|
|
AddObjectsIfNotExists(drawnObjects);
|
|
}
|
|
|
|
//
|
|
// Draw Price Inside Zones ...
|
|
if (hasPriceInsideZone)
|
|
{
|
|
//
|
|
XCBaseObject *drawnObjects[];
|
|
int drawnObjectsCount = DrawTWPVPriceInsideZones(
|
|
mXTWPVConditions,
|
|
mPOIDrawer,
|
|
drawnObjects //
|
|
);
|
|
AddObjectsIfNotExists(drawnObjects);
|
|
}
|
|
}
|
|
|
|
//
|
|
result = isSetuped;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Detect Trigger Conditions ...
|
|
bool canTrigger = mXTWPVConditions.CanTrigger();
|
|
datetime triggerTime = mXTWPVConditions.triggerTime;
|
|
if (!canTrigger)
|
|
{
|
|
//
|
|
// Detect Signal Trigger ...
|
|
canTrigger = DetectX121SMCXTWPVSiganlTrigger(
|
|
mXTWPVConditions,
|
|
symbol,
|
|
period,
|
|
mTriggerCycleHelper,
|
|
mDecisionCycleHelper,
|
|
mAnalyseCycleHelper,
|
|
mVerificationCycleHelper,
|
|
mConsolidationCycleHelper,
|
|
mVisionCycleHelper //
|
|
);
|
|
|
|
//
|
|
result = canTrigger;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
triggerTime = mXTWPVConditions.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 = mXTWPVConditions.sl;
|
|
conditions.target = mXTWPVConditions.target;
|
|
|
|
//
|
|
mXTWPVConditions.Clean();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Private ...
|
|
private:
|
|
//
|
|
// Props ...
|
|
|
|
/**
|
|
* Initial Required Indicators Helper ...
|
|
*/
|
|
void InitialHelpers()
|
|
{
|
|
//
|
|
string symbol = GetSymbol();
|
|
ENUM_TIMEFRAMES period = GetPeriod();
|
|
|
|
//
|
|
// 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.scMethod = scMethod;
|
|
x121Inputs.scPeriod = scPeriod;
|
|
x121Inputs.mcMethod = mcMethod;
|
|
x121Inputs.mcPeriod = mcPeriod;
|
|
x121Inputs.lcMethod = lcMethod;
|
|
x121Inputs.lcPeriod = lcPeriod;
|
|
x121Inputs.hcMethod = hcMethod;
|
|
x121Inputs.hcPeriod = hcPeriod;
|
|
|
|
//
|
|
x121Inputs.showFibo2Levels = false;
|
|
x121Inputs.showFibo3Levels = false;
|
|
x121Inputs.showFibo4Levels = false;
|
|
|
|
//
|
|
x121Inputs.showSar = false;
|
|
x121Inputs.showPeaks = true;
|
|
x121Inputs.showVales = true;
|
|
x121Inputs.showVWap = false;
|
|
x121Inputs.showDonchain = true;
|
|
|
|
//
|
|
x121Inputs.showOpen = true;
|
|
x121Inputs.showHigh = true;
|
|
x121Inputs.showClose = true;
|
|
x121Inputs.showLow = true;
|
|
|
|
//
|
|
// Initialize Cycle Helpers ...
|
|
|
|
//
|
|
bool isInited = false;
|
|
|
|
//
|
|
mTriggerCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mTriggerCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_M1,
|
|
x121Inputs //
|
|
);
|
|
|
|
//
|
|
mDecisionCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mDecisionCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_M5,
|
|
x121Inputs //
|
|
);
|
|
|
|
//
|
|
mAnalyseCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mAnalyseCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_M15,
|
|
x121Inputs //
|
|
);
|
|
|
|
//
|
|
mVerificationCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mVerificationCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_M30,
|
|
x121Inputs //
|
|
);
|
|
|
|
//
|
|
mConsolidationCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mConsolidationCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_H1,
|
|
x121Inputs //
|
|
);
|
|
|
|
//
|
|
mVisionCycleHelper = new XC121SMCCycleHelper();
|
|
isInited = mVisionCycleHelper.Init(
|
|
symbol,
|
|
PERIOD_H4,
|
|
x121Inputs //
|
|
);
|
|
}
|
|
|
|
//
|
|
};
|
|
|
|
// |