NetProfit montly average;

Questions about MultiCharts and user contributed studies.
gioakino
Posts: 11
Joined: 11 Feb 2019

NetProfit montly average;

Postby gioakino » 21 Jun 2019

Hi everyone,
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);

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 21 Jun 2019

gioakino,

when you apply the study to anything but monthly bars, the average will be computed using the value of montlyprofit over the last avglen number of bars and not the last avglen monthly values.

One approach to get the average over the last X month would be by using an array where you store one value for every month and then to average the values.

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 21 Jun 2019

Abc,
thanks for your answer, I'm not very expert with arrays, there is another possible approach?

Regards

gioakino

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 24 Jun 2019

gioakino,

a list would work, too if you want your code to be able to be flexible and allow varying average length settings. You could probably even use variables, but this would require a lot more coding and be a lot less flexible.

Your idea sounds like a perfect opportunity to become more familiar with arrays.

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 24 Jun 2019

Abc,
thanks you for your answear, you're right, what I want from my code is flexibility. I looking for some examples and I tried to write the code using arrays, but something is wrong because the avg of the array is always zero. If you or someone can check it, I'd be grateful.

Regards

gioakino

Code: Select all

array: Myarray[20] (0); var: counter(0),arraysize(20),HoldRow(0), SaveValue(0); 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; savevalue= Montlyprofit; end; for counter = 1 to arraysize begin holdrow= myarray[counter]; myarray[1]= savevalue; savevalue= holdrow; end; MontlyAvgprofit = average(myarray[0],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);

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 24 Jun 2019

gioakino,

I wouldn't suggest using the average function with the array, but to compute the average yourself over the desired length within your array. The print reserved word is a good way to find out what values are currently stored within your array, if you correctly update them etc..

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 24 Jun 2019

Abc,
I tried to correct the code, now I get the last value of Montlyprofit divided by three instead the average of the last three elements, I don't understand the reason .

Regards

gioakino

Code: Select all

array: Myarray[20] (0); var: counter(0),arraysize(20),HoldRow(0), SaveValue(0); 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; for counter = 1 to arraysize begin holdrow= myarray[counter]; myarray[1]= savevalue; savevalue= holdrow; Montlyavgprofit= AverageArray(myarray,avglen); end; 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) + " " + NumToStr( myarray[1],2) +NewLine);

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 25 Jun 2019

gioakino,

what are the results of your checks using the print reserved word?

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 25 Jun 2019

ABC,

myarray[1] returns the value of the last "montlyprofit" and Montlyavgprofit returns the value of the last "montlyprofit" divided by three (average size).

Regards
gioakino

Code: Select all

array: Myarray[20] (0); var: counter(0),arraysize(20),HoldRow(0), SaveValue(0); 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; for counter = 1 to arraysize begin holdrow= myarray[counter]; myarray[1]= savevalue; savevalue= MontlyProfit; Montlyavgprofit= AverageArray(myarray,avglen); end; 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) + " " + NumToStr( myarray[1],2) +NewLine);

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 25 Jun 2019

gioakino,

where do you store the values from the prior months?

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 25 Jun 2019

Abc,


I suppose that's the mistake, the problem is that I have no idea how to store the values . How can I do it?

Regards

gioakino

User avatar
ABC
Posts: 626
Joined: 16 Dec 2006
Contact:

Re: NetProfit montly average;

Postby ABC » 26 Jun 2019

gioakino,

it might make sense to take one step back to familiarize yourself with the basics of arrays a bit more as this should make it a lot easier later. The free EasyLanguage Essentials PDF contains a brief section about arrays that provides a good first start.
In general your code will have to keep track of the index number where you store values within the array. This can be accomplished using a variable that you increment at the beginning of every new period (month for example). Using this variable you can store a value at this index within your array. Once the variable becomes larger than your desired average length you reset it and and start over. This way you can also keep track of the first time when the array is populated with the desired amount of values and compute the average (or compute the average using a shorter length while it's not yet fully populated).

Regards,

ABC

gioakino
Posts: 11
Joined: 11 Feb 2019

Re: NetProfit montly average;

Postby gioakino » 26 Jun 2019

ABC,

thanks for your help, I read the "easylanguage essentials" guide and i've changed everything.
Is wrong this way of calculating the average with arrays of the last n elements ?
I have this error message in study: " Array bounds. Wrong index value : 1 . "
Regards

gioakino

Code: Select all

array: Mydynamicarray[] (0); array_setMaxIndex (Mydynamicarray, count); input: avglen(4); Variables: isLastDayOfMonth(False),isSameMonth(False),nextBusinessDay(0),MontlyProfit(0),LastBar(False), Montlyavgprofit(0) , count(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 count= count + 1 ; MontlyProfit= Netprofit; end; Mydynamicarray[count] = MontlyProfit; MontlyAvgprofit= AverageArray(Mydynamicarray,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) + " " + NumToStr( Mydynamicarray[count],2)+NewLine);


Return to “MultiCharts”