Not traded on the bar signal triggers

 

Hi,


I have the robot (combination of SAR and MA), which is doing the following (attaching the code):

1. enter into position using MA crossover confirmed with SAR.

2. exit and enter the opposite direction whenever the signal changes (hence you always have one position opened). 


However, the problem is that it's trading on the third bar after the signal triggers (not on the bar the signal triggers). For instance, please see the image example attached. 

Files:
MA_PSAR.mq5  8 kb
image.PNG  84 kb
 
In the debug mode, you will trace how the iSAR indicator is formed (especially pay attention to the moment when the iSAR changes direction) and many questions will disappear at once.
 
Vladimir Karputov:
In the debug mode, you will trace how the iSAR indicator is formed (especially pay attention to the moment when the iSAR changes direction) and many questions will disappear at once.

Thanks a lot Vladimir for your reply. I was trying to narrow down the issue and removed the SAR completely from the trading strategy, hence now it just performs the MA crossover (attaching the code):

1. MA crossover signal (open position if there is no opened position with the same direction already);

2. Exit if the signal changes the opposite direction and enter the new one. 


I am attaching example from the backtest, where you can clearly see that the issue persist (and buy/sell happens consistently on the third bar after the signal triggers, while I would expect trade to happen at the time of the signal), hence the issue doesn't seem to be driven by SAR. I would appreciate if you can suggest.

Files:
image.PNG  131 kb
 

I saw this: creating a handle ON EVERY TEAK.

After that I threw out this code.

Read the help! See examples from help! DO NOT CREATE AN INDICATOR HANDLE ON EACH TEAK !!! The indicator handle must be created ONCE (this is done in OnInit) !!!

Correct this gross mistake. Until you correct this gross mistake, I will not be able to help you with anything.


Added: do not use variables of type 'static' - it is forbidden for you (until you gain experience).


Addded:  CopyBuffer

int  CopyBuffer(
   int       indicator_handle,     // indicator handle
   int       buffer_num,           // indicator buffer number
   int       start_pos,            // start position
   int       count,                // amount to copy
   double    buffer[]              // target array to copy
   );


Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
... until you gain experience.
So good.

Reason: