There are no telepaths here. Show the code. Then someone can help you.
Konstantin Nikitin:
/+------------------------------------------------------------------+ //| Normalize Price | //+------------------------------------------------------------------+ double NormalizePrice(double price) { double mTickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE); return(NormalizeDouble(MathRound(price/mTickSize)*mTickSize,_Digits)); } //+------------------------------------------------------------------+ //| Modify an Open Position | //+------------------------------------------------------------------+ void ModifyPosition(ulong ticket, string symbol, double stopLoss, double takeProfit) { // Validate stops level int type = (int) PositionGetInteger(POSITION_TYPE); double bid = SymbolInfoDouble(symbol,SYMBOL_BID); double ask = SymbolInfoDouble(symbol,SYMBOL_ASK); int stopLevel = (int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL); if(type == POSITION_TYPE_BUY) { double dif = NormalizeDouble((bid - stopLoss) / Poin, _Digits); if (dif < stopLevel) stopLoss = NormalizeDouble(stopLoss - ((stopLevel - dif) * Poin),_Digits); } else { double dif = NormalizeDouble((stopLoss - ask) / Poin, _Digits); if (dif < stopLevel) stopLoss = NormalizeDouble(stopLoss + ((stopLevel - dif) * Poin),_Digits); } // Prepare a request MqlTradeRequest request={0}; request.action = TRADE_ACTION_SLTP; // Type of trade operation request.position = ticket; // Ticket of the position request.symbol = symbol; // Symbol request.sl = stopLoss; // Stop Loss of the position request.tp = takeProfit; // Take Profit of the position request.magic = MagicNumber; // MagicNumber of the position // Send a trade request MqlTradeResult result={0}; bool val = OrderSend(request,result); // Write the server reply to log Print(__FUNCTION__,":",result.comment); if(result.retcode==10016) Print(result.bid,result.ask,result.price); if (val && ShowAlert) Alert(Symbol(), " M", Period(), ": Stop Loss Order Modified successfully."); if (!val && ShowAlert) { int error = GetLastError(); Alert("OrderModify failed with error # ", error , " : ", ErrorDescription(error)); } } //+------------------------------------------------------------------+ //| Trailing positions | //+------------------------------------------------------------------+ void TrailingPositions() { // ***** Go through all positions ***** for(int i=0; i<PositionsTotal(); i++) { // ***** Select order by position ***** if (PositionSelectByTicket(PositionGetTicket(i))) { // ***** Check Trail all positions and Symbol ***** if (TrailAllPositions || PositionGetSymbol(i)==Symbol()) { double bid, ask; double open = PositionGetDouble(POSITION_PRICE_OPEN); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); // ***** Trailing Buy ***** if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { bid = SymbolInfoDouble(Symbol(),SYMBOL_BID); if (!ProfitTrailing || (sl >= open && bid - open >= TrailingStop * Poin)) { if (sl < bid - (TrailingStop + TrailingStep) * Poin) { double newStopLoss = NormalizeDouble(bid - TrailingStep * Poin, _Digits); ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss , tp); Print("Trail Buy ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(bid, _Digits)); } } } // ***** End ***** // ***** Trailing Sell ***** if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK); if (!ProfitTrailing || (sl <= open && open - ask >= TrailingStop * Poin)) { if (sl > ask + (TrailingStop + TrailingStep) * Poin) { double newStopLoss = NormalizeDouble(ask + TrailingStep * Poin, _Digits); ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss, tp); Print("Trail Sell ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(ask, _Digits)); } } } // ***** End ***** } // ***** End ***** } // ***** End ***** } // ***** End ***** }
//+------------------------------------------------------------------+ //| Normalize Price | //+------------------------------------------------------------------+ double NormalizePrice(double price) { double mTickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE); return(NormalizeDouble(MathRound(price/mTickSize)*mTickSize,_Digits)); } //+------------------------------------------------------------------+ //| Modify an Open Position | //+------------------------------------------------------------------+ void ModifyPosition(ulong ticket, string symbol, double stopLoss, double takeProfit) { // Validate stops level int type = (int)PositionGetInteger(POSITION_TYPE); double bid = SymbolInfoDouble(symbol,SYMBOL_BID); double ask = SymbolInfoDouble(symbol,SYMBOL_ASK); double point = SymbolInfoDouble(symbol,SYMBOL_POINT); int stopLevel = (int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL)+1; switch(type) { case POSITION_TYPE_BUY : if (stopLevel > (bid - stopLoss) / point) stopLoss = NormalizeDouble(bid - stopLevel * point,_Digits); break; case POSITION_TYPE_SELL : if (stopLevel > (stopLoss - ask) / point) stopLoss = NormalizeDouble(ask + stopLevel * point,_Digits); break; } // Prepare a request MqlTradeRequest request={0}; request.action = TRADE_ACTION_SLTP; // Type of trade operation request.position = ticket; // Ticket of the position request.symbol = symbol; // Symbol request.sl = stopLoss; // Stop Loss of the position request.tp = takeProfit; // Take Profit of the position request.magic = MagicNumber; // MagicNumber of the position // Send a trade request MqlTradeResult result={0}; bool val = OrderSend(request,result); // Write the server reply to log Print(__FUNCTION__,":",result.comment); if(result.retcode==10016) Print(result.bid,result.ask,result.price); if (val && ShowAlert) Alert(_Symbol, " M", Period(), ": Stop Loss Order Modified successfully."); if (!val && ShowAlert) { int error = GetLastError(); Alert("OrderModify failed with error # ", error , " : ", ErrorDescription(error)); } } //+------------------------------------------------------------------+ //| Trailing positions | //+------------------------------------------------------------------+ void TrailingPositions() { // ***** Go through all positions ***** for(int i=0; i<PositionsTotal(); i++) { // ***** Select order by position ***** if (PositionSelectByTicket(PositionGetTicket(i))) { // ***** Check Trail all positions and Symbol ***** if (TrailAllPositions || PositionGetSymbol(i)==_Symbol) { double bid, ask; double open = PositionGetDouble(POSITION_PRICE_OPEN); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); double point = SymbolInfoDouble(_Symbol,SYMBOL_POINT); // ***** Trailing Buy ***** if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); if (!ProfitTrailing || (sl >= open && bid - open >= TrailingStop * point)) { if (sl < bid - (TrailingStop + TrailingStep) * point) { double newStopLoss = NormalizeDouble(bid - TrailingStep * point, _Digits); ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss , tp); Print("Trail Buy ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(bid, _Digits)); } } } // ***** End ***** // ***** Trailing Sell ***** if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); if (!ProfitTrailing || (sl <= open && open - ask >= TrailingStop * point)) { if (sl > ask + (TrailingStop + TrailingStep) * point) { double newStopLoss = NormalizeDouble(ask + TrailingStep * point, _Digits); ModifyPosition(PositionGetTicket(i), PositionGetSymbol(i), newStopLoss, tp); Print("Trail Sell ", PositionGetString(POSITION_COMMENT), " Price : ", NormalizeDouble(ask, _Digits)); } } } // ***** End ***** } // ***** End ***** } // ***** End ***** } // ***** End ***** }
Konstantin Nikitin:
Thank you Konstantin it solved the SL.
now Is the volume issue with par bgp/usd on the 'testing loading EA' - can validate de EA.
Will search to limit the trade and adjust de volume.
test on EURUSD,H1 (netting) strategy tester report 86 total trades test on XAUUSD,D1 (netting) there are no trading operations test on GBPUSD,M30 (netting) 2017.02.06 08:00:40 failed request sell 15.00 GBPUSD at 1.24869 sl: 1.25069 tp: 1.24469 [Volume limit reached] 2017.02.06 08:30:00 failed request sell 15.00 GBPUSD at 1.24871 sl: 1.25071 tp: 1.24471 [Volume limit reached] 2017.02.06 09:06:59 failed request sell 15.00 GBPUSD at 1.24869 sl: 1.25069 tp: 1.24469 [Volume limit reached]

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi all
I got an issue with the Stop Loss and can't find out the cause - I got the Mt4 version running just fine however, mt5 is not able to run.
Mql5 can't HELP ..... and suggest to ask the Forum - Does any one has this issue (anoying...) and how to solved it
EA on mt4 works fine - Mt5 ...!!!
Validation state: Validation completed with errors