Trying to perform some calculations while excluding Sunday

 
Basically I want to have a loop that will add up the range of the daily bars for the last xx days, ignoring Sundays.  I am generating a basic ATR because my current one is too low due to including Sunday bars.  So I am thinking the easiest way is probably to have a variable that adds up the range of the daily bars over the last xx days, but only adds it if the day it is looking at is not a Sunday, then when done divide it by the number of days it has included and I have an accurate ATR excluding Sundays.  I think its proably a very simple loop, I just haven't been able to figure it out yet.
 
What did you code so far?
 
lippmaje:
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;
 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 

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.
 

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(...)


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...
 
Nice thanks, I have got it working now with your revised method
Reason: