How to coding about: Example, From open position let's begin count Bars

 

Hi everyone!

I have one problem, in mq4 code, I make indicator, this is Fractal component system.

And I use IndicatorCounted() in Start function:

int start()
  {
   int i=0, j=0;
   cbars = IndicatorCounted()/25;
   //cbars = WindowFirstVisibleBar()/25;
   
   if(cbars<0) return(-1);
   if(cbars>0) cbars--;
   pos=0;
   if(cbars > (Bars-cnt-1)) pos=(Bars-cnt-1);
   else pos=cbars+nRightUp;


And below run the code, and when come good signal (SELL OR BUY), (fractal component complex system)

Before I open a position, I count backwards a certain amount of bars according to a certain system, I saved into that variable.

And I add it to the IndicatorCounted () function. I'll show you how:

                 

                 ...  

                 if(pos == nLeftUp+1) iHCount_FINISH = FALSE;

                  ...

                  if(!iHCount_FINISH)     // allows 1 period to run

                     {                               //  LOOKS BACK SEARCHES IN ARRAY EXISTING IHIGH VALUEIT'S A FRACTAL VALUE HERE

                       iHCount = 1;
                       while(DFUPI_ONE != COUNT_H_PRICE_BAR)
                            {
                              COUNT_H_PRICE_BAR = iHigh(NULL, 0, iHCount);
                              iHCount++;
                              iHCount_FINISH = TRUE;
                            }
                       iHCount = iHCount - pos;
                       Target_Bars_S = (IndicatorCounted()-1) + iHCount; // EXAMPLE: 1200 + 12 = 1212
                     }

It does this and I show it with the Comment() function. And he shows me. Then we need the IndicatorCounted() function again to write the number of bars to a variable from the position opening.

And now comes the problem, that when I start a backtest, it counts for a few bars and stops counting all of a sudden.


Why is that?

Is the IndicatorCounted() function not working properly when running backtest?

Or why is this?

Ok, I haven’t tried a demo account like the M1 time icon, but I think the backtest is for us to see full mirror info about our work when we write the code.

Thanks in advance for your help, I look forward to helping anyone who can comment on this issue.

Thanks Peter


Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Bars - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Peter Horvath: I have one problem, in mq4 code,

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


  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  3. And I use IndicatorCounted() in Start function:
    
    int start()

    You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. See How to do your lookbacks correctly #9#14 & #19.