Files
MQL5Data/X121SMCEA/Libraries/x-121.smc.guard.lib.mq5
T
2025-01-16 02:14:34 +03:30

1051 lines
26 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 X121 SMC Signal Class
// -------------------------------------------------
// Name:
// Description: provide all Global Guarding functions ...
//
//
// 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.market.cycle.helper.class.mq5"
#include "../Libraries/x-121.smc.lib.mq5"
//
XPriceZones x121SMCDecisionPriceZone;
XPriceZones x121SMCAnalysePriceZone;
XPriceZones x121SMCVerificationPriceZone;
XConsolidationZone x121SMCConsolidationZone;
bool DetectX121SMCGuards(
string provider, // Provider ...
X121SMCGuard &guards[],
const XPosition &positions[],
XC121SMCCycleHelper *triggerCycleHelper,
XC121SMCCycleHelper *decisionCycleHelper,
XC121SMCCycleHelper *analyseCycleHelper,
XC121SMCCycleHelper *verificationCycleHelper,
XC121SMCCycleHelper *consolidationCycleHelper,
XC121SMCCycleHelper *visionCycleHelper //
)
{
//
bool result = false;
//
int count = ArraySize(positions);
result = IsValidSize(count);
if (!result)
{
return result;
}
//
string symbol = triggerCycleHelper.GetSymbol();
ENUM_TIMEFRAMES period = triggerCycleHelper.GetPeriod();
//
XPosition ownPositions[];
XPosition ownLongPositions[];
XPosition ownShortPositions[];
for (int i = 0; i < count; i++)
{
//
XPosition iPosition = positions[i];
bool isOwn = iPosition
.IsFiltersPassed(
symbol,
provider,
NULL,
X_POSITION_TYPE_ALL,
iPosition.magic //
);
if (isOwn)
{
//
AddRef(
iPosition,
ownPositions //
);
//
bool isLong = IsLong(positions[i].type);
if (isLong)
{
//
AddRef(
iPosition,
ownLongPositions //
);
}
else
{
//
AddRef(
iPosition,
ownShortPositions //
);
}
}
}
//
count = ArraySize(ownPositions);
//
int longsCount = ArraySize(ownLongPositions);
bool hasLongs = IsValidSize(longsCount);
//
int shortsCount = ArraySize(ownShortPositions);
bool hasShorts = IsValidSize(shortsCount);
//
result = IsValidSize(count);
if (!result)
{
return result;
}
//
// Here we Are Sure there is Positions to Guard ...
// based on Current Provider ...
//
// Detect Required Data for Analyse Market to Guard Positions ...
//
int zIndex = 0;
int cIndex = zIndex + 1;
int pIndex = cIndex + 1;
int ppIndex = pIndex + 1;
//
int loopback = 10;
//
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 ask = GetAsk(symbol);
double bid = GetBid(symbol);
datetime cTime = TimeCurrent();
double points = GetPoints(symbol);
//
int swingLoopback = 3;
double ll = cBar.FindLowest(swingLoopback, MODE_LOW);
double hh = cBar.FindHighest(swingLoopback, MODE_HIGH);
double llDown = cBar.FindLowesttDown(swingLoopback);
double hhUp = cBar.FindHighestUp(swingLoopback);
//
// Cycles Conditions ...
//
// Trigger ...
ENUM_XPOI_EVENTS triggerPoiEvents[];
ENUM_X_CYCLE_EVENTS triggerCycleEvents[];
X121SMCCycleConditions triggerConditions;
result = triggerCycleHelper.GetConditions(
triggerCycleEvents,
triggerPoiEvents,
triggerConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Decision ...
ENUM_XPOI_EVENTS decisionPoiEvents[];
ENUM_X_CYCLE_EVENTS decisionCycleEvents[];
X121SMCCycleConditions decisionConditions;
result = decisionCycleHelper.GetConditions(
decisionCycleEvents,
decisionPoiEvents,
decisionConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Analyse ...
ENUM_XPOI_EVENTS analysePoiEvents[];
ENUM_X_CYCLE_EVENTS analyseCycleEvents[];
X121SMCCycleConditions analyseConditions;
result = analyseCycleHelper.GetConditions(
analyseCycleEvents,
analysePoiEvents,
analyseConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Verification ...
ENUM_XPOI_EVENTS verificationPoiEvents[];
ENUM_X_CYCLE_EVENTS verificationCycleEvents[];
X121SMCCycleConditions verificationConditions;
result = verificationCycleHelper.GetConditions(
verificationCycleEvents,
verificationPoiEvents,
verificationConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Consolidation ...
ENUM_XPOI_EVENTS consolidationPoiEvents[];
ENUM_X_CYCLE_EVENTS consolidationCycleEvents[];
X121SMCCycleConditions consolidationConditions;
result = consolidationCycleHelper.GetConditions(
consolidationCycleEvents,
consolidationPoiEvents,
consolidationConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Vision ...
ENUM_XPOI_EVENTS visionPoiEvents[];
ENUM_X_CYCLE_EVENTS visionCycleEvents[];
X121SMCCycleConditions visionConditions;
result = visionCycleHelper.GetConditions(
visionCycleEvents,
visionPoiEvents,
visionConditions,
zIndex,
loopback //
);
if (!result)
{
return result;
}
//
// Cycles POI States ...
XPOIState triggerState = triggerConditions.state;
XPOIState decisionState = decisionConditions.state;
XPOIState analyseState = analyseConditions.state;
XPOIState verificationState = verificationConditions.state;
XPOIState consolidationState = consolidationConditions.state;
XPOIState visionState = visionConditions.state;
//
// Cycle X121Conditions ...
X121Conditions triggerXConditions = triggerConditions.x121Conditions;
X121Conditions decisionXConditions = decisionConditions.x121Conditions;
X121Conditions analyseXConditions = analyseConditions.x121Conditions;
X121Conditions verificationXConditions = verificationConditions.x121Conditions;
X121Conditions consolidationXConditions = consolidationConditions.x121Conditions;
X121Conditions visionXConditions = visionConditions.x121Conditions;
//
// Trigger ...
ENUM_X_DIRECTION triggerMarketDir;
bool hasTriggerMarketDir = triggerCycleHelper
.DetectMarketStructure(triggerMarketDir);
//
bool hasTriggerBullishTrend =
hasTriggerMarketDir &&
IsBullish(triggerMarketDir);
//
bool hasTriggerBearishTrend =
hasTriggerMarketDir &&
IsBearish(triggerMarketDir);
//
// Decision ...
ENUM_X_DIRECTION decisionMarketDir;
bool hasDecisionMarketDir = decisionCycleHelper
.DetectMarketStructure(decisionMarketDir);
//
bool hasDecisionBullishTrend =
hasDecisionMarketDir &&
IsBullish(decisionMarketDir);
//
bool hasDecisionBearishTrend =
hasDecisionMarketDir &&
IsBearish(decisionMarketDir);
//
// Consolidation Zone ...
int consolidationLoopback = 7;
double consolidationZoneLower = 0;
double consolidationZoneUpper = 0;
bool isConsolidationZoneBreaked = false;
bool isConsolidationZoneBreakedUp = false;
bool isConsolidationZoneBreakedDown = false;
bool hasConsolidationZone = x121SMCConsolidationZone.IsValid();
if (!hasConsolidationZone)
{
//
double upper = 0;
double lower = 0;
hasConsolidationZone =
triggerCycleHelper
.mBarAnalyser
.IsConsolidate(
cBar,
upper,
lower,
consolidationLoopback //
);
//
if (hasConsolidationZone)
{
//
hasConsolidationZone = x121SMCConsolidationZone.Init(
cBar.symbol,
cBar.period,
upper,
lower,
consolidationLoopback //
);
}
}
//
if (hasConsolidationZone)
{
//
x121SMCConsolidationZone.Update();
//
consolidationZoneUpper = x121SMCConsolidationZone.upper;
consolidationZoneLower = x121SMCConsolidationZone.lower;
//
isConsolidationZoneBreaked = x121SMCConsolidationZone.IsBreaked();
isConsolidationZoneBreakedUp =
isConsolidationZoneBreaked &&
IsBullish(x121SMCConsolidationZone.breakDirection);
isConsolidationZoneBreakedDown =
isConsolidationZoneBreaked &&
IsBearish(x121SMCConsolidationZone.breakDirection);
//
consolidationZoneUpper = triggerXConditions.peaksBuffer[1];
consolidationZoneLower = triggerXConditions.valesBuffer[1];
//
if (isConsolidationZoneBreaked)
{
x121SMCConsolidationZone.Clean();
}
}
//
// Detect Price Zones ...
//
// Decision ...
double decisionPriceZoneUpper = 0;
double decisionPriceZoneLower = 0;
bool isValidDecisionPriceZoneForPeak = false;
bool isValidDecisionPriceZoneForVale = false;
bool isDecisionPriceZoneLowerRejected = false;
bool isDecisionPriceZoneUpperRejected = false;
bool isDecisionPriceZoneUpperBreakedUp = false;
bool isDecisionPriceZoneLowerBreakedDown = false;
bool hasDecisionPriceZone = x121SMCDecisionPriceZone.IsValid();
//
if (!hasDecisionPriceZone)
{
//
hasDecisionPriceZone = CalculatePriceInsideZones(
x121SMCDecisionPriceZone,
decisionState,
cBar //
);
}
//
if (hasDecisionPriceZone)
{
//
bool hasBoundary = x121SMCDecisionPriceZone.GetBoundary(
X_DIRECTION_ALL,
decisionPriceZoneUpper,
decisionPriceZoneLower //
);
//
if (hasBoundary)
{
//
XOHCL iBar;
bool isInited = iBar.Init(
symbol,
decisionXConditions.period,
cIndex //
);
//
isValidDecisionPriceZoneForPeak =
decisionXConditions.peaksBuffer[1] <= decisionPriceZoneUpper &&
decisionXConditions.peaksBuffer[1] >= decisionPriceZoneLower;
//
isValidDecisionPriceZoneForVale =
decisionXConditions.valesBuffer[1] <= decisionPriceZoneUpper &&
decisionXConditions.valesBuffer[1] >= decisionPriceZoneLower;
//
isDecisionPriceZoneLowerRejected =
isInited &&
IsBarReject(
decisionPriceZoneLower,
X_DIRECTION_BULLISH,
iBar //
);
//
isDecisionPriceZoneUpperRejected =
isInited &&
IsBarReject(
decisionPriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
//
isDecisionPriceZoneUpperBreakedUp =
isInited &&
IsBarBreak(
decisionPriceZoneUpper,
X_DIRECTION_BULLISH,
iBar //
);
//
isDecisionPriceZoneLowerBreakedDown =
isInited &&
IsBarBreak(
decisionPriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
}
}
//
// Analyse ...
double analysePriceZoneUpper = 0;
double analysePriceZoneLower = 0;
bool isValidAnalysePriceZoneForPeak = false;
bool isValidAnalysePriceZoneForVale = false;
bool isAnalysePriceZoneLowerRejected = false;
bool isAnalysePriceZoneUpperRejected = false;
bool isAnalysePriceZoneUpperBreakedUp = false;
bool isAnalysePriceZoneLowerBreakedDown = false;
bool hasAnalysePriceZone = x121SMCAnalysePriceZone.IsValid();
//
if (!hasAnalysePriceZone)
{
//
hasAnalysePriceZone = CalculatePriceInsideZones(
x121SMCAnalysePriceZone,
analyseState,
cBar //
);
}
//
if (hasAnalysePriceZone)
{
//
bool hasBoundary = x121SMCAnalysePriceZone.GetBoundary(
X_DIRECTION_ALL,
analysePriceZoneUpper,
analysePriceZoneLower //
);
//
if (hasBoundary)
{
//
XOHCL iBar;
bool isInited = iBar.Init(
symbol,
analyseXConditions.period,
cIndex //
);
//
isValidAnalysePriceZoneForPeak =
analyseXConditions.peaksBuffer[1] <= analysePriceZoneUpper &&
analyseXConditions.peaksBuffer[1] >= analysePriceZoneLower;
//
isValidAnalysePriceZoneForVale =
analyseXConditions.valesBuffer[1] <= analysePriceZoneUpper &&
analyseXConditions.valesBuffer[1] >= analysePriceZoneLower;
//
isAnalysePriceZoneLowerRejected =
isInited &&
IsBarReject(
analysePriceZoneLower,
X_DIRECTION_BULLISH,
iBar //
);
//
isAnalysePriceZoneUpperRejected =
isInited &&
IsBarReject(
analysePriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
//
isAnalysePriceZoneUpperBreakedUp =
isInited &&
IsBarBreak(
analysePriceZoneUpper,
X_DIRECTION_BULLISH,
iBar //
);
//
isAnalysePriceZoneLowerBreakedDown =
isInited &&
IsBarBreak(
analysePriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
}
}
//
// Verification ...
double verificationPriceZoneUpper = 0;
double verificationPriceZoneLower = 0;
bool isValidVerificationPriceZoneForPeak = false;
bool isValidVerificationPriceZoneForVale = false;
bool isVerificationPriceZoneLowerRejected = false;
bool isVerificationPriceZoneUpperRejected = false;
bool isVerificationPriceZoneUpperBreakedUp = false;
bool isVerificationPriceZoneLowerBreakedDown = false;
bool hasVerificationPriceZone = x121SMCVerificationPriceZone.IsValid();
//
if (!hasVerificationPriceZone)
{
//
hasVerificationPriceZone = CalculatePriceInsideZones(
x121SMCVerificationPriceZone,
verificationState,
cBar //
);
}
//
if (hasVerificationPriceZone)
{
//
bool hasBoundary = x121SMCVerificationPriceZone.GetBoundary(
X_DIRECTION_ALL,
verificationPriceZoneUpper,
verificationPriceZoneLower //
);
//
if (hasBoundary)
{
//
XOHCL iBar;
bool isInited = iBar.Init(
symbol,
verificationXConditions.period,
cIndex //
);
//
isValidVerificationPriceZoneForPeak =
verificationXConditions.peaksBuffer[1] <= verificationPriceZoneUpper &&
verificationXConditions.peaksBuffer[1] >= verificationPriceZoneLower;
//
isValidVerificationPriceZoneForVale =
verificationXConditions.valesBuffer[1] <= verificationPriceZoneUpper &&
verificationXConditions.valesBuffer[1] >= verificationPriceZoneLower;
//
isVerificationPriceZoneLowerRejected =
isInited &&
IsBarReject(
verificationPriceZoneLower,
X_DIRECTION_BULLISH,
iBar //
);
//
isVerificationPriceZoneUpperRejected =
isInited &&
IsBarReject(
verificationPriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
//
isVerificationPriceZoneUpperBreakedUp =
isInited &&
IsBarBreak(
verificationPriceZoneUpper,
X_DIRECTION_BULLISH,
iBar //
);
//
isVerificationPriceZoneLowerBreakedDown =
isInited &&
IsBarBreak(
verificationPriceZoneUpper,
X_DIRECTION_BEARISH,
iBar //
);
}
}
//
// Here We Have all Requirements for Guarding Positions ...
//
// Close ...
// TODO: Implement Force Close Position ...
//
// Close All ...
//
MqlDateTime mDate;
bool hasDate = TimeCurrent(mDate);
if (hasDate) {
//
ENUM_X_WEEK_DAYS dayOfWeek = GetDayOfWeek();
//
bool isWeekend = dayOfWeek == X_WEEK_DAY_FRIDAY;
if (isWeekend && mDate.hour == 23 && mDate.min == 30) {
//
X121SMCGuard guard;
//
guard.time = cTime;
guard.symbol = symbol;
guard.provider = provider;
guard.action = X_121_SMC_GUARD_ACTION_CLOSE_ALL;
//
AddGuard(
guard,
guards //
);
}
}
//
// Close Longs ...
if (hasLongs)
{
//
bool hasGuard =
//
hasDecisionBearishTrend
//
;
//
if (hasGuard)
{
//
X121SMCGuard guard;
//
guard.time = cTime;
guard.symbol = symbol;
guard.provider = provider;
guard.action = X_121_SMC_GUARD_ACTION_CLOSE_LONGS;
//
AddGuard(
guard,
guards //
);
}
}
//
// Close Shorts ...
if (hasShorts)
{
//
bool hasGuard =
//
hasDecisionBullishTrend
//
;
//
if (hasGuard)
{
//
X121SMCGuard guard;
//
guard.time = cTime;
guard.symbol = symbol;
guard.provider = provider;
guard.action = X_121_SMC_GUARD_ACTION_CLOSE_SHORTS;
//
AddGuard(
guard,
guards //
);
}
}
//
// Partial Close ...
//
if (hasLongs)
{
//
bool hasGuard =
//
hasTriggerBearishTrend ||
!hasDecisionBullishTrend
//
;
//
if (hasGuard)
{
//
for (int i = 0; i < longsCount; i++)
{
//
XPosition iPosition = ownLongPositions[i];
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE;
//
iGuard.ticket = iPosition.ticket;
iGuard.volumeMultiplier = 0.5;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
if (hasShorts)
{
//
bool hasGuard =
//
hasTriggerBullishTrend ||
!hasDecisionBearishTrend
//
;
//
if (hasGuard)
{
//
for (int i = 0; i < shortsCount; i++)
{
//
XPosition iPosition = ownShortPositions[i];
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE;
//
iGuard.ticket = iPosition.ticket;
iGuard.volumeMultiplier = 0.5;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
// Trail Stop ...
//
if (hasLongs)
{
//
bool hasGuard =
//
(hasConsolidationZone &&
isConsolidationZoneBreaked &&
isConsolidationZoneBreakedUp)
//
;
//
if (hasGuard)
{
//
for (int i = 0; i < longsCount; i++)
{
//
XPosition iPosition = ownLongPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
// Price Must Greater than Zone Lower ...
if (iPosition.price < consolidationZoneLower)
{
continue;
}
//
// Position Entry Must Lesser than Zone Lower ...
if (iPosition.entry > consolidationZoneLower)
{
continue;
}
//
// Position SL must be Zero or Lesser than Zone Lower ...
if (iPosition.sl != 0 &&
iPosition.sl > consolidationZoneLower)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP;
//
iGuard.ticket = iPosition.ticket;
//
iGuard.sl = consolidationZoneLower;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
if (hasShorts)
{
//
bool hasGuard =
//
(hasConsolidationZone &&
isConsolidationZoneBreaked &&
isConsolidationZoneBreakedDown)
//
;
//
if (hasGuard)
{
//
for (int i = 0; i < shortsCount; i++)
{
//
XPosition iPosition = ownShortPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
// Price Must Lesser than Zone Upper ...
if (iPosition.price > consolidationZoneUpper)
{
continue;
}
//
// Position Entry Must Greater than Zone Upper ...
if (iPosition.entry < consolidationZoneUpper)
{
continue;
}
//
// Position SL must be Zero or Greater than Zone Upper ...
if (iPosition.sl != 0 &&
iPosition.sl < consolidationZoneUpper)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP;
//
iGuard.ticket = iPosition.ticket;
//
iGuard.sl = consolidationZoneUpper;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
// Trail Target ...
// TODO: Implement Target Trail ...
//
if (hasLongs)
{
}
//
if (hasShorts)
{
}
//
// Hedge ...
// TODO: Implement Hedge ...
//
count = ArraySize(guards);
result = IsValidSize(count);
//
return result;
}