At the first glance, I can see three problems.
- i-4 is not going to work at i=1;
- you are recalculating all the bars on every tick;
- and empty values are better populated explicitly.
This should work:
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- // we're starting at bar 3, which is actually 4th // as the numbering starts at zero // int startpos=fmax(prev_calculated-1,3); // important correction: if we're working with open // prices only, we don't need to recalculate the current // bar on a new tick, hence it becomes: int startpos=fmax(prev_calculated,3); for(int i=startpos; i<rates_total; i++) { // while it could work without this, it's better to // populate every value to avoid e.g. issues when // switching timeframes BuyBuffer[i]=EMPTY_VALUE; SellBuffer[i]=EMPTY_VALUE; // I also took the liberty to move the calculation // one bar forward as open prices normally aren't // changing so we can use open[i] without the fear // of repainting double var_max = open[i]/low[i-3]; if(var_max > 1.003) { BuyBuffer[i] = open[i]; } else if(var_max < 0.997) { SellBuffer[i] = open[i]; } } //--- return value of prev_calculated for next call return(rates_total); }
- i-4 may not work because you didn't set your arrays to non-series.
- Your buffers will not work because you didn't set them to non-series.
- You didn't set your look back (4)
How to do your lookbacks correctly #9 — #14 & #19
You should look up Examples/Fractals and look for every line that are not included in yours.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi! Why my indicator below endend up with really strange values in buffers? Please...