coding help about buy and sell on certain days of the month

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #46506 quote
    roq1000
    Participant
    New

    Hi there,

    I am a new ProRealTime user and just working my way through the programming manual in order to get to grips with the programming language!

    It would be ever so helpful if someone could tell me the correct code for the following – it would help with my understanding of how the specific monthly date element of coding works:

    Buy on the last 2 trading days of the month and sell at the close on the 3rd trading day of the month

     

    If someone would be kind enough to tell me the code for the above lines, it would help me as I work through the ProRealTime coding manual.

    Many thanks for your help!

    Rob

    #46525 quote
    Nicolas
    Keymaster
    Master

    I wrote functions to know if we are on the last day of month for someone else, here are the codes:

    This indicator will tell you the number of the last traded day of the current month (save it as an indicator named “#lastdayofmonth”):

    //bisextil year or not?
    If Year Mod 4 = 0 And (Year Mod 100 <> 0 Or Year Mod 400 = 0) Then
    IsBisextil = 1
    Else
    IsBisextil = 0
    EndIf
    
    //days quantity per month
    if IsBisextil then
    feb=29
    else
    feb=28
    endif
    
    //get the last day of the current month
    if month=1 or month=3 or month=5 or month=7 or month=10 or month=12 then
    lastdayofmonth = call "#get_lastdayofmonth"[year,month,31]
    elsif month=4 or month=6 or month=8 or month=9 or month=11 then
    lastdayofmonth = call "#get_lastdayofmonth"[year,month,30]
    elsif month=2 then
    lastdayofmonth = call "#get_lastdayofmonth"[year,month,feb]
    endif
    
    return lastdayofmonth

    This code above need this other function (saved as an indicator named “#get_lastdayofmonth”).

    //what is the day of week?
    if myMonth >= 3 then
    D = (((23*myMonth)/9) + myDay + 4 + myYear + (myYear/4) - (myYear/100) + (myYear/400) - 2) mod 7
    else
    z = myYear - 1
    D = (((23*myMonth)/9) + myDay + 4 + myYear + (z/4) - (z/100) + (z/400) ) mod 7
    endif
    
    lastday=myDay
    if D=6 then
    lastday=myDay-1
    elsif D=0 then
    lastday=myDay-2
    endif
    
    return lastday

    So in your strategy, you’ll have to code something like:

    lastdayofmonth = CALL "#lastdayofmonth"
    
    //buy the last day of the month 
    if day=lastdayofmonth then 
     buy 1 contract at market 
    endif 
    
    //sell at 3rd day of the next month 
    if longonmarket and day>=3 then 
     sell at market 
    endif 
    
    #46725 quote
    roq1000
    Participant
    New

    Many thanks Nicolas, that’s helped me get to grips with things!

    #112415 quote
    Bel
    Participant
    New

    Hi Nicholas.

    I’m interested in buying or selling at start or end of the month but have no idea how to code it right except manually enter dates. Any help possible?

    P.s. tried this code but can’t get to work 🙁

    #112419 quote
    Bel
    Participant
    New

    Hi Nicholas.

    I’m interested in buying or selling at start or end of the month but have no idea how to code it right except manually enter dates. Any help possible?

    P.s. tried this code but can’t get to work 🙁

    I think I worked it out right after posting. All good. Lets see how it rolls.

    #164512 quote
    teapot2002
    Participant
    Average

    If, like me, you’d forgotten how to pass parameters to indicators, this is very helpful: https://www.prorealcode.com/blog/video-tutorials/adding-dynamic-parameters-to-your-indicators-prorealtime/

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

coding help about buy and sell on certain days of the month


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
roq1000 @chequebook Participant
Summary

This topic contains 5 replies,
has 1 voice, and was last updated by teapot2002
4 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/18/2017
Status: Active
Attachments: No files
Logo Logo
Loading...