Files
MQL5Data/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5
T
2024-10-31 13:27:39 +03:30

2190 lines
104 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class Library
// --------------------------------------
// Name: XSCX121Strategy
// 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-saherelm.x-poi.class.mq5"
#include "../../Classes/x-saherelm.xalert.class.mq5"
#include "../../Classes/x-saherelm.xtrade.class.mq5"
#include "../Libraries/x-121.smc.lib.mq5"
//
// Helper(s) ...
//
// XStrategy Class Implementation(s) ...
class XCX121SMCStrategy : public XSCBaseAlert
{
//
// Public ...
public:
//
//
// Constructor(s) ...
void XCX121SMCStrategy(
//
// Base ...
string _symbol, // Trading Symbol
ENUM_TIMEFRAMES _period // Trading TimeFrame
)
{
//
mSymbol = _symbol;
mPeriod = _period;
//
Init();
}
//
// Deconstructor ...
void ~XCX121SMCStrategy()
{
DeInit();
}
//
// Getter / Setter (s) ...
/**
* Retrieve Symbol ...
*
* @return ( string )
*/
string Symbol()
{
return mSymbol;
}
/**
* Retrieve TimeFrame ...
*
* @return ( ENUM_TIMEFRAMES )
*/
ENUM_TIMEFRAMES Period()
{
return mPeriod;
}
/**
* Check Strategy is Disabled or not ...
*
* @return ( bool )
*/
bool IsDisabled()
{
return mForceDisabled;
}
/**
* Get Chart Id ...
*
* @return ( long )
*/
long ChartIdentification()
{
return mChartID;
}
/**
* Set Chart Id ...
*
* @param value: Long ...
*/
void ChartIdentification(long value)
{
//
if (value < 0)
{
value = 0;
}
//
mChartID = value;
if (mPOIDrawer != NULL)
{
mPOIDrawer.ChartIdentification(value);
}
}
/**
* Get Sub Window Id ...
*
* @return ( int )
*/
int SubWindowIdentification()
{
return mSubWindow;
}
/**
* Set Sub Window Id ...
*
* @param value: Integer ...
*/
void SubWindowIdentification(int value)
{
//
if (value < 0)
{
value = 0;
}
//
mSubWindow = value;
if (mPOIDrawer != NULL)
{
mPOIDrawer.SubWindowIdentification(value);
}
}
/**
* Get Signal Volume ...
*
* @return ( double )
*/
double Volume()
{
return mVolume;
}
/**
* Set Signal Volume ...
*
* @param value: Double ...
*/
void Volume(double value)
{
//
if (value < 0.01)
{
value = 0.01;
}
//
mVolume = value;
}
/**
* Get Risk/Reward Ratio ...
*
* @return ( double )
*/
double R2R()
{
return mR2R;
}
/**
* Set Risk/Reward Ratio ...
*
* @param value: Double ...
*/
void R2R(double value)
{
//
if (value < 0)
{
value = 1.5;
}
//
mR2R = value;
}
/**
* Get Max Allowed Spread ...
*
* @return ( double )
*/
double MaxAllowedSpread()
{
return mMaxAllowedSpread;
}
/**
* Set Max Allowed Spread ...
*
* @param value: Double ...
*/
void MaxAllowedSpread(double value)
{
mMaxAllowedSpread = value;
}
/**
* Get Long Signalling State ...
*
* @return ( bool )
*/
bool AllowLong()
{
return mAllowLong;
}
/**
* Set Long Signalling State ...
*
* @param value: Boolean ...
*/
void AllowLong(bool value)
{
mAllowLong = value;
}
/**
* Get Short Signalling State ...
*
* @return ( bool )
*/
bool AllowShort()
{
return mAllowShort;
}
/**
* Set Short Signalling State ...
*
* @param value: Argument 1
*/
void AllowShort(bool value)
{
mAllowShort = value;
}
//
// Actions ...
/**
* Disable Strategy ...
*/
void Disable()
{
mForceDisabled = true;
}
/**
* Enable Strategy ...
*/
void Enable()
{
mForceDisabled = false;
}
/**
* Register Signal Event Handler ...
*
* @param handler: an Instance of TOnXSignal ...
*/
void AddXSignalEventHandler(TOnX121SMCSignal handler)
{
//
Add(
handler,
mSignalEventHandlers //
);
}
//
// Virtual Actions ...
//
// Check Conditions For Signal and Execute Signal ...
virtual void HandleTick()
{
//
// Check State ...
if (IsDisabled())
{
return;
}
//
// Check Signalling State ...
if (!AllowLong() && !AllowShort())
{
return;
}
//
// Check Bar Tracker State ...
if (!mBarTracker.CanProcessBar())
{
return;
}
//
// Retrieve Current Tick and Check it's Time
// by Last Issued Tick ...
MqlTick cTick;
bool hasTick = GetTick(
mSymbol,
cTick //
);
bool isSameAsLast = cTick.time == mLastTick.time;
if (isSameAsLast)
{
return;
}
//
// Update Last Issued Tick ...
mLastTick = cTick;
SaveTickData();
CleanupTicks();
//
// Update POIs ...
//
ENUM_XPOI_EVENTS _1DStates[];
m1DPOI.Update(_1DStates);
//
ENUM_XPOI_EVENTS _4HStates[];
m4HPOI.Update(_4HStates);
// //
// ENUM_XPOI_EVENTS _4HSupDemStates[];
// m4HSupDemPOI.Update(_4HSupDemStates);
//
ENUM_XPOI_EVENTS _15MStates[];
m15MPOI.Update(_15MStates);
//
ENUM_XPOI_EVENTS _5MStates[];
m5MPOI.Update(_5MStates);
//
ENUM_XPOI_EVENTS _1MStates[];
m1MPOI.Update(_1MStates);
//
// Check Current Processing Tick has Signal or not ...
XSignal signal;
X121SMCStrategyConditions conditions;
bool hasSignal = HasSignal(
signal,
conditions //
);
if (!hasSignal)
{
return;
}
//
// Waits Until Next Candle if Current Tick has Signal ...
mBarTracker.Waits();
//
// Notify Signal Event Handlers ...
NotifyOnSignalEventHandlers(
signal,
conditions //
);
//
// TODO: ...
// DrawSignal(
// signal,
// conditions //
// );
//
// ClearDraws();
}
//
// Check Conditions For Signal ...
virtual bool HasSignal(
XSignal &signal,
X121SMCStrategyConditions &conditions //
)
{
//
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();
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;
//
// Start Calculations ...
//
double points = GetPoints(mSymbol);
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
// Retrieve Bars ...
//
// Zero Bar ...
XOHCL zBar;
result = zBar.Init(
mSymbol,
mPeriod,
zIndex //
);
if (!result)
{
return result;
}
//
// Last Bar ...
XOHCL cBar;
result = cBar.Init(
mSymbol,
mPeriod,
cIndex //
);
if (!result)
{
return result;
}
//
// Reading POI(s) ...
//
// 1D ...
XPOIState _1DState;
m1DPOI.GetState(_1DState);
//
// 4H ...
XPOIState _4HState;
m4HPOI.GetState(_4HState);
//
// 15M ...
XPOIState _15MState;
m15MPOI.GetState(_15MState);
//
// 5M ...
XPOIState _5MState;
m5MPOI.GetState(_5MState);
//
// 1M ...
XPOIState _1MState;
m1MPOI.GetState(_1MState);
//
// Try to Implement SMC Based Conditions ...
//
// 1- First Step is Find a Supply and Demand Zone ...
// - [] this Zone must be Fresh ...
// - [] contains Engulfing ...
// - [] has Order Block ...
// - [] has FVG ...
bool isDetected = DetectImportantPOIs(
conditions,
_1DState,
_4HState,
_15MState,
_5MState,
_1MState //
);
if (isDetected)
{
//
// Check Conditions for Removing Exists ...
RemoveConditionsIfExists(conditions);
//
// Add New Condition ...
AddRef(
conditions,
mConditions //
);
}
//
conditions.Clean();
//
// Check Conditions for Signal ...
int conditionsCount = ArraySize(mConditions);
if (!IsValidSize(conditionsCount))
{
//
result = false;
return result;
}
//
// Loop through Conditions for Filter Based on POIs ...
int conditionsIDX = -1;
for (int i = 0; i < conditionsCount; i++)
{
//
result = false;
X121SMCStrategyConditions iConditions = mConditions[i];
//
bool isSpreadPassed = IsSpreadPass();
if (!isSpreadPassed)
{
break;
}
//
DrawConditions(iConditions);
//
// Longs ...
bool isAllowLong = AllowLong();
if (isAllowLong)
{
//
hasLong = false;
//
// Find Demand Zone ...
hasLong = iConditions.demandZone.IsValid();
//
if (hasLong)
{
//
conditionsIDX = i;
//
type = POSITION_TYPE_BUY;
entry = GetEntry(mSymbol, type);
sl = iConditions.demandZone.lower - (10 * points);
risk = entry - sl;
reward = risk * r2r;
tp = entry + reward;
//
break;
}
}
//
// Shorts ...
bool isAllowShort = AllowShort();
if (isAllowShort)
{
//
hasShort = false;
//
if (hasShort)
{
//
conditionsIDX = i;
//
type = POSITION_TYPE_SELL;
entry = GetEntry(mSymbol, type);
sl = iConditions.supplyZone.upper + (10 * points);
risk = sl - entry;
reward = risk * r2r;
tp = entry - reward;
//
break;
}
}
}
//
result = hasLong ||
hasShort;
//
// Rmove Signal Condition ...
if (result &&
IsValidIndex(conditionsIDX))
{
//
ArrayRemove(
mConditions,
conditionsIDX,
1 //
);
//
signal.sl = sl;
signal.tp = tp;
signal.type = type;
signal.mode = mode;
signal.entry = entry;
signal.volume = volume;
signal.symbol = mSymbol;
signal.pushers = pushers;
signal.provider = GetTag();
signal.time = TimeCurrent();
signal.period = _5MState.period;
//
result = PrepareSignal(signal);
//
hasLong = false;
hasShort = false;
}
//
return result;
}
//
// Overrides ...
//
// Customize Strategy Identifier ...
string GetTag() override
{
return X121SMCStrategyToken;
}
//
// Protected ...
protected:
//
//
// Props ...
//
// Actions ...
/**
* Check Spread Passed for Positions...
*
* @return ( bool )
*/
bool IsSpreadPass()
{
//
bool result = false;
//
int length = 5;
double pointValue = GetPoints(mSymbol);
double maxAllowedSpread = MaxAllowedSpread();
if (maxAllowedSpread > 0)
{
maxAllowedSpread *= pointValue;
}
//
int ticksCount = ArraySize(mTicks);
if (ticksCount <= length)
{
return result;
}
//
int start = -1;
int end = -1;
CalculateTicksParams(
start,
end,
length + 1 //
);
result = IsValidIndex(start) &&
IsValidIndex(end);
if (!result)
{
return result;
}
//
end -= 1;
double spreadAVG = 0;
for (int i = start; i < end; i++)
{
spreadAVG += mTicks[i].spread;
}
spreadAVG /= length;
//
double lastSpread = mTicks[ticksCount - 1].spread;
//
result = lastSpread <= spreadAVG &&
(maxAllowedSpread <= 0
? true
: lastSpread <= maxAllowedSpread);
//
return result;
}
/**
* Check Ticks is Bullish or not ...
*
* @return ( bool )
*/
bool IsTicksBullish()
{
//
bool result = false;
//
int start = -1;
int end = -1;
CalculateTicksParams(
start,
end //
);
result = IsValidIndex(start) &&
IsValidIndex(end);
if (!result)
{
return result;
}
//
result = true;
for (int i = start + 1; i < end; i++)
{
//
XTick iTick = mTicks[i];
XTick prevTick = mTicks[i - 1];
//
bool isBullish = iTick.bid >= prevTick.bid;
result = result && isBullish;
if (!result)
{
break;
}
}
//
return result;
}
/**
* Check Ticks is Bearish or not ...
*
* @return ( bool )
*/
bool IsTicksBearish()
{
//
bool result = false;
//
int start = -1;
int end = -1;
CalculateTicksParams(
start,
end //
);
result = IsValidIndex(start) &&
IsValidIndex(end);
if (!result)
{
return result;
}
//
result = true;
for (int i = start + 1; i < end; i++)
{
//
XTick iTick = mTicks[i];
XTick prevTick = mTicks[i - 1];
//
bool isBearish = iTick.bid <= prevTick.bid;
result = result && isBearish;
if (!result)
{
break;
}
}
//
return result;
}
/**
* Check Ticks Has Momentum or not ...
*
* @return ( bool )
*/
bool IsTicksHasMomentum()
{
//
bool result = false;
//
int start = -1;
int end = -1;
CalculateTicksParams(
start,
end //
);
result = IsValidIndex(start) &&
IsValidIndex(end);
if (!result)
{
return result;
}
//
result = true;
int lastDiff = 0;
for (int i = start + 1; i < end; i++)
{
//
XTick iTick = mTicks[i];
XTick prevTick = mTicks[i - 1];
//
int timeDiff = MathAbs((int)iTick.time - (int)prevTick.bid);
result = result && timeDiff >= lastDiff;
if (!result)
{
break;
}
//
lastDiff = timeDiff;
}
//
return result;
}
/**
* Prepare and Normalize Signal for Execution in Strategy ...
*
* @param signal: XSignal instance ...
*
* @return ( bool )
*/
bool PrepareSignal(XSignal &signal)
{
//
bool result = false;
//
// Check Strategy Is Enables ...
result = !IsDisabled();
if (!result)
{
return result;
}
//
// Validate Signal ...
result = signal.IsValid();
if (!result)
{
return result;
}
//
bool isLong = IsLong(signal.type);
//
// TODO: Implement Signal Preparation Mechanism here ...
//
return result;
}
/**
* Update Supply and Demand Zones ...
*
* @param state: Argument 1
*/
bool DetectImportantPOIs(
X121SMCStrategyConditions &conditions,
XPOIState &d1State,
XPOIState &h4State,
XPOIState &m15State,
XPOIState &m5State,
XPOIState &m1State //
)
{
//
bool result = false;
//
// Prepare Conditions ...
//
conditions.Clean();
conditions.symbol = mSymbol;
conditions.period = mPeriod;
conditions.time = TimeCurrent();
// //
// bool d1HasBullishTrend = HasBullishTrend(d1State);
// bool d1HasBearishTrend = HasBearishTrend(d1State);
// //
// bool h4HasBullishTrend = HasBullishTrend(h4State);
// bool h4HasBearishTrend = HasBearishTrend(h4State);
// //
// bool m15HasBullishTrend = HasBullishTrend(m15State);
// bool m15HasBearishTrend = HasBearishTrend(m15State);
// //
// bool m5HasBullishTrend = HasBullishTrend(m5State);
// bool m5HasBearishTrend = HasBearishTrend(m5State);
// //
// bool m1HasBullishTrend = HasBullishTrend(m1State);
// bool m1HasBearishTrend = HasBearishTrend(m1State);
// //
// bool isBullishTrendValid = h4HasBullishTrend &&
// m15HasBullishTrend &&
// m1HasBullishTrend;
// if (isBullishTrendValid)
// {
// Print("isBullishTrendValid");
// }
// //
// bool isBearishTrendValid = h4HasBearishTrend &&
// m15HasBearishTrend &&
// m1HasBearishTrend;
// if (isBearishTrendValid)
// {
// Print("isBearishTrendValid");
// }
//
int supplyZonesCount = ArraySize(m15State.supplyZones);
int demandZonesCount = ArraySize(m15State.demandZones);
result =
IsValidSize(demandZonesCount) ||
IsValidSize(supplyZonesCount);
if (!result)
{
return result;
}
//
// Demand Zone ...
for (int i = 0; i < demandZonesCount; i++)
{
//
// Demand Zone ...
XZone iDemandZone = m15State.demandZones[i];
conditions.demandZone = iDemandZone;
//
// Order Blocks ...
int orderBlocksCount = ArraySize(h4State.bullishOrderBlocks);
if (IsValidSize(orderBlocksCount))
{
//
for (int j = 0; j < orderBlocksCount; j++)
{
//
XZone jZone = h4State.bullishOrderBlocks[j];
//
bool isInside =
(jZone.lower > iDemandZone.lower &&
jZone.lower < iDemandZone.upper) ||
(jZone.lower < iDemandZone.lower);
if (isInside)
{
//
jZone.to = TimeCurrent();
//
AddRef(
jZone,
conditions.bullishOrderBlocks //
);
}
}
}
//
// Support Zones ...
int supportsCount = ArraySize(h4State.supportZones);
if (IsValidSize(supportsCount))
{
//
for (int j = 0; j < supportsCount; j++)
{
//
XOHCL jBar = h4State.supportZones[j];
//
bool isInside =
(jBar.low > iDemandZone.lower &&
jBar.low < iDemandZone.upper) ||
(jBar.low < iDemandZone.lower);
if (isInside)
{
//
AddRef(
jBar,
conditions.supports //
);
}
}
}
//
// Bullish FVGs ...
int fvgsCount = ArraySize(m15State.bullishFVGs);
if (IsValidSize(fvgsCount))
{
//
for (int j = 0; j < fvgsCount; j++)
{
//
XZone jFVG = m15State.bullishFVGs[j];
//
bool isInside =
jFVG.from > iDemandZone.from &&
jFVG.lower < iDemandZone.upper &&
jFVG.upper > iDemandZone.upper;
if (isInside)
{
//
jFVG.to = TimeCurrent();
//
AddRef(
jFVG,
conditions.bullishFVGs //
);
}
}
}
//
// Swing Lows ...
int swingLowsCount = ArraySize(m5State.swingLows);
if (IsValidSize(swingLowsCount))
{
//
for (int j = 0; j < swingLowsCount; j++)
{
//
XOHCL jBar = m5State.swingLows[j];
//
bool isInside =
jBar.time > iDemandZone.from &&
jBar.low > iDemandZone.lower &&
jBar.GetDown() < iDemandZone.upper;
if (isInside)
{
//
AddRef(
jBar,
conditions.swingLows //
);
}
}
}
//
// Bullish Momentum Bars ...
int bullishMomentumBarsCount = ArraySize(m5State.momentumBars);
if (IsValidSize(bullishMomentumBarsCount))
{
//
for (int j = 0; j < bullishMomentumBarsCount; j++)
{
//
XOHCL jBar = m5State.momentumBars[j];
//
bool isInside =
jBar.IsBullish() &&
jBar.time > iDemandZone.from &&
jBar.low > iDemandZone.lower &&
jBar.low < iDemandZone.upper;
if (isInside)
{
//
AddRef(
jBar,
conditions.bullishMomentumBars //
);
}
}
}
//
// Bullish Rejection Bars ...
int bullishRejectionBarsCount = ArraySize(m5State.rejectionBars);
if (IsValidSize(bullishRejectionBarsCount))
{
//
for (int j = 0; j < bullishRejectionBarsCount; j++)
{
//
XOHCL jBar = m5State.rejectionBars[j];
//
bool isInside =
jBar.GetLowShadow() > jBar.GetHighShadow() &&
jBar.time > iDemandZone.from &&
jBar.low > iDemandZone.lower &&
jBar.low < iDemandZone.upper;
if (isInside)
{
//
AddRef(
jBar,
conditions.bullishRejectionBars //
);
}
}
}
}
//
// Demand ...
// Used for Long ...
bool hasValidDemandZone = HasValidDemand(conditions);
//
// Supply ...
// Used for Short ...
bool hasValidSupplyZone = HasValidSupply(conditions);
//
// Check Conditions is Valid or not ...
result = hasValidDemandZone ||
hasValidSupplyZone;
//
return result;
}
/**
* Check Conditions has Valid Demand ...
*
* @param conditions: X121SMCStrategyConditions instance ...
*
* @return ( bool )
*/
bool HasValidDemand(X121SMCStrategyConditions &conditions)
{
//
bool result = false;
//
result = conditions.IsValid();
if (!result)
{
return result;
}
//
result =
//
conditions.demandZone.IsValid() &&
(ArraySize(conditions.supports) > 0 ||
ArraySize(conditions.swingLows) > 0 ||
ArraySize(conditions.bullishFVGs) > 0 ||
ArraySize(conditions.bullishOrderBlocks) > 0 ||
ArraySize(conditions.bullishMomentumBars) > 0 ||
ArraySize(conditions.bullishRejectionBars) > 0)
//
;
//
return result;
}
/**
* Check Conditions has Valid Supply ...
*
* @param conditions: X121SMCStrategyConditions instance ...
*
* @return ( bool )
*/
bool HasValidSupply(X121SMCStrategyConditions &conditions)
{
//
bool result = false;
//
result = conditions.IsValid();
if (!result)
{
return result;
}
//
result =
//
conditions.supplyZone.IsValid() &&
(ArraySize(conditions.swingHighs) > 0 ||
ArraySize(conditions.bearishFVGs) > 0 ||
ArraySize(conditions.resistances) > 0 ||
ArraySize(conditions.bearishOrderBlocks) > 0 ||
ArraySize(conditions.bearishMomentumBars) > 0 ||
ArraySize(conditions.bearishRejectionBars) > 0)
//
;
//
return result;
}
/**
* Combine a Conditions by Conditions List and Remove it if Exists ...
*
* @param conditions: X121SMCStrategyConditions instance ...
*/
void RemoveConditionsIfExists(X121SMCStrategyConditions &conditions)
{
//
int count = ArraySize(mConditions);
if (!IsValidSize(count))
{
return;
}
//
bool hasValidSupply = HasValidSupply(conditions);
bool hasValidDemand = HasValidDemand(conditions);
if (!hasValidSupply && !hasValidDemand)
{
return;
}
//
int idx = -1;
for (int i = 0; i < count; i++)
{
//
X121SMCStrategyConditions iConditions = mConditions[i];
//
bool isSupplySame =
hasValidSupply &&
HasValidSupply(iConditions) &&
conditions.supplyZone.from == iConditions.supplyZone.from &&
conditions.supplyZone.upper == iConditions.supplyZone.upper &&
conditions.supplyZone.lower == iConditions.supplyZone.lower;
bool isDemandSame =
hasValidDemand &&
HasValidDemand(iConditions) &&
conditions.demandZone.from == iConditions.demandZone.from &&
conditions.demandZone.upper == iConditions.demandZone.upper &&
conditions.demandZone.lower == iConditions.demandZone.lower;
//
if (isSupplySame || isDemandSame)
{
//
idx = i;
break;
}
}
//
if (IsValidIndex(idx))
{
//
ArrayRemove(
mConditions,
idx,
1 //
);
}
}
//
/**
* Notified Signal Event Handler(s) ...
*
* @param signal: XSignal instance ...
* @param conditions: XStrategyConditions instance ...
*/
void NotifyOnSignalEventHandlers(
XSignal &signal,
X121SMCStrategyConditions &conditions //
)
{
//
if (!signal.IsValid() || !conditions.IsValid())
{
return;
}
//
int count = ArraySize(mSignalEventHandlers);
if (!IsValidSize(count))
{
return;
}
//
for (int i = 0; i < count; i++)
{
//
mSignalEventHandlers[i](
signal,
conditions //
);
}
}
//
// Draw Functions ...
/**
* Clear all Drawn Objects ...
*/
void ClearDraws()
{
mDrawnObjects.Clear();
}
/**
* Remove Specified Drawn Object ...
*
* @param name: Object name ...
*/
void RemoveObject(string name)
{
//
if (!IsValid(name))
{
return;
}
//
int count = mDrawnObjects.Total();
if (!IsValidSize(count))
{
return;
}
//
int idx = -1;
for (int i = 0; i < count; i++)
{
//
string iName = ((CChartObject *)mDrawnObjects.At(i)).Name();
string iObjName = NULL;
if (!IsValid(iName))
{
iObjName = ((XCBaseObject *)mDrawnObjects.At(i)).ObjName();
}
//
if ((IsValid(iName) &&
iName == name) ||
(IsValid(iObjName) &&
iObjName == name))
{
//
idx = i;
break;
}
}
//
if (IsValidIndex(idx))
{
mDrawnObjects.Delete(idx);
}
}
/**
* Draw Specific Condition ...
*
* @param conditions: X121SMCStrategyConditions instance ...
*/
void DrawConditions(X121SMCStrategyConditions &conditions)
{
//
if (!conditions.IsValid())
{
return;
}
//
// Demand Zone ...
if (conditions.demandZone.IsValid())
{
//
// Demand ...
XCDemandZoneObject *iObj;
bool isCreated = mPOIDrawer.CreateDemandZone(
conditions.demandZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
//
// Order Blocks ...
int obsCount = ArraySize(conditions.bullishOrderBlocks);
if (IsValidSize(obsCount))
{
//
for (int i = 0; i < obsCount; i++)
{
//
XZone iZone = conditions.bullishOrderBlocks[i];
//
XCBullishOrderBlockObject *iObj;
bool isCreated = mPOIDrawer.CreateBullishOrderBlock(
iZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Fair Value Gaps ...
int fvgsCount = ArraySize(conditions.bullishFVGs);
if (IsValidSize(fvgsCount))
{
//
for (int i = 0; i < fvgsCount; i++)
{
//
XZone iZone = conditions.bullishFVGs[i];
//
XCBullishFVGObject *iObj;
bool isCreated = mPOIDrawer.CreateBullishFVG(
iZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Supports ...
int supportsCount = ArraySize(conditions.supports);
if (IsValidSize(supportsCount))
{
//
for (int i = 0; i < supportsCount; i++)
{
//
XOHCL iBar = conditions.supports[i];
//
XCSupportZoneObject *iObj;
bool isCreated = mPOIDrawer.CreateSupportZone(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Swings ...
int swingsCount = ArraySize(conditions.swingLows);
if (IsValidSize(swingsCount))
{
//
for (int i = 0; i < swingsCount; i++)
{
//
XOHCL iBar = conditions.swingLows[i];
//
XCSwingLowObject *iObj;
bool isCreated = mPOIDrawer.CreateSwingLow(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Momentum Bars ...
int momentumBarsCount = ArraySize(conditions.bullishMomentumBars);
if (IsValidSize(momentumBarsCount))
{
//
for (int i = 0; i < momentumBarsCount; i++)
{
//
XOHCL iBar = conditions.bullishMomentumBars[i];
//
XCBullishMomentumBarObject *iObj;
bool isCreated = mPOIDrawer.CreateBullishMomentumBar(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Rejection Bars ...
int rejectionBarsCount = ArraySize(conditions.bullishRejectionBars);
if (IsValidSize(rejectionBarsCount))
{
//
for (int i = 0; i < rejectionBarsCount; i++)
{
//
XOHCL iBar = conditions.bullishRejectionBars[i];
//
XCBullishRejectionBarObject *iObj;
bool isCreated = mPOIDrawer.CreateBullishRejectionBar(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
}
//
// Supply Zone ...
if (conditions.supplyZone.IsValid())
{
//
// Supply ...
XCSupplyZoneObject *iObj;
bool isCreated = mPOIDrawer.CreateSupplyZone(
conditions.supplyZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
//
// Order Blocks ...
int obsCount = ArraySize(conditions.bearishOrderBlocks);
if (IsValidSize(obsCount))
{
//
for (int i = 0; i < obsCount; i++)
{
//
XZone iZone = conditions.bearishOrderBlocks[i];
//
XCBearishOrderBlockObject *iObj;
bool isCreated = mPOIDrawer.CreateBearishOrderBlock(
iZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Fair Value Gaps ...
int fvgsCount = ArraySize(conditions.bearishFVGs);
if (IsValidSize(fvgsCount))
{
//
for (int i = 0; i < fvgsCount; i++)
{
//
XZone iZone = conditions.bearishFVGs[i];
//
XCBearishFVGObject *iObj;
bool isCreated = mPOIDrawer.CreateBearishFVG(
iZone,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Resistances ...
int resistancesCount = ArraySize(conditions.resistances);
if (IsValidSize(resistancesCount))
{
//
for (int i = 0; i < resistancesCount; i++)
{
//
XOHCL iBar = conditions.resistances[i];
//
XCResistanceZoneObject *iObj;
bool isCreated = mPOIDrawer.CreateResistanceZone(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Swings ...
int swingsCount = ArraySize(conditions.swingHighs);
if (IsValidSize(swingsCount))
{
//
for (int i = 0; i < swingsCount; i++)
{
//
XOHCL iBar = conditions.swingHighs[i];
//
XCSwingHighObject *iObj;
bool isCreated = mPOIDrawer.CreateSwingHigh(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Momentum Bars ...
int momentumBarsCount = ArraySize(conditions.bearishMomentumBars);
if (IsValidSize(momentumBarsCount))
{
//
for (int i = 0; i < momentumBarsCount; i++)
{
//
XOHCL iBar = conditions.bearishMomentumBars[i];
//
XCBearishMomentumBarObject *iObj;
bool isCreated = mPOIDrawer.CreateBearishMomentumBar(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
//
// Rejection Bars ...
int rejectionBarsCount = ArraySize(conditions.bearishRejectionBars);
if (IsValidSize(rejectionBarsCount))
{
//
for (int i = 0; i < rejectionBarsCount; i++)
{
//
XOHCL iBar = conditions.bearishRejectionBars[i];
//
XCBearishRejectionBarObject *iObj;
bool isCreated = mPOIDrawer.CreateBearishRejectionBar(
iBar,
iObj //
);
if (isCreated)
{
//
// RemoveObject(iObj.ObjName());
mDrawnObjects.Add(iObj);
}
}
}
}
}
/**
* Draw Specified Signal ...
*
* @param signal: XSignal instance ...
* @param conditions: X121SMCStrategyConditions instance ...
*/
void DrawSignal(
XSignal &signal,
X121SMCStrategyConditions &conditions //
)
{
//
if (!signal.IsValid() ||
!conditions.IsValid())
{
//
return;
}
//
// DrawConditions(conditions);
//
string name = signal.GetTag();
RemoveObject(name);
//
XCSignalObject *iObj;
iObj = new XCSignalObject();
//
bool isCreated = iObj.Create(
ChartID(),
name,
0,
signal //
);
if (!isCreated)
{
return;
}
//
iObj.TPWidth(1);
iObj.TPColor(clrGreen);
iObj.TPWidth(STYLE_DOT);
//
iObj.SLWidth(1);
iObj.SLColor(clrRed);
iObj.SLWidth(STYLE_DOT);
//
iObj.EntryWidth(1);
iObj.EntryColor(clrYellow);
iObj.EntryWidth(STYLE_DOT);
//
mDrawnObjects.Add(iObj);
}
//
// Private ...
private:
//
// Props ...
//
string mSymbol; // Trading Symbol ...
ENUM_TIMEFRAMES mPeriod; // Trading TimeFrame ...
bool mForceDisabled; // Force Disabled ...
bool mAllowLong; // Allow Long Signals
bool mAllowShort; // Allow Short Signals
double mR2R; // Risk/Rewar Ratio ...
double mVolume; // Static Volume Per Trades ...
double mMaxAllowedSpread; // Max Allowed Spread ...
//
long mChartID; // Chart ID ...
int mSubWindow; // Sub Window ID ...
XCPOIDrawer *mPOIDrawer; // POI Drawer Class ...
//
XTick mTicks[]; // Hold Ticks Data ...
MqlTick mLastTick; // Last Issued Tick ...
//
XBarTracker mBarTracker; // Strategy Time Tracker ...
//
// Point Of Interests ...
XCPOI *m1DPOI;
XCPOI *m4HPOI;
XCPOI *m15MPOI;
XCPOI *m5MPOI;
XCPOI *m1MPOI;
//
// Collect all Drawn Objects ...
CArrayObj mDrawnObjects;
//
XOHCL mSwingHigh;
CChartObjectHLine *mSwingHighObject;
//
XOHCL mSwingLow;
CChartObjectHLine *mSwingLowObject;
//
X121SMCStrategyConditions mConditions[];
//
// On X121 Signal Recieved Event Handler(s) ...
TOnX121SMCSignal mSignalEventHandlers[];
//
// Actions ...
/**
* Install all Helpers and Prepare all Configurations for required Indicators ...
*/
void Init()
{
//
mForceDisabled = false;
//
R2R(2);
Volume(0.01);
//
ChartIdentification(0);
SubWindowIdentification(0);
//
// Configure POI Drawer if Required ...
mPOIDrawer = new XCPOIDrawer();
mPOIDrawer.ChartIdentification(0);
mPOIDrawer.SubWindowIdentification(0);
//
mPOIDrawer.DemandZoneFill(false);
mPOIDrawer.SupplyZoneFill(false);
mPOIDrawer.BullishFVGFill(false);
mPOIDrawer.BearishFVGFill(false);
mPOIDrawer.SupportZoneFill(false);
mPOIDrawer.ResistanceZoneFill(false);
mPOIDrawer.BullishOrderBlockFill(false);
mPOIDrawer.BearishOrderBlockFill(false);
//
string symbol = Symbol();
ENUM_TIMEFRAMES period = Period();
//
// Initialize Bar Tracker ...
mBarTracker.Init(
symbol,
period //
);
//
// Initialize POIs ...
//
// 1D Period ...
// POI Detector ...
m1DPOI = new XCPOI(
symbol,
PERIOD_D1 //
);
//
m1DPOI.DisableDrawPOIs();
m1DPOI.SupplyZonePeriod(PERIOD_W1);
m1DPOI.DemandZonePeriod(PERIOD_W1);
m1DPOI.SubWindowIdentification(0);
m1DPOI.ChartIdentification(ChartID());
//
m1DPOI.Init();
//
// 4H Period ...
// POI Detector ...
m4HPOI = new XCPOI(
symbol,
PERIOD_H4 //
);
//
m4HPOI.DisableDrawPOIs();
m4HPOI.SupplyZonePeriod(PERIOD_D1);
m4HPOI.DemandZonePeriod(PERIOD_D1);
m4HPOI.SubWindowIdentification(0);
m4HPOI.ChartIdentification(ChartID());
//
m4HPOI.Init();
//
// 15M Period ...
// POI Detector ...
m15MPOI = new XCPOI(
symbol,
PERIOD_M15 //
);
//
m15MPOI.DisableDrawPOIs();
m15MPOI.MaxNumberOfRequiredPOIs(2);
m15MPOI.SupplyZonePeriod(PERIOD_H4);
m15MPOI.DemandZonePeriod(PERIOD_H4);
m15MPOI.SubWindowIdentification(0);
m15MPOI.ChartIdentification(ChartID());
//
m15MPOI.Init();
//
// 5M Period ...
// POI Detector ...
m5MPOI = new XCPOI(
symbol,
PERIOD_M5 //
);
//
m5MPOI.DisableDrawPOIs();
m5MPOI.MaxNumberOfRequiredPOIs(2);
m5MPOI.SupplyZonePeriod(PERIOD_M15);
m5MPOI.DemandZonePeriod(PERIOD_M15);
m5MPOI.SubWindowIdentification(0);
m5MPOI.ChartIdentification(ChartID());
//
m5MPOI.Init();
//
// 1M Period ...
// POI Detector ...
m1MPOI = new XCPOI(
symbol,
PERIOD_M1 //
);
//
m1MPOI.DisableDrawPOIs();
m1MPOI.SupplyZonePeriod(PERIOD_M5);
m1MPOI.DemandZonePeriod(PERIOD_M5);
m1MPOI.SubWindowIdentification(0);
m1MPOI.ChartIdentification(ChartID());
//
m1MPOI.Init();
//
}
/**
* De Initialize all Tools ...
*/
void DeInit()
{
//
delete m1DPOI;
delete m4HPOI;
delete m5MPOI;
delete m1MPOI;
delete m15MPOI;
//
delete mPOIDrawer;
//
ClearDraws();
}
/**
* Reste Bar Tracker ...
*/
void ResetBarTracker()
{
mBarTracker.Clean();
}
/**
* Cleanup Ticks Size ...
*/
void CleanupTicks()
{
//
int maxRequiredTicks = 50;
int ticksCount = ArraySize(mTicks);
if (!IsValidSize(ticksCount) ||
ticksCount <= maxRequiredTicks)
{
return;
}
//
int removeCount = ticksCount - maxRequiredTicks;
if (!IsValidSize(removeCount))
{
return;
}
//
ArrayRemove(
mTicks,
0,
removeCount //
);
}
/**
* Add new Tick Data to Collection ...
*/
void SaveTickData()
{
//
XTick tick;
bool isInited = tick.Init(mSymbol);
if (!isInited)
{
return;
}
//
AddRef(
tick,
mTicks //
);
}
/**
* Calculate Ticks Param for Looping through Last n items ...
*
* @param start: Integer Reference to Start ...
* @param end: Integer Reference to End ...
* @param numOfLastTicks: Integer Max Number of Required N Last Ticks ...
*/
void CalculateTicksParams(
int &start,
int &end,
int numOfLastTicks = 5 //
)
{
//
start = -1;
end = -1;
//
int count = ArraySize(mTicks);
//
start = count - numOfLastTicks;
if (start < 0)
{
start = 0;
}
//
end = start + numOfLastTicks;
if (end > count)
{
end = count;
}
}
//
};
//