Stop loss & Take profit are too close to price

 

I'm new to MQL4, please help with my code. When I place my 2nd order, both Take profit and stop loss are TOO close to price instead of TP= 7, SL=2. What is the problem???

extern double TakeProfit=5;
extern double TakeProfit2=7;
extern double StopLoss=2;
extern double StopLoss2=2;

int MagicNumber=2343;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

void BuyTrade()
 {
  double TickSize=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  double TickValue=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
  double point=SymbolInfoDouble(Symbol(),SYMBOL_POINT);
  double PointValue=TickValue*TickSize/point;
  
  int Final=0;
  
  const string GetBase[4]={"EURUSD","GBPUSD","AUDUSD","NZDUSD"};
  const string GetBase2[3]={"USDJPY","USDCHF","USDCAD"};
  
  string Base="";
  Base=GetBase[Final];
  
  string Base2="";
  Base2=GetBase2[Final];
  
  double PriceBase2=MarketInfo(Base2,MODE_BID);
  
  double TakeProfitBuy=Ask+TakeProfit*Pips()/PointValue;
  double StopLossBuy=Ask-(StopLoss*Pips());
  
  double TakeProfitSell=PriceBase2-TakeProfit2*Pips()/PointValue;
  double StopLossSell=PriceBase2+(StopLoss2*Pips());
  
  if(OrdersTotal()==0)
  {
   int BuyTrade=OrderSend(Base,OP_BUYSTOP,0.01,Ask+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy,"Scalper8",MagicNumber,0,clrBlue);

   int SellTrade=OrderSend(Base2,OP_SELLSTOP,0.01,PriceBase2,0,StopLossSell,TakeProfitSell,"Scalper8",MagicNumber,0,clrRed);
  } 
  
 }
 
Scalper8:

I'm new to MQL4, please help with my code. When I place my 2nd order, both Take profit and stop loss are TOO close to price instead of TP= 7, SL=2. What is the problem???

  1. The way I have always calcuated tp & sl is:



extern double TakeProfit=5;
extern double TakeProfit2=7;
extern double StopLoss=2;
extern double StopLoss2=2;
double
stopLoss                      =  NormalizeDouble(Point*StopLoss2,Digits); double takeProfit                    =  NormalizeDouble(Point*TakeProfit2,Digits); double shortStopLoss                 =  Bid + stopLoss; double shortTakeProfit               =  Bid - takeProfit; //i.e. 1.50000+(0.00001 * 2) if(OrdersTotal()==0)   {    int BuyTrade=OrderSend(Base,OP_BUYSTOP,0.01,Ask+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy,"Scalper8",MagicNumber,0,clrBlue);    int SellTrade=OrderSend(Base2,OP_SELLSTOP,0.01,PriceBase2,0,shortStopLoss,shortTakeProfit,"Scalper8",MagicNumber,0,clrRed);   }

2. When calculating sl & tp, always use the NormalizeDouble(...) function, this will round your prices to the appropriate accuracy as specified by the number of decimal places your broker has rounded your pricing to. i,e, EURJPY normally is 3 decimal paced where the EURUDS either 4 or 5 decimal places. the NormalizeDouble() feature will automatically work this out, or you could get errors such as the "Invalid StopLoss" error. 

3. Just a quick heads up with scalping, most brokers will have what's is called a minimum stop level, which means that brokers will set a minimum number of pips either side of the entry price. This means that traders cannot set tp or sl levels between the entry price and the minimum stop level, making it very difficult for scalpers to..well, scalp! 

Hope this helps, and welcome to the community! 

 
  1. double TakeProfitBuy=Ask+TakeProfit*Pips()/PointValue;

    TP is a price. Dividing by PointValue makes no sense.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


  3.   string Base=GetBase[Final];
      string Base2=GetBase2[Final];
      
      double PriceBase2=MarketInfo(Base2,MODE_BID);
     ⋮
       int BuyTrade=OrderSend(Base,OP_BUYSTOP,0.01,Ask+(0.5*Pips()),0,StopLossBuy,TakeProfitBuy,"Scalper8",MagicNumber,0,clrBlue);
    You are getting the Bid From Base2, ticksize, tickvalue, and point from the current chart, but opening Base. Makes no sense.
  4. You can't use any predefined variables because that referes tot he current chart, not Base.
 
TheHonestPrussian #:

  1. The way I have always calcuated tp & sl is:

2. When calculating sl & tp, always use the NormalizeDouble(...) function, this will round your prices to the appropriate accuracy as specified by the number of decimal places your broker has rounded your pricing to. i,e, EURJPY normally is 3 decimal paced where the EURUDS either 4 or 5 decimal places. the NormalizeDouble() feature will automatically work this out, or you could get errors such as the "Invalid StopLoss" error. 

3. Just a quick heads up with scalping, most brokers will have what's is called a minimum stop level, which means that brokers will set a minimum number of pips either side of the entry price. This means that traders cannot set tp or sl levels between the entry price and the minimum stop level, making it very difficult for scalpers to..well, scalp! 

Hope this helps, and welcome to the community! 

I used NormalizeDouble() and the 2nd pair TP & SL is still close.
 
William Roeder #:
  1. TP is a price. Dividing by PointValue makes no sense.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


  3. You are getting the Bid From Base2, ticksize, tickvalue, and point from the current chart, but opening Base. Makes no sense.
  4. You can't use any predefined variables because that referes tot he current chart, not Base.

The reason why I'm dividing is cause of the spread. It works properly for me when trade GPB/JPY and XAU/USD (TP). 

So instead of using predefined variables, what is the alternative function?

 
Scalper8 #:
I used NormalizeDouble() and the 2nd pair TP & SL is still close.

It will be, scalping is about setting TP's & SL's tight to the entry price.

The NormalizeDouble(...) function is just so you maintain consistency in your pricing, otherwise your EA may calculate your prices to infinity decimal places which will clash with your broker's pricing and/or yield a very different result compared to the one you wre expecting.

i.e a 1.52356 - 7pips tp (a NormalizeDouble(....)-adjusted price, assuming your broker works to 5 decimal places) is going to yield a very different result compared to if your EA returns a price of 1.5235654623132456465 - 7 pips tp, the former throwing your EA into a state of confusion as the broker will have only calculated to 5 decimal places.

The reason why the TP & SL are too close is because you're setting them too close. You need to set larger numbers for both.

Pip, pip! 

(No pun intended!)

(OK, pun intended! ;)) 

 
TheHonestPrussian #:

  1. The way I have always calcuated tp & sl is:

2. When calculating sl & tp, always use the NormalizeDouble(...) function, this will round your prices to the appropriate accuracy as specified by the number of decimal places your broker has rounded your pricing to. i,e, EURJPY normally is 3 decimal paced where the EURUDS either 4 or 5 decimal places. the NormalizeDouble() feature will automatically work this out, or you could get errors such as the "Invalid StopLoss" error. 

3. Just a quick heads up with scalping, most brokers will have what's is called a minimum stop level, which means that brokers will set a minimum number of pips either side of the entry price. This means that traders cannot set tp or sl levels between the entry price and the minimum stop level, making it very difficult for scalpers to..well, scalp! 

Hope this helps, and welcome to the community! 

The simplest method ;) Just researched) Thanks

Reason: