with this code, I would like to calculate the average of the Montly netprofits the last day of month, if I put the length at two periods it is well calculated but if the length is three or more periods it is not well calculated. Can someone help me to understand why this happens?
Code: Select all
input: avglen(4);
Variables:isLastDayOfMonth(False),isSameMonth(False),nextBusinessDay(0),MontlyProfit(0),LastBar(False), Montlyavgprofit(0) ;
LastBar = SessionLastBar ;
if (DayOfWeek(Date) = Friday) then
nextBusinessDay = JulianToDate(DateToJulian(Date) + 3)
else
nextBusinessDay = JulianToDate(DateToJulian(Date) + 1);
isSameMonth = (Month(Date) = Month(nextBusinessDay));
if (not isSameMonth) then
isLastDayOfMonth = true
else
isLastDayOfMonth = false;
If isLastDayOfMonth and Lastbar then begin
MontlyProfit= Netprofit;
end;
MontlyAvgprofit= average(montlyprofit,avglen);
If isLastDayOfMonth and Lastbar then FileAppend("C:\Users\utente\Desktop\outputMulticharts\MontlyReport.txt"," "+getstrategyname +" " +FormatDate("dd-MM-yyyy", DateToJulian(Date) )+" " + NumToStr(MontlyProfit,2)+ " " + NumToStr(Montlyavgprofit,2) +NewLine);