Problem with StopLoss and TakeProfit

 

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


 

 

Here are MT4 reccommandations for a SELL order, buy on Bid, SL and TP on Ask :

https://book.mql4.com/trading/orders

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 :


 
ffoorr:


now here is the result, 12 dollar win if TP is hit, and 8 dollar loss if SL is hit

You have that back to front.

 

If you want profit and/or loss to be equal, for a buy you add the TP to the Entry price and deduct the SL from the Entry price 

 
GumRai:

You have that back to front.

 

If you want profit and/or loss to be equal, for a buy you add the TP to the Entry price and deduct the SL from the Entry price 


 This is what I want, and I also want my TP or SL if they are 100 point give 10 dollar;

This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?

 
ffoorr:


 This is want I want, and I also want my TP or SL if they are 100 point give 10 dollar;

This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?

It is not illogical. A Buy is closed at Bid, no matter whether in profit or loss. If both TP and SL are set at 100 Points and spread is 20 Points, because of the spread, price has to rise 120 Points to hit TP, but only has to drop 80 Points to hit the SL. That is the reason why scalpers lose, working with small profits where the cost of trading is expensive compared to potential profit and win/loss ratio.
 
ffoorr:


 This is what I want, and I also want my TP or SL if they are 100 point give 10 dollar;

This is what the code do if SL and TP are added to Entry price, but it is illogic, what about the spread ?

Your reasoning is illogic. The profit/loss is calculated simply by +/-(close price - open price). If you want 10pips profit(TP) or loss(SL), just put the value (close price) at 10 pips of the open price : ask for a buy, bid for a sell. The spread just means that the market will have to move 10+spread to hit the TP and 10-spread tp hit the SL. So it changes the probability not the profit/loss.
 
ffoorr: L if they are 100 point give 10 dollar;
You adjust the lot size
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  • Do NOT use TickValue by itself - DeltaPerlot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
 

Thank's for your reply,

What I wanted to do was to place SL and TP as close as possible, at the stop_level distance, with the same value for SL and TP.

For a buy order I found it is 30 point, because of the SL_buy =  Ask - spread*Point- stop_level*Point; 

Reason: