English

## Problem with StopLoss and TakeProfit

 253 ffoorr 2016.02.11 11:54   I made a simple EA following MT4 recommandations. I buy on the ask, my stoploss (SL = Bid -stoploss;) and takeprofit (TP = Bid + takeprofit;) are on the Bid,    just like MT4 say it has to be, this way we take account of the spread. if my SL and TP are 100 point, it should male 10 dollar profit if the takeprofit is hit, and 10 dollar loss if the SL is hit.Backtest is on EURUSD 1 Mn, each bar, spread fixed 20 point :```extern double Lots = 0.1; extern double  TakeProfit = 100; extern double  StopLoss   = 100; extern int  MagicNumber = 15986; extern int MA_Per = 30; //+------------------------------------------------------------------+ //| Expert initialization function                                   | //+------------------------------------------------------------------+ int OnInit() {   return(INIT_SUCCEEDED);  } //+------------------------------------------------------------------+ //| Expert deinitialization function                                 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) {} //+------------------------------------------------------------------+ //| Expert tick function                                             | //+------------------------------------------------------------------+ void OnTick()   { //---      static datetime time_0;      if( time_0 == Time[0] ) return;      time_0 = Time[0];     //-----------------------------------------------------------------------          double stop_mini = MarketInfo(Symbol(),MODE_STOPLEVEL);                   int buyticket = -1; int sellticket = -1;               double atr = iATR(NULL,0,20,1);     double ma_1 = iMA(NULL,0,MA_Per,0,3,0,1);     double ma_2 = iMA(NULL,0,MA_Per,0,3,0,2);     double ma_3 = iMA(NULL,0,MA_Per,0,3,0,3);          double buy_sl = 0, buy_tp=0;     if( ma_3 > ma_2 && ma_2< ma_1 )       {               if( StopLoss > stop_mini )   buy_sl = Bid - StopLoss*Point;       if( TakeProfit > stop_mini )  buy_tp = Bid + TakeProfit*Point;                         buyticket = -1;         buyticket =      OrderSend(Symbol(),OP_BUY,Lots,Ask,3,buy_sl,buy_tp,"",MagicNumber,0,Green);      }            } //+------------------------------------------------------------------+```now here is the result, 12 dollar win if TP is hit, and 8 dollar loss if SL is hit
 253 ffoorr 2016.02.11 12:23 #   Here are MT4 reccommandations for a SELL order, buy on Bid, SL and TP on Ask : ```StopLoss = Ask + minimum distance = 1.2989 + 0.0005 = 1.2994, and TakeProfit = Ask - minimum distance = 1.2989 - 0.0005 = 1.2984.```Now i will do the reverse : fix my sl and tp of a Buy Order on the Ask, this EA will have the proosibility to choose if he SL and TP are on the Bid or on the Ask```extern double Lots = 0.1; extern double  TakeProfit = 100; extern double  StopLoss   = 100; extern int  MagicNumber = 15986; extern int MA_Per = 30; extern bool buy_on_bid = true; //+------------------------------------------------------------------+ //| Expert initialization function                                   | //+------------------------------------------------------------------+ int OnInit()  {   return(INIT_SUCCEEDED);  } //+------------------------------------------------------------------+ //| Expert deinitialization function                                 | //+------------------------------------------------------------------+ void OnDeinit(const int reason)  {  } //+------------------------------------------------------------------+ //| Expert tick function                                             | //+------------------------------------------------------------------+ void OnTick()   { //---      static datetime time_0;      if( time_0 == Time[0] ) return;      time_0 = Time[0];     //-----------------------------------------------------------------------          int buyticket = -1; int sellticket = -1; double price = 0;    double spread = MarketInfo(Symbol(),MODE_SPREAD);    double stop_mini  = MarketInfo(Symbol(),MODE_STOPLEVEL);                 double atr = iATR(NULL,0,20,1);     double ma_1 = iMA(NULL,0,MA_Per,0,3,0,1);     double ma_2 = iMA(NULL,0,MA_Per,0,3,0,2);     double ma_3 = iMA(NULL,0,MA_Per,0,3,0,3);          double buy_sl = 0, buy_tp=0;     if( ma_3 > ma_2 && ma_2< ma_1 )       {        price = 0.0;       if( buy_on_bid ) price = Bid; else price = Ask;            if( StopLoss > stop_mini )   buy_sl = price - StopLoss*Point;       else                         buy_sl = price - stop_mini*Point;        if( TakeProfit > stop_mini )  buy_tp = price + TakeProfit*Point;// + spread*Point;        else                          buy_tp = price + stop_mini*Point;                buyticket = -1;         buyticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,buy_sl,buy_tp,"",MagicNumber,0,Green);       }   } //+------------------------------------------------------------------+ ```And if I put SL and TP on a buy order on the ASK, here are the result :