Bad calculation of profit and loss of transaction in USD

 

Hello everybody,

I wrote very simple code to analyse and backtest Inside bar strategy, but it is giving strange values when hits Targtet profit points.


AUDNZD, H4, Spread 30

For example from the attached picture:

System opened 2020.05.07 00:00

No 11 Buy Stop order

Price: 1.06644  // High of signal bar

SL: 1.06361 // Low of signal bar

TP: 1.06927 // TP and SL 1:1

and reversal order

No 12 Sell Stop

Price: 1.06361 // Low of signal bar

SL: 1.06644 // High of signal bar

TP: 1.06078 // TP and SL 1:1


System opened 2020.05.07 00:55 Sell position No 12 and went to the SL price. Position was closed with loss -35.52 USD. //EDIT: I think loss could be -16.98 USD

But system opened 2020.05.07 05:06 Buy position No 11 and went to the TP price. But the TP position was closed also with loss -4.47 USD.


I can not see the mistake. Could anybody help me please and tell me where it could be? I have written very simple code:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  high1=NormalizeDouble(iHigh(0,0,1),Digits);
  high2=NormalizeDouble(iHigh(0,0,2),Digits);
  low1=NormalizeDouble(iLow(0,0,1),Digits);
  low2=NormalizeDouble(iLow(0,0,2),Digits);
  
  
  TPbuy=(high2+(high2-low2));
  TPsell=(low2-(high2-low2));
  
  if (timePlacedOrders!=iTime(Symbol(),Period(),1) && high2>high1 && low2<low1)
  {
  OrderSend(Symbol(),OP_BUYSTOP,0.1,high2,5,low2,TPbuy,"My order",111,0,Green);
  OrderSend(Symbol(),OP_SELLSTOP,0.1,low2,5,high2,TPsell,"My order",222,0,Red);
  timePlacedOrders = iTime(Symbol(),Period(),1); //datuma  čas inside baru
  }
  }
//+------------------------------------------------------------------+

Thank you very much!

Viktor

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You can now not only read articles and download MQL5 programs, but you can also join discussions on the forum, leave comments on articles and source codes, rate MQL5 programs and share your own developments in the Code Base, and even publish articles for a decent fee (see Become an Author at MQL5.com!). MQL5.com services are constantly...
Files:
AUDNZD.png  20 kb
 

Your code works correctly with MT4 4.00 build 1260 on Windows 10.

 
Viktor Ševčík: TP: 1.06927 // TP and SL 1:1

You are not accounting for the spread, therefor your RRR is not 1:1

You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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.)
    Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.
 
mt4ski:

Your code works correctly with MT4 4.00 build 1260 on Windows 10.

Hello mt4ski, thank you for your response. What do you mean, works correctly? Do you get different numbers on your platform? Thank you, Viktor
 
William Roeder:

You are not accounting for the spread, therefor your RRR is not 1:1

You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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.)
    Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Dear William,

thank you very much. I know, this is not absolutely correct, but could this be the reason that I am having so nonsense numbers? Thank you very much. Viktor

 
Viktor Ševčík:

could this be the reason that I am having so nonsense numbers?

It has nothing to do with the spread because it is a result of backtesting (fixed spread 30 points). There are some mistakes in your code (the first parameter of iHigh() and iLow should be a string e.g. iLow(_Symbol,_Period,1)) but it is not a reason of your nonsense result. I've tried your code (with a small edit) in my MT4 build 1262 (win10) and I got awaited results.

Result

 
Petr Nosek:

It has nothing to do with the spread because it is a result of backtesting (fixed spread 30 points). There are some mistakes in your code (the first parameter of iHigh() and iLow should be a string e.g. iLow(_Symbol,_Period,1)) but it is not a reason of your nonsense result. I've tried your code (with a small edit) in my MT4 build 1262 (win10) and I got awaited results.


Dear Petr, thanks a lot! Could you please send me your whole source code?  I would try it on my platform at the same time and day as you and I will see what results I will get. I am thinking to try this also on different installation of diferrent broker (now I use XTB). If it works for you,it should work for me... Thank you very much!

 
Viktor Ševčík:

Dear Petr, thanks a lot! Could you please send me your whole source code?  I would try it on my platform at the same time and day as you and I will see what results I will get. I am thinking to try this also on different installation of diferrent broker (now I use XTB). If it works for you,it should work for me... Thank you very much!

Here you go. Bactest AUDNZD H4, spread 30 points, from 2020.05.07 to 2020.05.08

The whole code:

#property strict
void OnTick()
  {
   static datetime timePlacedOrders=0;
   double
      high1=iHigh(_Symbol,_Period,1),
      high2=iHigh(_Symbol,_Period,2),
      low1=iLow(_Symbol,_Period,1),
      low2=iLow(_Symbol,_Period,2),
      TPbuy=(high2+(high2-low2)),
      TPsell=(low2-(high2-low2));
  
   if(timePlacedOrders!=iTime(_Symbol,_Period,1) && high2>high1 && low2<low1)
     {
      if(OrderSend(Symbol(),OP_BUYSTOP,0.1,high2,5,low2,TPbuy,"My order",111,0,Green)==-1)
         printf("Order send error: %d",GetLastError());
      if(OrderSend(Symbol(),OP_SELLSTOP,0.1,low2,5,high2,TPsell,"My order",222,0,Red)==-1)
         printf("Order send error: %d",GetLastError());
      timePlacedOrders = iTime(Symbol(),Period(),1); //datum a čas inside baru
     }
  }
 
Petr Nosek:

Here you go. Bactest AUDNZD H4, spread 30 points, from 2020.05.07 to 2020.05.08

The whole code:

Thank you Petr! My results are attached, and still different. I am going to try different broker / installation of MT.


Files:
results.png  15 kb
 
I installed Metatrader 4 from Admiral markets and Purple trading and I get correct - different results. See attached pictures
Files:
 
Viktor Ševčík:

Thank you Petr! My results are attached, and still different. I am going to try different broker / installation of MT.

You'll have to ask your broker. I suppose you use MT4 provided by your broker, aren't you? Maybe it isn't a good idea.

Reason: