Trading Day of Month

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #114902 quote
    Thally
    Participant
    Average

    Hi all,

     

    I want to code out a simple system that goes long on the 3rd to last trading day of the month and then exits on the last day of the month. Does anyone know how to code this simple idea? I can’t fathom how to reliably identify the trading day of the month, using the DAY, MONTH, YEAR functions.

    Any help would be greatly appreciated.

     

    Thanks

    #114905 quote
    robertogozzi
    Moderator
    Master

    There you go (non tested):

    Once Count   = 0
    Once TradeON = 1
    If OpenMonth <> OpenMonth[1] then
          TradeON = 0
           Count  = 0
    Endif
    If OpenDay <> OpenDay[1] Then
            Count = Count + 1
    Endif
    If Count > 2 then
       TradeON = 1
    Endif

    you need to add TradeON to your conditions to enter a trade.

    #114909 quote
    Vonasi
    Moderator
    Master

    Roberto – That is the third day of the month and not the third to last day of the month.

    We will need some calculation based on month and year to know how many days are in any month so that we can decide if it is three days from the end of the month.

    We also need to know if we are counting trading days or actual days. It is more complicated than I want to get into after a glass of wine!

    #114911 quote
    robertogozzi
    Moderator
    Master

    He wrote “on the 3rd to last trading day”.

    #114922 quote
    Vonasi
    Moderator
    Master

    Third to last is three trading days before the end of the month. Your code starts counting three days from the start of the month.

    We need to know which month and year it is and then calculate how many days are in that month (allowing for leap years), then we need to know what day of the week three days before the end is and tweak our count back if it is a Sunday (unless the OP is happy with six candle weeks). We will then know what the third trading day before the end of the months date is and can just check to see if today is that day or not.

    #114923 quote
    robertogozzi
    Moderator
    Master

    This is some date math functions I made, despite many errors in past years data https://www.prorealcode.com/topic/tomorrows-date/

    So at the beginning of each month trading should be disabled, then max days in the month should be detected, then counting backwords from the last day, 3 trading days need to be found, to reenable trading.

    It’s not as straightforward but can be done.

    #114943 quote
    Thally
    Participant
    Average

    Cheers guys. I figured it would be a challenge. I’ll have a play around with Roberto’s functions.

    #114979 quote
    Nicolas
    Keymaster
    Master

    Maybe something of interest related to this strategy here: (snippets to find  last day and last trading day of the month)

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

    #115036 quote
    robertogozzi
    Moderator
    Master

    This sindicator will return the 3rd last trading day of the month (if the month ends on Monday it will return Thursday). It uses my functions that will be automatically imported when you import the indicator:

    ONCE DayStart = 0                   //day of the month on which trading will start
    IF OpenMonth <> OpenMonth[1] THEN
       DayStart = 0
       Count    = 0
       LastDay  = CALL DayMax[OpenMonth,OpenYear]
       FOR i = LastDay downto 1
          MyDate= (OpenYear  * 10000) + (OpenMonth * 100) + i
          MyDOW = CALL GetDayOfWeek[MyDate]
          IF MyDow > 0 AND MyDow < 6 THEN
             Count = Count + 1
             IF Count = 3 THEN
                DayStart = i
                Break
             ENDIF
          ENDIF
       NEXT
    ENDIF
    RETURN DayStart

    You can clear a flag, say variable TradeON (set it to  0), at the beginning of the month, then reenable it when OpenDay=DayStart.

    You can remove OPEN from Date, Year and Month if you prefer using those returned when a candle close, instead of when it opens.

    This is the sample code for the strategy:

    DEFPARAM CumulateOrders = false
    ONCE TradeON = 0     //trading disabled by default
    IF OpenMonth <> OpenMonth[1] THEN
       TradeON   = 0     //trading disabled at the beginnin of each month
       DayStart  = CALL "My Indicator"
    ENDIF
    IF OpenDay = DayStart THEN
       TradeON = 1
    ENDIF
    IF Not OnMarket AND TradeON = 1 THEN
       BUY 1 CONTRACT AT MARKET
       SET TARGET pPROFIT 100
       SET STOP   pLOSS   50
    ENDIF
    Nicolas, Thally and atxeel thanked this post
    My-Indicator.itf
    #115125 quote
    Thally
    Participant
    Average

    Thanks you so much Roberto, that really is a very useful set of functions. Many thanks for sharing them and helping with this. Alas the system ideas was not profitable, over the long term. The anecdotal evidence over the last few years looked like the might have been some alpha there in the T bonds but going back to 2000 it fails miserably. Nevermind. Good be able to test it.

    robertogozzi thanked this post
    #184855 quote
    814591
    Participant
    New

    Hi guys many thanks for this thread. Roberto – I have one question to u – What about if I want to go long on a specfic day and date – how can I do this. I tried this but does not work:

    //Set Buy- and SellTime
    BuyTime = 0
    SellTime = 0
    //Set Entry date

    IF (date = 20210113) and (time=220000) Then
    BuyTime = 1
    ENDIF

    IF (date = 20210118) and (time=220000) Then
    SellTime = 1
    ENDIF

    Greetings

    Salah

    #184857 quote
    robertogozzi
    Moderator
    Master

    What timeframe are you using?

    #184858 quote
    814591
    Participant
    New

    daily

    #184860 quote
    robertogozzi
    Moderator
    Master

    That’s why it doesn’t work. In a Daily TF you cannot access times, so there will never be 220000.

    This is because code is always executed when a candle closes.

    You have to have to use a TF with candles closing on a 1-hour boundary.

    #184984 quote
    814591
    Participant
    New

    Many thanks Robert – what do I need to change in my code then?

    Greetings

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

Trading Day of Month


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Thally @rodger_dodger Participant
Summary

This topic contains 16 replies,
has 2 voices, and was last updated by robertogozzi
4 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/14/2019
Status: Active
Attachments: 1 files
Logo Logo
Loading...