- Relative Vigor Index - Oscillators - Indicators - Charts - MetaTrader 5 for Android
- Relative Vigor Index - Oscillators - Indicators - Chart - MetaTrader 5 for iPhone
- Relative Vigor Index - Oscillators - Technical Indicators - Price Charts, Technical and Fundamental Analysis
What did you code so far?
Just an outline of something that doesn't work. Input number of days the ATR should be for, do a loop adding on the ATR for each individual
day for that number of days from today backwards, when done divide the total by number of days to get the ATR for the last x days. The
dayofweek bit doesn't work and is just a placeholder for what needs to go in there that will skip adding that day's ATR if that day happens to be a
Sunday. So if it discovers the day is a Sunday then it just takes an extra 1 off the counter to skip past it when adding the ATR together for
each of the days. I know the sort of logic I need but I don't know mql much to be able to make it do it.
extern int Numofdays int start() double ATR; For(int periodforloop=Numofdays; periodforloop >= 0; periodforloop--) { if(DayOfWeek(periodforloop)==0) periodforloop--; ATR = ATR + (iATR(Symbol(),PERIOD_D1,1,periodforloop))* MathPow(10,Digits); } ATR=ATR/numofdays;
Some remarks:
- The use of extern and start() is deprecated.
-
if(DayOfWeek(periodforloop)==0) periodforloop--;
- Looks like you want to skip the loop, you can use continue for this.
- If you use a value for shifting then name it so.
- In MQL4 you can use TimeDayOfWeek() to check for a Sunday.
-
ATR = ATR + (iATR(Symbol(),PERIOD_D1,1,periodforloop))* MathPow(10,Digits);
- When requesting data from histories other than current you need to check for history not found errors.
- Instead of multiplying within each iteration, multiply the final result, this would be much more performant.
-
ATR=ATR/numofdays;
- You can use /= like so: ATR /= numofdays;
- Since the loop may have skipped some days, most of the times you won't get numofdays summands. So dividing by it will yield the wrong
result. Imagine it's 1 and you start on Sunday.
- You can use /= like so: ATR /= numofdays;
Ok I now have this which sort of works but I don't think it is ignoring Sundays. I am using dayscount in the loop so that I know how many days
were NOT Sundays so this is the number I need to divide by to get the right average. However I have manually calculated the ATR and it is
definitely still including Sundays.
int dayscount; extern int period; for(int periodforloop=period; periodforloop >= 0; periodforloop--) { if(TimeDayOfWeek(TimeDay(Time[periodforloop]))==0) continue; dayscount++; ATR = ATR + (iATR(Symbol(),PERIOD_D1,1,periodforloop)); } ATR=ATR* MathPow(10,Digits); ATR /= dayscount;
My remarks:
-
if(TimeDayOfWeek(TimeDay(Time[periodforloop]))==0) continue;
- This is ok, but TimeDayOfWeek() takes a datetime as input. Passing TimeDay() which returns an int between 1..31 breaks the logic.
- When accessing Time[] you get the datetime values for the current timeframe. This surely causes havoc when it differs from PERIOD_D1. Better use iTime().
-
dayscount++;
- dayscount is nowhere initialized. Worst case is you count up some 'random' memory junk value without even noticing it, except that the ATR occasionally appears to be way too low.
- What about counting up dayscount, and when it reaches period you break from the loop? So that the ATR is calculated for exactly the amount of days specified?
-
ATR=ATR* MathPow(10,Digits);
- Likewise to /= there's also a *= so why not just do ATR *= MathPow(...)
- Likewise to /= there's also a *= so why not just do ATR *= MathPow(...)
Proposal:
input int InpPeriod=14; // ATR Period int dayscount = 0; int shift = -1; double ATR = 0; ResetLastError(); while(dayscount<InpPeriod) { shift++; datetime dt = iTime(_Symbol,PERIOD_D1,shift); if(_LastError) { HandleHistoryNotFound(); break; } if(TimeDayOfWeek(dt)==SUNDAY) continue; ATR += iATR(_Symbol,PERIOD_D1,1,shift); if(_LastError) { HandleHistoryNotFound(); break; } dayscount++; } ATR...
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use