Ea order correct tp and sl issue

 
Hi everyone,


I'm programming an expert advisor for mt4 but when i run it on icmarket or any broker and the bot start opening a trade, it is not setting up the right price for the tp and sl. For exemple, imagine it opened a buy order at the price 1900 and i fix the tp at 30. It will set up randomly like for example sometimes at 1910 or sometimes at 1945 but almost never at the good price. I'm aware that slippage and spread can be the cause but i'm using it on a raw spread account (0 pips on icmarket) and slippage can't be this high ! Knowing that i defined in the ordersend function slippage =1 and if condition maxspread=1. So why does the tp and sl are not the correct one when opening an order ? But in backtest there is no problem, tp and sl are always the same when trade is opened.

The part of the code is the following one :

[...]

int maxspread = 1*Point();

double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK); 

double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);

double Spread=Ask-Bid;

if(Spread<=maxspread){

double LotSize = 1.0; 

int Slippage = 1;

int TakeProfit = 30; 

int StopLoss = 12; 


OrderSend(_Symbol, OP_BUY, LotSize, Ask, Slippage, Bid - StopLoss * Point(), Ask + TakeProfit * Point(), "My order", MagicNumber, 0, Green); 

}


It is the first time for me to post on the forum so please be indulgent if anything is wrong. Thank you very much for any help

 

Hi

I recommend normalizing the prices you use or even add some standardization based on ticks – to be sure that the prices you use are properly managed and allowed by the broker. So use NormalizeDouble (sl, _Digits).
But more important is to set sl/tp based on open price – so calculate both SL/TP based on the open price – do not change it to Bid/Ask prices. For example for this buy trade set SL as NormalizeDouble(Ask-StopLoss*Point(), Digits()) and tp as NormalizeDouble(Ask+TakeProfit*Point(), Digits()) – so use Ask in both calculations. If spread would be “0” the prices would be the same but I assume this is not the real spread here since you get those wrong prices as a result.

Best Regards

 
Marzena Maria Szmit #:

Hi

I recommend normalizing the prices you use or even add some standardization based on ticks – to be sure that the prices you use are properly managed and allowed by the broker. So use NormalizeDouble (sl, _Digits).
But more important is to set sl/tp based on open price – so calculate both SL/TP based on the open price – do not change it to Bid/Ask prices. For example for this buy trade set SL as NormalizeDouble(Ask-StopLoss*Point(), Digits()) and tp as NormalizeDouble(Ask+TakeProfit*Point(), Digits()) – so use Ask in both calculations. If spread would be “0” the prices would be the same but I assume this is not the real spread here since you get those wrong prices as a result.

Best Regards

Ok i'm testing that. Thank you forbyour reply ! 
Reason: