Download MetaTrader 5

Bizarre uneven takeprofit and stoploss

To add comments, please log in or register
phoenix13
10
phoenix13  

Hi All,

I've been trying to ensure that my takeprofit and stoploss are equal (in other words, profits and losses should be of the same magnitude). I've done a bit of investigating and tried a few solutions but I still get uneven profits and losses - even though looking at the prices in the results tab it they should be equal. Here's the code I used to execute the trades:


double trade_size = 0.0003; 

bool OpenBuy()
{

  //Get new market prices

   RefreshRates();

   

   //Define lot size
   double Lots = 0.01;
   int LotDigits = (int) - MathLog10(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP));
   double LotSize=NormalizeDouble(Lots,LotDigits); 

   

   //Calculate and check spread  

   double MaxSpreadInPoints = 50;
   double Spread = Ask - Bid;

   if(Spread>MaxSpreadInPoints)
return(false);

   

   //Define allowed slippage

   int SlippageInPoints = 0;

   //Define TakeProfit and StopLoss levels 

   double TakeProfit = NormalizeDouble(Bid+Spread+trade_size,Digits);
   double StopLoss = NormalizeDouble(Bid+Spread-trade_size,Digits);;

   int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,SlippageInPoints,StopLoss,TakeProfit,"Spread Test",13,0,Green);

   if(Ticket==-1)
   {
      Alert("Buy Error: " ,GetLastError());
      return(false);
   }

   if(Ticket>=0)
   {
      return(true);
   }

   return(false);

}

bool OpenSell()
{

   //Get new market prices

   RefreshRates();

   

   //Define lot size

   double Lots = 0.01;
   int LotDigits = (int) - MathLog10(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP));
   double LotSize=NormalizeDouble(Lots,LotDigits);

   //Calculate and check spread  

   double MaxSpreadInPoints = 50;
   double Spread = Ask - Bid;

   if(Spread>MaxSpreadInPoints)
      return(false);

   //Define allowed slippage

   int SlippageInPoints = 0;

   //Define TakeProfit and StopLoss levels
   double TakeProfit = NormalizeDouble(Bid-trade_size,Digits);
   double StopLoss = NormalizeDouble(Bid+trade_size,Digits);;

   int Ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,SlippageInPoints,StopLoss,TakeProfit,"Spread Test",13,0,Red);

   if(Ticket==-1)
   {
      Alert("Sell Error: " ,GetLastError());
      return(false);
   }

   if(Ticket>=0)
   {
      return(true);
   }

   return(false);

}

 and here are some of the resulting trades:

 

As you can see the profit is always 23 cents and the loss is always 37 cents. I've been wracking my brain to figure out what is going on but I can't figure it out. Am I missing something obvious to do with the bid/ask spread? 

Any help would be greatly appreciated.

Keith Watford
Moderator
10651
Keith Watford  
7 cents commission?
phoenix13
10
phoenix13  
Keith Watford:
7 cents commission?

You know what - I think you're right. That makes sense on what the broker charges. I hadn't figured on that. Dammit!

Thanks for your help though :) Much appreciated 

whroeder1
15925
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2.    int LotDigits = (int) - MathLog10(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP));
       double LotSize=NormalizeDouble(Lots,LotDigits);

       double TakeProfit = NormalizeDouble(Bid-trade_size,Digits);
       double StopLoss = NormalizeDouble(Bid+trade_size,Digits);;
    Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
    LotDigits also fails when not a power of 1/10. (int) MathCeil(-MathLog10(...)) will work.
    You also aren't getting a 1:1 RRR. For a buy your SL includes the spread (buy at the Ask, close at Bid,) your TP does not (buy at the Ask, close at the Ask-spread i.e. Bid.) For a sell your SL doesn't include the spread (sell at the Bid, close at the Bid-spread i.e. Ask,) same for TP.

  3. Your code
    Simplified
       if(Ticket>=0)
       {
          return(true);
       }

       return(false);
       return Ticket>=0;
To add comments, please log in or register