Add trailing stop to BO EA v5.1 — all 17 presets enabled
ApplyTrailingStop() runs every 5s on open positions: activates after 1000 pts (100 pips) of profit, trails SL 500 pts (50 pips) behind price. SL only moves in the profitable direction, never backwards. New inputs: InpUseTrailingStop, InpTrailStart, InpTrailStop (all presets set to true/1000/500). EA version bumped 5.0 → 5.1. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2024, Garfield Heron"
|
||||
#property link "https://fetcherpay.com"
|
||||
#property version "5.0"
|
||||
#property version "5.1"
|
||||
|
||||
#include <Trade\Trade.mqh>
|
||||
#include <Trade\PositionInfo.mqh>
|
||||
|
||||
#define VERSION "Version 5.0 Smart Grid Breakout BO MT5"
|
||||
#define VERSION "Version 5.1 Smart Grid Breakout BO MT5"
|
||||
#define MAX_TRADES 600
|
||||
#define MAX_LOG_TRADES 1200
|
||||
|
||||
@@ -71,6 +71,12 @@ input string BreakevenSettings = "=== Breakeven ===";
|
||||
input bool InpUseBreakeven = false; // Move remaining SLs to breakeven after first TP hits
|
||||
input int InpBreakevenBufferPoints = 5; // Spread cushion above entry
|
||||
|
||||
//--- Trailing Stop (5.1)
|
||||
input string TrailingSettings = "=== Trailing Stop ===";
|
||||
input bool InpUseTrailingStop = false; // Enable trailing stop on open positions
|
||||
input int InpTrailStart = 1000; // Points of profit before trail activates
|
||||
input int InpTrailStop = 500; // Trail distance behind price (points)
|
||||
|
||||
//--- Correlation Cap (4.3)
|
||||
input string CorrelationSettings = "=== Correlation Cap ===";
|
||||
input int InpMaxLongSymbols = 0; // Max distinct symbols with open longs (0 = unlimited)
|
||||
@@ -806,6 +812,56 @@ void ApplyBreakeven()
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Trailing Stop (5.1) |
|
||||
//+------------------------------------------------------------------+
|
||||
void ApplyTrailingStop()
|
||||
{
|
||||
if(!InpUseTrailingStop) return;
|
||||
if(InpTrailStart <= 0 || InpTrailStop <= 0) return;
|
||||
|
||||
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
|
||||
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
|
||||
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
|
||||
double trailStart = InpTrailStart * point;
|
||||
double trailDist = InpTrailStop * point;
|
||||
|
||||
for(int i = PositionsTotal() - 1; i >= 0; i--)
|
||||
{
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if(ticket == 0) continue;
|
||||
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
|
||||
if(PositionGetInteger(POSITION_MAGIC) != MagicNum) continue;
|
||||
|
||||
long ptype = PositionGetInteger(POSITION_TYPE);
|
||||
double open = PositionGetDouble(POSITION_PRICE_OPEN);
|
||||
double curSL = PositionGetDouble(POSITION_SL);
|
||||
double tp = PositionGetDouble(POSITION_TP);
|
||||
double newSL;
|
||||
|
||||
if(ptype == POSITION_TYPE_BUY)
|
||||
{
|
||||
if(bid - open < trailStart) continue; // profit threshold not reached
|
||||
newSL = NormalizeDouble(bid - trailDist, _Digits);
|
||||
if(newSL <= curSL) continue; // SL already at or ahead of trail
|
||||
}
|
||||
else if(ptype == POSITION_TYPE_SELL)
|
||||
{
|
||||
if(open - ask < trailStart) continue;
|
||||
newSL = NormalizeDouble(ask + trailDist, _Digits);
|
||||
if(curSL > 0 && newSL >= curSL) continue; // SL already at or ahead of trail
|
||||
}
|
||||
else continue;
|
||||
|
||||
if(!trade.PositionModify(ticket, newSL, tp))
|
||||
PrintS("Trail modify failed #" + IntegerToString((int)ticket) +
|
||||
" err=" + IntegerToString((int)trade.ResultRetcode()));
|
||||
else
|
||||
PrintS("Trail SL → " + DoubleToString(newSL, _Digits) +
|
||||
" #" + IntegerToString((int)ticket));
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Count partial TP fills since cycle start |
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -1184,6 +1240,8 @@ int OnInit()
|
||||
|
||||
PrintS(VERSION + " initialized — Magic=" + IntegerToString(MagicNum) +
|
||||
" SL=" + (InpUseStopLoss ? "ON(" + IntegerToString(StopLoss) + ")" : "OFF") +
|
||||
" Trail=" + (InpUseTrailingStop ? "ON(start=" + IntegerToString(InpTrailStart) +
|
||||
"/dist=" + IntegerToString(InpTrailStop) + "pts)" : "OFF") +
|
||||
" AdaptiveEntry=" + (InpAdaptiveEntry ? "ON" : "OFF"));
|
||||
return INIT_SUCCEEDED;
|
||||
}
|
||||
@@ -1267,6 +1325,7 @@ void OnTick()
|
||||
CalcAvgPrice();
|
||||
cyclePartialTPCount = CountPartialTPsSinceCycle();
|
||||
ApplyBreakeven();
|
||||
ApplyTrailingStop();
|
||||
lastStatsTime = TimeCurrent();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user