Query about storing indicator value over a period of time

 

Hello everyone,


I am having a hard time writing the perfect code for what I want. Basically I have the following scenario:

  • Candle 1: 
    • 12:00:00: Indicator value=5
    • 12:03:24: Indicator value=5.2
    • 12:04:59: Indicator value=5.5
  • Candle 2:
    • 12:05:00: Indicator value=6
    • 12:09:59: Indicator value=8
  • Candle 3: current one
    • 12:10:00 Indicator value=10 


I am trying to store current value=10, previous value=8 and the candle one last value=5.5. (Logic behind this: I work on 5min chart and I want to store candle's actual value, previous candle's last value, second previous candle's last value)


I have successfully coded for two values and I have been using it without problems however for three, I got confused as I don't know whether it's appropriate to use arrays or there are other better ways.. Below is what I coded:

double CurrentIndicator=0;
double PreviousIndicator5m=0;
double SecondPreviousIndicator5m=0;

void OnTick()
{
if((m==0||m==5||m==10||m==15||m==20||m==25||m==30||m==35||m==40||m==45||m==50||m==55) && (s==0)){ CurrentIndicator=iINDICATOR(NULL,PERIOD_M5,x,y,z,..,0);}
.
.
. // rest of my code here 
Print("Current value is: ", CurrentIndicator);
Print("Previous candle's value is: ", PreviousIndicator5m);
Print("Second Previous candle's value is: ", SecondPreviousIndicator5m);

.
.
// I put this before I close my OnTick()
if((m==59||m==4||m==9||m==14||m==19||m==24||m==29||m==34||m==39||m==44||m==49||m==54) && (s==59)){PreviousIndicator5m=iINDICATOR(NULL,PERIOD_M5,x,y,z,..,0);};
}


I would appreciate it if someone clarifies what should I use to store these values.


Kind regards,

Abdel

 
  1. AbdelEl: I would appreciate it if someone clarifies what should I use to store these values.

    Why do you think you need to store them at all? Just read the indicator for the M5 bar index you want.

  2. Your code
    if((m==0||m==5||m==10||m==15||m==20||m==25||m==30||m==35||m==40||m==45||m==50||m==55) && (s==0))
    Simplified
    if((m % 5==0                                                                        ) && (s==0))

  3. Your code
    if((m==59||m==4||m==9||m==14||m==19||m==24||m==29||m==34||m==39||m==44||m==49||m==54) && (s==59))
    Simplified
    if((m % 5 == 4                                                                      ) && (s==59))
  4. If s is seconds, what happens if there is no tick that second? What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.
 
William Roeder:
  1. Why do you think you need to store them at all? Just read the indicator for the M5 bar index you want.

  2. Your code
    Simplified

  3. Your code
    Simplified
  4. If s is seconds, what happens if there is no tick that second? What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.


Thank you for your reply. I took in consideration the simplified version in 2 and 3.

About 1, I need to store these three values because I compare the two previous values (for two previous candles) at that specific time with the actual value.

About 4, it doesn't pose a problem if there are no ticks that second.. all what matters is that I keep saving these three values like in the scenario.


Thank you!

 
AbdelEl:


Thank you for your reply. I took in consideration the simplified version in 2 and 3.

About 1, I need to store these three values because I compare the two previous values (for two previous candles) at that specific time with the actual value.

About 4, it doesn't pose a problem if there are no ticks that second.. all what matters is that I keep saving these three values like in the scenario.


Thank you!


I thought about what you said (@William Roeder) in point 1, I have tried to read the indicator as follows: (shift 1,2,3)

CurrentIndicator=iINDICATOR(NULL,PERIOD_M5,x,y,z,..,0);

PreviousIndicator5m=iINDICATOR(NULL,PERIOD_M5,x,y,z,..,1);

SecondPreviousIndicator5m=iINDICATOR(NULL,PERIOD_M5,x,y,z,..,2);


I tried to make sure if it shows the values as I want but it got me very confusing. I am not sure if the values stored in my variables are at the start of the candle or the last value recorded with the candle..

I will keep trying to figure it out and if found, I will reply to confirm :)

Kind regards

 
AbdelEl: I am not sure if the values stored in my variables are at the start of the candle or the last value recorded with the candle..

Always last unless your indicator is doing something unusual.

 
William Roeder:

Always last unless your indicator is doing something unusual.

My query is solved. I simply used the shift in the indicator. It did the work without the need to calculate all that stuff..

Thank you @William Roeder.

Reason: