Seasonality Curve using arrays.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #121052 quote
    Vonasi
    Moderator
    Master

    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.

    defparam drawonlastbaronly = true
    
    $month[openmonth] = $month[openmonth] + (close-open)
    $count[openmonth] = $count[openmonth] + 1
    
    $avg[openmonth] = $month[openmonth]/$count[openmonth]
    
    mymin = 0
    value = 0
    for i = 1 to 12
    ii = 13 - i
    drawsegment(barindex-ii,value,barindex-(ii)+1,value + $avg[i])
    value = value + $avg[i]
    mymin = min(mymin, value)
    next
    
    for i = 1 to 12
    ii = 13 - i
    if i = 1 then
    drawtext("Jan",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 2 then
    drawtext("Feb",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 3 then
    drawtext("Mar",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 4 then
    drawtext("Apr",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 5 then
    drawtext("May",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 6 then
    drawtext("Jun",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 7 then
    drawtext("Jul",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 8 then
    drawtext("Aug",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 9 then
    drawtext("Sep",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 10 then
    drawtext("Oct",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 11 then
    drawtext("Nov",barindex-ii,mymin) coloured(204,0,204)
    endif
    if i = 12 then
    drawtext("Dec",barindex-ii,mymin) coloured(204,0,204)
    endif
    next
    
    return 
    
    Nicolas and Bel thanked this post
    #123417 quote
    Vonasi
    Moderator
    Master

    I 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.

     

    //Seasonality Curve
    //By Vonasi
    //Date: 20200326
    
    defparam drawonlastbaronly = true
    
    //StartDate = 19950101 //Set to zero to analyse all of history
    
    if barindex > 0 and opendate >= startdate then
    mydate = date - (floor(date/10000)*10000)
    $total[mydate] = $total[mydate] + (close - close[1])
    endif
    
    if islastbarupdate then
    if barindex > 365 then
    mymonth = 100
    myday = 1
    thisdate = mymonth + myday
    lastsegment = 0
    drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)
    
    a = 365
    for b =  1 to 365
    mybar = barindex - a
    if isset($total[thisdate]) then
    drawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])
    lastsegment = lastsegment + $total[thisdate]
    a = a - 1
    endif
    
    if myday = 31 then
    mymonth = mymonth + 100
    myday = 0
    
    if mymonth = 200 then
    drawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 300 then
    drawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 400 then
    drawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 500 then
    drawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 600 then
    drawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 700 then
    drawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 800 then
    drawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 900 then
    drawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1000 then
    drawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1100 then
    drawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1200 then
    drawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    endif
    
    myday = myday + 1
    thisdate = mymonth + myday
    
    mymax = max(lastsegment,mymax)
    once mymin = mymax
    mymin = min(lastsegment,mymin)
    next
    endif
    endif
    
    return mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)
    
    Kovit thanked this post
    #123432 quote
    Vonasi
    Moderator
    Master

    I 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.

    #123445 quote
    Vonasi
    Moderator
    Master

    That 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.

    //Seasonality Curve
    //By Vonasi
    //Date: 20200326
    
    defparam drawonlastbaronly = true
    
    //StartDate = 19950101 //Set to zero to analyse all of history
    //EndDate = 20200101 //Set to zero to analyse all of history
    
    if enddate <= 0 or enddate < startdate then
    enddate = 99999999
    endif
    
    if barindex > 0 and opendate >= startdate and opendate <= enddate then
    mydate = date - (floor(date/10000)*10000)
    $total[mydate] = $total[mydate] + (close - close[1])
    endif
    
    if islastbarupdate then
    if barindex > 365 then
    mymonth = 100
    myday = 1
    thisdate = mymonth + myday
    lastsegment = 0
    drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)
    
    a = 365
    for b =  1 to 365
    mybar = barindex - a
    if isset($total[thisdate]) then
    drawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])
    lastsegment = lastsegment + $total[thisdate]
    a = a - 1
    endif
    
    if myday = 31 then
    mymonth = mymonth + 100
    myday = 0
    
    if mymonth = 200 then
    drawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 300 then
    drawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 400 then
    drawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 500 then
    drawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 600 then
    drawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 700 then
    drawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 800 then
    drawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 900 then
    drawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1000 then
    drawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1100 then
    drawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1200 then
    drawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    endif
    
    myday = myday + 1
    thisdate = mymonth + myday
    
    mymax = max(lastsegment,mymax)
    once mymin = mymax
    mymin = min(lastsegment,mymin)
    next
    endif
    endif
    
    return mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)
    
    #123449 quote
    Vonasi
    Moderator
    Master

    Soybean however looks pretty similar for the two sample periods.

    #123453 quote
    Vonasi
    Moderator
    Master

    Another 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.

    //Seasonality Curve
    //By Vonasi
    //Date: 20200326
    
    defparam 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
    
    if enddate <= 0 or enddate < startdate then
    enddate = 99999999
    endif
    
    if barindex > 0 and opendate >= startdate and opendate <= enddate then
    mydate = date - (floor(date/10000)*10000)
    $total[mydate] = $total[mydate] + (close - close[1])
    endif
    
    if islastbarupdate then
    
    if barindex > 365 then
    mymonth = 100
    myday = 1
    thisdate = mymonth + myday
    lastsegment = 0
    drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)
    
    a = 365
    for b =  1 to 365
    mybar = barindex - a
    if isset($total[thisdate]) then
    drawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])
    lastsegment = lastsegment + $total[thisdate]
    a = a - 1
    endif
    
    if myday = 31 then
    mymonth = mymonth + 100
    myday = 0
    
    if mymonth = 200 then
    drawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 300 then
    drawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 400 then
    drawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 500 then
    drawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 600 then
    drawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 700 then
    drawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 800 then
    drawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 900 then
    drawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1000 then
    drawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1100 then
    drawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1200 then
    drawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    endif
    
    myday = myday + 1
    thisdate = mymonth + myday
    
    mymax = max(lastsegment,mymax)
    once mymin = lastsegment
    once mymax = lastsegment
    mymin = min(lastsegment,mymin)
    if mymax = lastsegment then
    maxval = lastsegment
    maxbar = mybar
    endif
    if mymin = lastsegment then
    minval = lastsegment
    minbar = mybar
    endif
    next
    if HighAndLow then
    drawpoint(maxbar+1,maxval,3)coloured(128,0,0)
    drawpoint(minbar+1,minval,3)coloured(0,128,0)
    endif
    endif
    endif
    
    return mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)
    
    Bel thanked this post
    #123456 quote
    Vonasi
    Moderator
    Master

    Lean Hogs pre 1995 and post 1995. Pretty similar charts.

    #123464 quote
    Vonasi
    Moderator
    Master

    Another 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.

    //Seasonality Curve
    //By Vonasi
    //Date: 20200326
    
    defparam 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 price
    
    if enddate <= 0 or enddate < startdate then
    enddate = 99999999
    endif
    
    if barindex > 0 and opendate >= startdate and opendate <= enddate then
    
    if percentage then
    diff = (((close - close[1])/close[1])*100)
    else
    diff = close - close[1]
    endif
    
    mydate = date - (floor(date/10000)*10000)
    $total[mydate] = $total[mydate] + diff
    endif
    
    if islastbarupdate then
    
    if barindex > 365 then
    mymonth = 100
    myday = 1
    thisdate = mymonth + myday
    lastsegment = 0
    drawtext("Jan",barindex - 365,0,SansSerif,Bold,12)coloured(0,0,255)
    
    a = 365
    for b =  1 to 365
    mybar = barindex - a
    if isset($total[thisdate]) then
    drawsegment(mybar,lastsegment,mybar+1,lastsegment + $total[thisdate])
    lastsegment = lastsegment + $total[thisdate]
    a = a - 1
    endif
    
    if myday = 31 then
    mymonth = mymonth + 100
    myday = 0
    
    if mymonth = 200 then
    drawtext("Feb",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 300 then
    drawtext("Mar",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 400 then
    drawtext("Apr",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 500 then
    drawtext("May",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 600 then
    drawtext("Jun",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 700 then
    drawtext("Jul",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 800 then
    drawtext("Aug",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 900 then
    drawtext("Sep",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1000 then
    drawtext("Oct",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1100 then
    drawtext("Nov",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    if mymonth = 1200 then
    drawtext("Dec",mybar,lastsegment - $total[thisdate],SansSerif,Bold,12)coloured(0,0,255)
    endif
    endif
    
    myday = myday + 1
    thisdate = mymonth + myday
    
    mymax = max(lastsegment,mymax)
    once mymin = lastsegment
    once mymax = lastsegment
    mymin = min(lastsegment,mymin)
    if mymax = lastsegment then
    maxval = lastsegment
    maxbar = mybar
    endif
    if mymin = lastsegment then
    minval = lastsegment
    minbar = mybar
    endif
    next
    if HighAndLow then
    drawpoint(maxbar+1,maxval,3)coloured(128,0,0)
    drawpoint(minbar+1,minval,3)coloured(0,128,0)
    endif
    endif
    endif
    
    return mymax coloured(0,0,0,0), mymin coloured(0,0,0,0)
    
    #123467 quote
    Vonasi
    Moderator
    Master

    It 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.

    #123482 quote
    Vonasi
    Moderator
    Master

    Heating Oil looks pretty seasonal.

    #123498 quote
    Rory Dryden
    Participant
    New

    Excellent!

    Vonasi thanked this post
    #188724 quote
    Ciccarelli Franco
    Participant
    Junior

    Sono 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?

    #188732 quote
    robertogozzi
    Moderator
    Master

    @Ciccarelli Franco

    Only 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 🙂

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

Seasonality Curve using arrays.


ProBuilder support

New Reply
Author
author-avatar
Vonasi @vonasi Moderator
Summary

This topic contains 12 replies,
has 2 voices, and was last updated by robertogozzi
3 years, 11 months ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 03/03/2020
Status: Active
Attachments: 11 files
Logo Logo
Loading...