This commit is contained in:
2025-01-15 22:09:21 +03:30
parent 341992374c
commit 9b307ae6e6
12 changed files with 3448 additions and 671 deletions
+703
View File
@@ -0,0 +1,703 @@
///////////////////////////////////////////////////////
//
// 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"
//
//
XConsolidationZone x121SMCConsolidationZone;
bool DetectX121SMCGuards(
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();
string provider = ToString(X_121_SMC_PROVIDER_XSTRPV);
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();
}
}
//
// Here We Have all Requirements for Guarding Positions ...
//
// Close ...
//
// Close All ...
//
// 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
//
;
//
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
//
;
//
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];
//
if (iPosition.profit <= 0)
{
continue;
}
//
if (iPosition.price < 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];
//
if (iPosition.profit <= 0)
{
continue;
}
//
if (iPosition.price > 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 ...
//
if (hasLongs)
{
}
//
if (hasShorts)
{
}
//
// Hedge ...
//
count = ArraySize(guards);
result = IsValidSize(count);
//
return result;
}
+151 -17
View File
@@ -36,8 +36,7 @@ const string X121SMCStrategyToken = "X121SMC";
//
const string X121SMCXTestToken = "X121XTest";
const string X121SMCXOBGPVToken = "X121XOBGPV";
const string X121SMCXOBSPVToken = "X121XOBSPV";
const string X121SMCXPZToken = "X121XPZ";
const string X121SMCXSTRPVToken = "X121XSTRPV";
//
@@ -47,11 +46,25 @@ enum ENUM_X_121_SMC_PROVIDERS
//
X_121_SMC_PROVIDER_NONE,
X_121_SMC_PROVIDER_XTEST,
X_121_SMC_PROVIDER_XOBGPV,
X_121_SMC_PROVIDER_XOBSPV,
X_121_SMC_PROVIDER_XPZ,
X_121_SMC_PROVIDER_XSTRPV,
};
//
// Guard Actions ...
enum ENUM_X_121_SMC_GUARD_ACTIONS
{
X_121_SMC_GUARD_ACTION_NONE, // Nothing to Do
X_121_SMC_GUARD_ACTION_CLOSE, // Close Specified Position
X_121_SMC_GUARD_ACTION_CLOSE_ALL, // Close All Positions
X_121_SMC_GUARD_ACTION_CLOSE_LONGS, // Close All Long Positions
X_121_SMC_GUARD_ACTION_CLOSE_SHORTS, // Close All Short Position
X_121_SMC_GUARD_ACTION_PARTIAL_CLOSE, // Partial Close Specified Position
X_121_SMC_GUARD_ACTION_TRAIL_STOP, // Trail Stop of Specified Position
X_121_SMC_GUARD_ACTION_TRAIL_TARGET, // Trail Target of Specified Position
X_121_SMC_GUARD_ACTION_HEDGE, // Hedge Specified Positions
};
//
// Each Market Cycle (Time Frame) Conditions
// Model as this Structure ...
@@ -506,6 +519,136 @@ struct X121SMCStrategyConditions
//
};
//
// Model a Guard Action ...
struct X121SMCGuard
{
//
// Props ...
ENUM_X_121_SMC_GUARD_ACTIONS action;
datetime time;
//
string symbol;
string provider;
//
// Specified Position ...
ulong ticket;
//
// Partial Close ...
double volumeMultiplier;
//
// SL Trial ...
double sl;
//
// TP Trial ...
double tp;
//
// Constructor ...
X121SMCGuard()
{
Clean();
}
//
// Tools ...
/**
* Cleanup ...
*/
void Clean()
{
//
time = NULL;
action = X_121_SMC_GUARD_ACTION_NONE;
//
symbol = NULL;
provider = NULL;
//
ticket = 0;
//
volumeMultiplier = 0;
//
sl = 0;
tp = 0;
}
/**
* Validate ...
*
* @return ( bool )
*/
bool IsValid()
{
//
bool result = false;
//
result =
//
IsValid(time) &&
IsValid(symbol) &&
IsValid(provider) &&
action != X_121_SMC_GUARD_ACTION_NONE
//
;
if (!result)
{
return result;
}
//
// Validate Model Based On Specified Guard Actions ...
//
return result;
}
};
/**
* Add Guard to Collection ...
*
* @param guard: X121SMCGuard instance ...
* @param guards: X121SMCGuard instance Collection ...
*
* @return ( int )
*/
int AddGuard(
X121SMCGuard &guard,
X121SMCGuard &guards[] //
)
{
//
int result = 0;
//
if (!guard.IsValid())
{
return result;
}
//
AddRef(
guard,
guards //
);
//
result = ArraySize(guards);
//
return result;
}
/**
* Draw X121 SMC Conditions ...
*
@@ -678,13 +821,8 @@ string ToString(ENUM_X_121_SMC_PROVIDERS value)
break;
//
case X_121_SMC_PROVIDER_XOBGPV:
result = X121SMCXOBGPVToken;
break;
//
case X_121_SMC_PROVIDER_XOBSPV:
result = X121SMCXOBSPVToken;
case X_121_SMC_PROVIDER_XPZ:
result = X121SMCXPZToken;
break;
//
@@ -720,13 +858,9 @@ ENUM_X_121_SMC_PROVIDERS ParseProvider(string value)
{
result = X_121_SMC_PROVIDER_XTEST;
}
else if (Contains(X121SMCXOBGPVToken, value))
else if (Contains(X121SMCXPZToken, value))
{
result = X_121_SMC_PROVIDER_XOBGPV;
}
else if (Contains(X121SMCXOBSPVToken, value))
{
result = X_121_SMC_PROVIDER_XOBSPV;
result = X_121_SMC_PROVIDER_XPZ;
}
else if (Contains(X121SMCXSTRPVToken, value))
{