//+------------------------------------------------------------------+ //| Expert advisor: CandlePatternRiskTrailingEA_M5.mq5 | //| Versie voor M5 timeframe | //| Stop-loss en trailing in euro's, max 1 Buy & 1 Sell positie | //+------------------------------------------------------------------+ #property strict //--- inputs input int GreenCandlesForBuy = 3; // aantal groene candles voor buy input int RedCandlesForSell = 3; // aantal rode candles voor sell input double RiskInEuro = 22.0; // stop-loss in euro input double TrailingInEuro = 5.0; // trailing-afstand in euro input double TrailingStartEuro = 5.0; // start trailing pas na € winst input double LotSize = 0.01; // vaste lotgrootte input bool EnableTrailingStop = true; // trailing aan/uit //--- globals datetime lastBarTime = 0; int minStopsLevel; double tickValue, tickSize; //+------------------------------------------------------------------+ //| Expert initialization | //+------------------------------------------------------------------+ int OnInit() { minStopsLevel = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL); tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE); Print("INIT: minStopsLevel=", minStopsLevel, " tickValue=", tickValue, " tickSize=", tickSize); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { int needed = MathMax(GreenCandlesForBuy, RedCandlesForSell) + 2; MqlRates rates[]; if(CopyRates(_Symbol, PERIOD_M5, 0, needed, rates) < needed) return; ArraySetAsSeries(rates, true); if(rates[0].time == lastBarTime) return; lastBarTime = rates[0].time; bool hasBuy = false, hasSell = false; for(int i=0; i= r[i].open) return false; } return true; } //+------------------------------------------------------------------+ void PlaceOrder(ENUM_ORDER_TYPE type) { double entry = (type == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID); double ptsSL = RiskInEuro / (tickValue * LotSize) * (tickSize / _Point); ptsSL = NormalizeDouble(ptsSL, 0); int guard = minStopsLevel + 1; if(ptsSL < guard) { Print(" ptsSL <",guard," -> forceren naar ",guard); ptsSL = guard; } double sl_price = (type==ORDER_TYPE_BUY) ? entry - ptsSL * _Point : entry + ptsSL * _Point; sl_price = NormalizeDouble(sl_price, _Digits); MqlTradeRequest rq; MqlTradeResult rs; ZeroMemory(rq); ZeroMemory(rs); rq.action = TRADE_ACTION_DEAL; rq.symbol = _Symbol; rq.volume = LotSize; rq.type = type; rq.price = NormalizeDouble(entry, _Digits); rq.sl = sl_price; rq.tp = 0; rq.deviation = 10; rq.magic = 20250607; rq.type_filling = ORDER_FILLING_IOC; if(!OrderSend(rq, rs)) Print("OrderSend failed: ", rs.retcode); else if(rs.retcode != TRADE_RETCODE_DONE) Print("Order rejected: ", rs.retcode, " / ", rs.comment); else Print("Order placed: ", EnumToString(type), " entry=", DoubleToString(rq.price,_Digits), " SL=", DoubleToString(sl_price,_Digits)); } //+------------------------------------------------------------------+ void ApplyTrailing() { for(int i=0; i curSL) || (t==POSITION_TYPE_SELL && newSL < curSL)) { MqlTradeRequest rq; MqlTradeResult rs; ZeroMemory(rq); ZeroMemory(rs); rq.action = TRADE_ACTION_SLTP; rq.position = tk; rq.sl = newSL; rq.tp = 0; if(!OrderSend(rq, rs)) Print("Trailing SL failed: ", rs.retcode); else Print("Trailing SL updated pos#",tk, " to ", DoubleToString(newSL,_Digits)); } } } //+------------------------------------------------------------------+