Incorrect TP value

 
Hello Developers,

I need your help to solve a peculiar case.
My problem happens when sending TP and SL for orders.
EA seeks TP of only 10 points. However when opening orders this distance is not being respected. For example, if I open a buy order by placing a 10-point TP, there are times when the order closes at 0 profit or sometimes even in lose trade.
am using for BUY orders the sum of TP along with Ask price and SELL orders with Bid price

I don't know if I'm handling orders in the right way. Can anyone please analyze my code and help me solve this problem?

In the following image the EA had a TP of 10 points or 1 pip, in the first order respect the values, but in the second order opened with the same entry price.  

bug

I also attached the original EA


Thank you


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
input int magic=2019; //magic
input int TP=5; //Take Profit
input int SL=800; //Stop Loss
input bool useLotPerEquity=false; // lot manage
input int lotPerEquity=50; //add 0.01 for each
input double lots=0.1; //or fixed lot
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   openFirstTrade(); //valid and open first trade

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
openFirstTrade()
  {
   static datetime oldTime; //define the variable

   if(oldTime==0) oldTime=Time[0];

   if(oldTime!=Time[0]) //New Bar and Hour time Handler
     {
      Buy(OP_BUY,lots,SL,TP,magic,"ScalperX BUY");
      Sell(OP_SELL,lots,SL,TP,magic+100,"ScaperX SELL");
      oldTime=Time[0];
     }
  }
//+------------------------------------------------------------------+
//| Buy                                                              |
//+------------------------------------------------------------------+
void Buy(int orderTypeTrade,double lot,double sl,double tp,int magicNumber,string comment)
  {
   int ticket=0;
//Lot handle
   if(useLotPerEquity)
     {
      lot=0;
      int valueLotHandler=(int)AccountEquity()/lotPerEquity;
      for(int i=0; i<valueLotHandler; i++) lot=lot+0.01;
     }
   double internalSLBuy=0;
   double internalTPBuy=0;
   if(sl!=0) internalSLBuy=NormalizePrice(Ask-(SL*Point));
   if(tp!=0) internalTPBuy=NormalizePrice(Ask+(TP*Point));

   ticket=OrderSend(
                    Symbol(),                                        // symbol 
                    orderTypeTrade,                                  // operation 
                    lot,                                             // volume 
                    Ask,                                             // price 
                    30,                                              // slippage 
                    internalSLBuy,                                              // stop loss 
                    internalTPBuy,                                              // take profit 
                    comment,// comment 
                    magicNumber,                                     // magic number 
                    clrBlue                                          // color 
                    );
   if(ticket<0)
     {
      Print("EA - OrderSend  failed with error #",GetLastError());
     }
   else
      Print(comment);
  }
//+------------------------------------------------------------------+
//|  Sell                                                            |
//+------------------------------------------------------------------+
void Sell(int orderTypeTrade,double lot,double sl,double tp,int magicNumber,string comment)
  {
   int ticket=0;
//Lot handle
   if(useLotPerEquity)
     {
      lot=0;
      int valueLotHandler=(int)AccountEquity()/lotPerEquity;
      //Print("valueLotHandler:"+IntegerToString(valueLotHandler));
      for(int i=0; i<valueLotHandler; i++) lot=lot+0.01;
     }
//SL & TP handler case input is 0
   double internalSLSell=0;
   double internalTPSell=0;
   if(sl!=0) internalSLSell=NormalizePrice(Bid+(SL*Point));
   if(tp!=0) internalTPSell=NormalizePrice(Bid-(TP*Point));
   ticket=OrderSend(
                    Symbol(),                               // symbol 
                    orderTypeTrade,                         // operation 
                    lot,                                    // volume 
                    Bid,                                    // price 
                    30,                                     // slippage 
                    internalSLSell,                                     // stop loss 
                    internalTPSell,                                     // take profit 
                    comment,// comment 
                    magicNumber,                            // magic number 
                    clrBlue                                 // color 
                    );
   if(ticket<0)
     {
      Print("EA - OrderSend  failed with error #",GetLastError());
     }
   else
      Print(comment);
  }
//+------------------------------------------------------------------+
double NormalizePrice(double p,string pair="")
  {
// https://www.mql5.com/en/forum/135345 zzuegg reports for non-currency DE30:
// MarketInfo(chart.symbol,MODE_TICKSIZE) returns 0.5
// MarketInfo(chart.symbol,MODE_DIGITS) return 1
// Point = 0.1
// Prices to open must be a multiple of ticksize 
   if(pair=="") pair=Symbol();
   double ts=MarketInfo(pair,MODE_TICKSIZE);
   return( MathRound(p/ts) * ts );
  }
//+------------------------------------------------------------------+
Files:
ScalpeX.mq4  77 kb
 
  1.    if(sl!=0) internalSLSell=NormalizePrice(Bid+(SL*Point));
       if(tp!=0) internalTPSell=NormalizePrice(Bid-(TP*Point));
       ticket=OrderSend(
                        Symbol(),                               // symbol 
                        orderTypeTrade,                         // operation 
                        lot,                                    // volume 
                        Bid,         
    
    Your stops have StopLoss/Takeprofit Points minus spread for buys and that plus spread for sells.) Don't you want the same amount for either direction?

    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP is longer.
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at 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.)

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.
 

Hi, I have looked at your code. I will tell you my understanding, so I apologize in advance if it does not help.

The code executes the 1st trade differently to the subsequent trades. This is what the function openfirsttrade() is for.

However, in your Buy function and sell function, The code refers to sl!=0 AND tp!=0, but I cannot see in your code any defined values for tp and sl.

in the line where they are declared they are not assigned any value.


So I would recommend to assign a value to sl and tp and try run the code again.

 
William Roeder:
  1. Your stops have StopLoss/Takeprofit Points minus spread for buys and that plus spread for sells.) Don't you want the same amount for either direction?

    You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP is longer.
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at 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.)

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

Hello William Roeder

Thanks for taking the time to help me.

Can you please enlighten me a little more?

As in the example above I used Bid price to Sell and Ask price to Buy with the TP and SL. This way I thought I already added the spread because I was using the opening price as a reference for my TP, and after that i only needed to add the distance of the TP.

For me to at least guarantee that my order will have  the value of TP plus spread, what is the correct way?


Thanks in advance


I used this video as a reference


 
EA_Research_Development:

Hi, I have looked at your code. I will tell you my understanding, so I apologize in advance if it does not help.

The code executes the 1st trade differently to the subsequent trades. This is what the function openfirsttrade() is for.

However, in your Buy function and sell function, The code refers to sl!=0 AND tp!=0, but I cannot see in your code any defined values for tp and sl.

in the line where they are declared they are not assigned any value.


So I would recommend to assign a value to sl and tp and try run the code again.

Hello, thanks for the help, but in the code above I put TP and SL as input for users