Your code works correctly with MT4 4.00 build 1260 on Windows 10.
You are not accounting for the spread, therefor your RRR is not 1:1
You buy at the Ask and sell at the Bid.
- 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?
- 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 - 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.
Your code works correctly with MT4 4.00 build 1260 on Windows 10.
You are not accounting for the spread, therefor your RRR is not 1:1
You buy at the Ask and sell at the Bid.
- 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?
- 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 - 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
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.
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!
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 } }
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.
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
Thank you very much!
Viktor