Seasonality Curve using arrays.
Forums › ProRealTime English forum › ProBuilder support › Seasonality Curve using arrays.
- This topic has 12 replies, 2 voices, and was last updated 2 years ago by robertogozzi.
Tagged: array, seasonality
-
-
03/03/2020 at 12:18 PM #121052
I decided to have a little play with the new arrays in v11 and created this simple code that draws a Jan to Dec seasonality curve. It needs plenty more work to turn it into something exciting but has shown me how much easier it is to analyse historical data now that we have arrays.
Launch it on a monthly chart as it is only coded to work on that time frame at the moment.
The attached image is of Soybeans.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657defparam drawonlastbaronly = true$month[openmonth] = $month[openmonth] + (close-open)$count[openmonth] = $count[openmonth] + 1$avg[openmonth] = $month[openmonth]/$count[openmonth]mymin = 0value = 0for i = 1 to 12ii = 13 - idrawsegment(barindex-ii,value,barindex-(ii)+1,value + $avg[i])value = value + $avg[i]mymin = min(mymin, value)nextfor i = 1 to 12ii = 13 - iif i = 1 thendrawtext("Jan",barindex-ii,mymin) coloured(204,0,204)endifif i = 2 thendrawtext("Feb",barindex-ii,mymin) coloured(204,0,204)endifif i = 3 thendrawtext("Mar",barindex-ii,mymin) coloured(204,0,204)endifif i = 4 thendrawtext("Apr",barindex-ii,mymin) coloured(204,0,204)endifif i = 5 thendrawtext("May",barindex-ii,mymin) coloured(204,0,204)endifif i = 6 thendrawtext("Jun",barindex-ii,mymin) coloured(204,0,204)endifif i = 7 thendrawtext("Jul",barindex-ii,mymin) coloured(204,0,204)endifif i = 8 thendrawtext("Aug",barindex-ii,mymin) coloured(204,0,204)endifif i = 9 thendrawtext("Sep",barindex-ii,mymin) coloured(204,0,204)endifif i = 10 thendrawtext("Oct",barindex-ii,mymin) coloured(204,0,204)endifif i = 11 thendrawtext("Nov",barindex-ii,mymin) coloured(204,0,204)endifif i = 12 thendrawtext("Dec",barindex-ii,mymin) coloured(204,0,204)endifnextreturn03/26/2020 at 1:58 PM #123417I found some time to work a little more on the v11 seasonality curve indicator and it is now something that stores in arrays the increase or decrease for every day of every year for all of history or from a chosen start date and then displays it as a curve representing a year.
The month labels represent the start of each month.
The availability of arrays makes the code very small compared to how it would need to be without them and it displays instantly thanks to ISLASTBARUPDATE. It was also an opportunity to use the new FLOOR instruction for rounding down and also ISSET.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980//Seasonality Curve//By Vonasi//Date: 20200326defparam drawonlastbaronly = true//StartDate = 19950101 //Set to zero to analyse all of historyif barindex > 0 and opendate >= startdate thenmydate = date - (floor(date/10000)*10000)$total[mydate] = $total[mydate] + (close - close[1])endifif islastbarupdate thenif barindex > 365 thenmymonth = 100myday = 1thisdate = mymonth + mydaylastsegment = 0drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)a = 365for b = 1 to 365mybar = barindex - aif isset($total[thisdate]) thendrawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])lastsegment = lastsegment + $total[thisdate]a = a - 1endifif myday = 31 thenmymonth = mymonth + 100myday = 0if mymonth = 200 thendrawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 300 thendrawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 400 thendrawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 500 thendrawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 600 thendrawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 700 thendrawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 800 thendrawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 900 thendrawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1000 thendrawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1100 thendrawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1200 thendrawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifendifmyday = myday + 1thisdate = mymonth + mydaymymax = max(lastsegment,mymax)once mymin = mymaxmymin = min(lastsegment,mymin)nextendifendifreturn mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)1 user thanked author for this post.
03/26/2020 at 2:39 PM #123432I forgot to say to only apply the indicator to the daily chart.
The first image is the 15k daily analysis of wheat so going right back to the early 60’s.
Not sure you would want to go long wheat in the first half of the year!
The second image is the same but just analysed from 01 Jan 1995. A bit more erratic due to the reduced amount of data but the trend is similar.
I will modify the code so that we can analyse from a start date to an end date and then we can compare in sample earlier data and see if the out of sample later data actually does a similar thing. That should give us more confidence that seasonality actually doe exist and still exists.
03/26/2020 at 2:56 PM #123445That modification was very easy. Here is the code that allows you to set a start date and an end date for the data to be analysed.
In the attached image of wheat we can see that the top graph which analyses the start of the 60’s up to the end of 1994 has very distinct seasonality but the lower graph which analyses from the start of 1995 to the present day is much more erratic and although some periods show similar seasonality it is in truth a very different chart.
There is a danger when using seasonality to analyse the whole of history and believe that is what it does every month of every year but by breaking it into two or more sample periods we can see that the markets have changed a lot. Computers started to be used for analysis and trading around the 90’s and I would guess that this has a lot to do with the differences as well as the fact that farming and transportation to other parts of the world has become much more efficient and so I guess wheat is now a lot less seasonal.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485//Seasonality Curve//By Vonasi//Date: 20200326defparam drawonlastbaronly = true//StartDate = 19950101 //Set to zero to analyse all of history//EndDate = 20200101 //Set to zero to analyse all of historyif enddate <= 0 or enddate < startdate thenenddate = 99999999endifif barindex > 0 and opendate >= startdate and opendate <= enddate thenmydate = date - (floor(date/10000)*10000)$total[mydate] = $total[mydate] + (close - close[1])endifif islastbarupdate thenif barindex > 365 thenmymonth = 100myday = 1thisdate = mymonth + mydaylastsegment = 0drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)a = 365for b = 1 to 365mybar = barindex - aif isset($total[thisdate]) thendrawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])lastsegment = lastsegment + $total[thisdate]a = a - 1endifif myday = 31 thenmymonth = mymonth + 100myday = 0if mymonth = 200 thendrawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 300 thendrawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 400 thendrawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 500 thendrawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 600 thendrawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 700 thendrawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 800 thendrawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 900 thendrawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1000 thendrawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1100 thendrawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1200 thendrawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifendifmyday = myday + 1thisdate = mymonth + mydaymymax = max(lastsegment,mymax)once mymin = mymaxmymin = min(lastsegment,mymin)nextendifendifreturn mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)03/26/2020 at 3:07 PM #12344903/26/2020 at 3:28 PM #123453Another small modification. This one marks the highest and lowest points in the seasonality curve. You can turn the markings off with the HighAndLow variable setting.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100//Seasonality Curve//By Vonasi//Date: 20200326defparam drawonlastbaronly = true//StartDate = 19950101 //Set to zero to analyse all of history//EndDate = 20200101 //Set to zero to analyse all of history//HighAndLow = 1 //switch on and off drawing of high and low pointsif enddate <= 0 or enddate < startdate thenenddate = 99999999endifif barindex > 0 and opendate >= startdate and opendate <= enddate thenmydate = date - (floor(date/10000)*10000)$total[mydate] = $total[mydate] + (close - close[1])endifif islastbarupdate thenif barindex > 365 thenmymonth = 100myday = 1thisdate = mymonth + mydaylastsegment = 0drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)a = 365for b = 1 to 365mybar = barindex - aif isset($total[thisdate]) thendrawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])lastsegment = lastsegment + $total[thisdate]a = a - 1endifif myday = 31 thenmymonth = mymonth + 100myday = 0if mymonth = 200 thendrawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 300 thendrawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 400 thendrawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 500 thendrawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 600 thendrawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 700 thendrawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 800 thendrawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 900 thendrawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1000 thendrawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1100 thendrawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1200 thendrawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifendifmyday = myday + 1thisdate = mymonth + mydaymymax = max(lastsegment,mymax)once mymin = lastsegmentonce mymax = lastsegmentmymin = min(lastsegment,mymin)if mymax = lastsegment thenmaxval = lastsegmentmaxbar = mybarendifif mymin = lastsegment thenminval = lastsegmentminbar = mybarendifnextif HighAndLow thendrawpoint(maxbar+1,maxval,3)coloured(128,0,0)drawpoint(minbar+1,minval,3)coloured(0,128,0)endifendifendifreturn mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)1 user thanked author for this post.
03/26/2020 at 3:33 PM #12345603/26/2020 at 4:01 PM #123464Another small improvement – I really should stop now! This version lets you choose whether the analysis is done with actual price or price as a percentage of the previous close. I personally think that the latter is better when we are comparing two different data samples from two very different times.
The image shows platinum with the top chart being calculated using percentage of price and the lower one calculated on actual price.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108//Seasonality Curve//By Vonasi//Date: 20200326defparam drawonlastbaronly = true//StartDate = 19950101 //Set to zero to analyse all of history//EndDate = 20200101 //Set to zero to analyse all of history//HighAndLow = 1 //switch on and off drawing of high and low points//Percentage = 1 //switch on or off calcualtion based on percentage of priceif enddate <= 0 or enddate < startdate thenenddate = 99999999endifif barindex > 0 and opendate >= startdate and opendate <= enddate thenif percentage thendiff = (((close - close[1])/close[1])*100)elsediff = close - close[1]endifmydate = date - (floor(date/10000)*10000)$total[mydate] = $total[mydate] + diffendifif islastbarupdate thenif barindex > 365 thenmymonth = 100myday = 1thisdate = mymonth + mydaylastsegment = 0drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)a = 365for b = 1 to 365mybar = barindex - aif isset($total[thisdate]) thendrawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])lastsegment = lastsegment + $total[thisdate]a = a - 1endifif myday = 31 thenmymonth = mymonth + 100myday = 0if mymonth = 200 thendrawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 300 thendrawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 400 thendrawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 500 thendrawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 600 thendrawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 700 thendrawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 800 thendrawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 900 thendrawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1000 thendrawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1100 thendrawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifif mymonth = 1200 thendrawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)endifendifmyday = myday + 1thisdate = mymonth + mydaymymax = max(lastsegment,mymax)once mymin = lastsegmentonce mymax = lastsegmentmymin = min(lastsegment,mymin)if mymax = lastsegment thenmaxval = lastsegmentmaxbar = mybarendifif mymin = lastsegment thenminval = lastsegmentminbar = mybarendifnextif HighAndLow thendrawpoint(maxbar+1,maxval,3)coloured(128,0,0)drawpoint(minbar+1,minval,3)coloured(0,128,0)endifendifendifreturn mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)03/26/2020 at 4:08 PM #123467It can be interesting to put multiple charts on the same screen. In the image we have three charts, pre 1995, post 1995 and on all the data for Lean Hogs using percentage of price as the calculation method. Looks like there could be some definite seasonality to use there.
I’ll submit the indicator to the library.
03/26/2020 at 5:01 PM #12348203/26/2020 at 6:42 PM #123498Excellent!
1 user thanked author for this post.
02/23/2022 at 6:37 PM #188724Sono nuovo del Market, ho importato il codice della stagionalità, ma non vedo nulla. Precisamente nel lanciarlo , appare un riquadro alla cui base vi sono le date impostate e poi più nulla.Puoi aiutarmi?I’m new to the Market, I imported the seasonality code, but I don’t see anything. Precisely when launching it, a box appears at the base of which there are the dates set and then nothing more. can you help me?
02/23/2022 at 9:23 PM #188732Only post in the language of the forumthat you are posting in. For example English only in the English speaking forums and French only in the French speaking forums,
Thank you 🙂
-
AuthorPosts
Find exclusive trading pro-tools on