apply trail stop and break even ...

This commit is contained in:
2025-02-11 23:28:28 +03:30
parent fe129c3288
commit 4a70a3ef4f
11 changed files with 2682 additions and 1063 deletions
@@ -4582,4 +4582,893 @@ else if (isPriceInsideDemandZone)
/////////////////////////////////////////////////////////////////////
// //
// bool isPriceAboveUpper =
// //
// decisionZBar.high < conditions.workingZone.zoneUpper &&
// decisionCBar.high < conditions.workingZone.zoneUpper &&
// //
// decisionZBar.low > conditions.workingZone.upper &&
// decisionCBar.low > conditions.workingZone.upper
// //
// ;
// if (isPriceAboveUpper)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_HIGH;
// }
// //
// bool isPriceInQuarterOne =
// //
// decisionZBar.high < conditions.workingZone.upper &&
// decisionCBar.high < conditions.workingZone.upper &&
// //
// decisionZBar.low > conditions.workingZone.quarter1 &&
// decisionCBar.low > conditions.workingZone.quarter1
// //
// ;
// if (isPriceInQuarterOne)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_REGULAR;
// }
// //
// bool isPriceInQuarterTwo =
// //
// decisionZBar.high < conditions.workingZone.quarter1 &&
// decisionCBar.high < conditions.workingZone.quarter1 &&
// //
// decisionZBar.low > conditions.workingZone.quarter2 &&
// decisionCBar.low > conditions.workingZone.quarter2
// //
// ;
// if (isPriceInQuarterTwo)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_LOW;
// }
// //
// bool isPriceInQuarterThree =
// //
// decisionZBar.high < conditions.workingZone.quarter2 &&
// decisionCBar.high < conditions.workingZone.quarter2 &&
// //
// decisionZBar.low > conditions.workingZone.quarter3 &&
// decisionCBar.low > conditions.workingZone.quarter3
// //
// ;
// if (isPriceInQuarterThree)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_LOW;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// }
// //
// bool isPriceInQuarterFour =
// //
// decisionZBar.high < conditions.workingZone.quarter3 &&
// decisionCBar.high < conditions.workingZone.quarter3 &&
// //
// decisionZBar.low > conditions.workingZone.lower &&
// decisionCBar.low > conditions.workingZone.lower
// //
// ;
// if (isPriceInQuarterFour)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_REGULAR;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// }
// //
// bool isPriceBelowLower =
// //
// decisionZBar.high < conditions.workingZone.lower &&
// decisionCBar.high < conditions.workingZone.lower &&
// //
// decisionZBar.low > conditions.workingZone.zoneLower &&
// decisionCBar.low > conditions.workingZone.zoneLower
// //
// ;
// if (isPriceBelowLower)
// {
// //
// bullishPriority = X121SMC_XSUPDEMOBS_PRIORITY_HIGH;
// bearishPriority = X121SMC_XSUPDEMOBS_PRIORITY_NONE;
// }
/////////////////////////////////////////////////////////////////////
//
//
//
//
// Close ...
if (!ignoreCloseOnTPNearGuard)
{
//
double tpCloseDiff = (15 * points);
for (int i = 0; i < count; i++)
{
//
XPosition iPosition = ownPositions[i];
//
// Position Must be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
bool isPricePassed = MathAbs(iPosition.tp - iPosition.price) <= tpCloseDiff;
if (!isPricePassed)
{
continue;
}
//
bool isLong = IsLong(iPosition.type);
//
bool iHasGuard =
//
isLong
? //
false
//
: //
false
//
;
if (iHasGuard)
{
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE;
//
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
if (hasLongs)
{
//
if (!ignoreCloseLongsOnBearishTrendGuard)
{
//
bool hasGuard = false; // hasDecisionBearishTrend;
//
if (hasGuard)
{
//
for (int i = 0; i < longsCount; i++)
{
//
XPosition iPosition = ownLongPositions[i];
//
// Position must be in Draw Down ...
if (iPosition.profit > 0)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE;
//
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
}
//
if (hasShorts)
{
//
if (!ignoreCloseShortsOnBullishTrendGuard)
{
//
bool hasGuard = false; // hasDecisionBullishTrend;
//
if (hasGuard)
{
//
for (int i = 0; i < shortsCount; i++)
{
//
XPosition iPosition = ownShortPositions[i];
//
// Position must be in Draw Down ...
if (iPosition.profit > 0)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_CLOSE;
//
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
}
//
// Close All ...
//
// Close All Weekend ...
if (!ignoreCloseAllWeekendGuard)
{
//
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 =
//
false
//
// hasAnalyseBearishTrend &&
// 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 =
//
false
//
// hasAnalyseBullishTrend &&
// 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 (!ignorePartialCloseOnTrendChangeGuard)
{
//
if (hasLongs)
{
//
bool hasGuard =
//
false
//
// 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 =
//
false
//
// 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 (!ignoreTrailStopGuard)
{
//
if (hasLongs)
{
//
bool hasTrendGuard = false; // hasDecisionBearishTrend;
//
bool hasConsolidationGuard =
(hasConsolidationZone &&
isConsolidationZoneBreaked &&
isConsolidationZoneBreakedUp);
//
bool hasDecisionPriceZoneGuard =
false;
// (hasDecisionPriceZone &&
// isDecisionPriceZoneBullish &&
// isDecisionPriceZoneValidForVale &&
// isDecisionPriceZoneUpperBreakedUp);
//
bool hasAnalysePriceZoneGuard =
false;
// (hasAnalysePriceZone &&
// isAnalysePriceZoneBullish &&
// isAnalysePriceZoneValidForVale &&
// isAnalysePriceZoneUpperBreakedUp);
//
bool hasVerificationPriceZoneGuard =
false;
// (hasVerificationPriceZone &&
// isVerificationPriceZoneBullish &&
// isVerificationPriceZoneValidForVale &&
// isVerificationPriceZoneUpperBreakedUp);
//
bool hasGuard =
//
hasTrendGuard ||
hasConsolidationGuard ||
hasDecisionPriceZoneGuard ||
hasAnalysePriceZoneGuard ||
hasVerificationPriceZoneGuard
//
;
//
double guardStop =
hasConsolidationGuard
? consolidationZoneLower
: hasDecisionPriceZoneGuard
? 0 // decisionPriceZoneLower
: hasAnalysePriceZoneGuard
? 0 // analysePriceZoneLower
: hasVerificationPriceZoneGuard
? 0 // verificationPriceZoneLower
: 0;
//
if (hasGuard)
{
//
for (int i = 0; i < longsCount; i++)
{
//
XPosition iPosition = ownLongPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
if (hasTrendGuard)
{
// guardStop = decisionXConditions.strBuffer[1] - decisionXConditions.atrBuffer[1];
}
//
// Price Must Greater than Zone Lower ...
if (iPosition.price < guardStop)
{
continue;
}
//
// Position Entry Must Lesser than Zone Lower ...
if (iPosition.entry > guardStop)
{
continue;
}
//
// Position SL must be Zero or Lesser than Zone Lower ...
if (iPosition.sl != 0 &&
iPosition.sl > guardStop)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP;
//
iGuard.sl = guardStop;
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
if (hasShorts)
{
//
bool hasTrendGuard = false; // hasDecisionBullishTrend;
//
bool hasConsolidationGuard =
(hasConsolidationZone &&
isConsolidationZoneBreaked &&
isConsolidationZoneBreakedDown);
//
bool hasDecisionPriceZoneGuard =
false;
// (hasDecisionPriceZone &&
// isDecisionPriceZoneBearish &&
// isDecisionPriceZoneValidForPeak &&
// isDecisionPriceZoneLowerBreakedDown);
//
bool hasAnalysePriceZoneGuard =
false;
// (hasAnalysePriceZone &&
// isAnalysePriceZoneBearish &&
// isAnalysePriceZoneValidForPeak &&
// isAnalysePriceZoneLowerBreakedDown);
//
bool hasVerificationPriceZoneGuard =
false;
// (hasVerificationPriceZone &&
// isVerificationPriceZoneBearish &&
// isVerificationPriceZoneValidForPeak &&
// isVerificationPriceZoneLowerBreakedDown);
//
bool hasGuard =
//
hasTrendGuard ||
hasConsolidationGuard ||
hasDecisionPriceZoneGuard ||
hasAnalysePriceZoneGuard ||
hasVerificationPriceZoneGuard
//
;
//
double guardStop =
hasConsolidationGuard
? consolidationZoneUpper
: hasDecisionPriceZoneGuard
? 0 // decisionPriceZoneUpper
: hasAnalysePriceZoneGuard
? 0 // analysePriceZoneUpper
: hasVerificationPriceZoneGuard
? 0 // verificationPriceZoneUpper
: 0;
//
if (hasGuard)
{
//
for (int i = 0; i < shortsCount; i++)
{
//
XPosition iPosition = ownShortPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
if (hasTrendGuard)
{
// guardStop = decisionXConditions.strBuffer[1] + decisionXConditions.atrBuffer[1];
}
//
// Price Must Lesser than Zone Upper ...
if (iPosition.price > guardStop)
{
continue;
}
//
// Position Entry Must Greater than Zone Upper ...
if (iPosition.entry < guardStop)
{
continue;
}
//
// Position SL must be Zero or Greater than Zone Upper ...
if (iPosition.sl != 0 &&
iPosition.sl < guardStop)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_STOP;
//
iGuard.sl = guardStop;
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
}
//
// Trail Target ...
if (!ignoreTrailTargetGuard)
{
//
if (hasLongs)
{
//
bool hasAnalyseGuard =
//
false
//
// (hasAnalysePriceZone &&
// isAnalysePriceZoneValidForPeak &&
// isAnalysePriceZoneUpperRejected)
//
;
//
bool hasVerificationGuard =
//
false
//
// (hasVerificationPriceZone &&
// isVerificationPriceZoneValidForPeak &&
// isVerificationPriceZoneUpperRejected)
//
;
//
bool hasGuard =
//
hasAnalyseGuard ||
hasVerificationGuard
//
;
//
double guardTP =
hasAnalyseGuard
? 0 // analysePriceZoneUpper
: hasVerificationGuard
? 0 // verificationPriceZoneUpper
: 0;
//
if (hasGuard)
{
//
for (int i = 0; i < longsCount; i++)
{
//
XPosition iPosition = ownLongPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_TARGET;
//
iGuard.tp = guardTP;
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
//
if (hasShorts)
{
//
bool hasAnalyseGuard =
//
false
//
// (hasAnalysePriceZone &&
// isAnalysePriceZoneValidForVale &&
// isAnalysePriceZoneLowerRejected)
//
;
//
bool hasVerificationGuard =
//
false
//
// (hasVerificationPriceZone &&
// isVerificationPriceZoneValidForVale &&
// isVerificationPriceZoneLowerRejected)
//
;
//
bool hasGuard =
//
hasAnalyseGuard ||
hasVerificationGuard
//
;
//
double guardTP =
hasAnalyseGuard
? 0 // analysePriceZoneLower
: hasVerificationGuard
? 0 // verificationPriceZoneLower
: 0;
//
if (hasGuard)
{
//
for (int i = 0; i < shortsCount; i++)
{
//
XPosition iPosition = ownShortPositions[i];
//
// Position Must Be In Profit ...
if (iPosition.profit <= 0)
{
continue;
}
//
X121SMCGuard iGuard;
//
iGuard.time = cTime;
iGuard.symbol = symbol;
iGuard.provider = provider;
iGuard.action = X_121_SMC_GUARD_ACTION_TRAIL_TARGET;
//
iGuard.tp = guardTP;
iGuard.ticket = iPosition.ticket;
//
AddGuard(
iGuard,
guards //
);
}
}
}
}
//
// Hedge ...
if (!ignoreHedgeGuard)
{
}
/////////////////////////////////////////////////////////