I´m trying to code a simple program to perform a stop loss and take profit operation through MT´s Strategy Tester without success. I´m using the MT 5.0, build 2085, and tried all kind of codes from Internet, including the ones written based on the standard library CTrade, but it doesn´t work.
The behaviour I´m facing is that the MT is triggering the Buy Stop Loss before the current price to reach the SL threshold.
The code below was taken from the book "Expert Advidor 5 - Programming for Meta Trader", by Andrew r. Young, page 83:
int OnInit()
double StopLossPorc=9.0;
double StopLossPrice=0.0;
double TakeProfitPorc=4.0;
double TakeProfitPrice = 0.0;
vPrecoAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
TakeProfitPrice = vPrecoAsk+((TakeProfitPorc)/100*vPrecoAsk);
StopLossPrice = vPrecoAsk-(StopLossPorc/100*vPrecoAsk);
request.action = TRADE_ACTION_DEAL;
request.type = ORDER_TYPE_BUY;
request.symbol = _Symbol;
request.volume = qtde_compra;
request.type_filling = ORDER_FILLING_FOK;
request.price = vPrecoAsk;
request.sl = 0;
request.tp = 0;
request.deviation = 50;
request.magic = 43;
OrderSend(request,result);
request.action = TRADE_ACTION_SLTP;
request.symbol = _Symbol;
request.sl = StopLossPrice;
request.tp = TakeProfitPrice;
}
I´ve ommited the rest of the script just for a matter of simplicity.
As can be seen in the logs through the link below, the stop loss was set to $13.16 when the buy order was sent, but the sell was performed at $14.47 based on a trigger whose the price was higher than the SL. Notice that the Low price in the respective period was $14.45, quite higher than the SL set ($13.16).
How can I code it in a way the stop loss is performed only when the current price reaches the SL defined?