Prevent you from operating a specific month and week

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #173300 quote
    Paolo.B
    Participant
    Average

    I am trying, but in vain, not to operate in a month and week of the year.
    Example I would like to prevent opening a position in the first and third week of October.
    If anyone is able to help me
    Thank you

    #173308 quote
    robertogozzi
    Moderator
    Master

    DATE & TIME data typee are not supported, so MATH (eXcel-like) functions are not available.
    There’s a workaround… just wait for the desired month and start counting weeks. The one that contains day 1 is the first week if that day is on Thursday or earlier (4+ out of 7 days must belong to the new month), otherwise it’s the last week of the prior month despite having day 1.
    You start enabling trading and will disable it when the required weeks show.
    This is an indicator you can add to your chart to tell week number for October:

    ONCE Wcount = 0
    // only check October
    IF OpenMonth = 10 THEN
       // check if it's the first week
       IF Wcount = 0 THEN
          // check if it's the first trading day of October
          IF OpenMonth <> OpenMonth[1] THEN
             // it's the first week of October of the firtst day is Thursday or earlier
             IF OpenDayOfWeek <= 4 THEN
                Wcount = 1
             ENDIF
          ELSE
             // if the first day was later than Thursday, then we have to wait for the next Monday
             IF (OpenDayOfWeek >= 1) AND (OpenDayOfWeek <> OpenDayOfWeek[1]) AND ((OpenDayOfWeek < OpenDayOfWeek[1]) OR (OpenDayOfWeek[1] = 0)) THEN
                Wcount = 1
             ENDIF
          ENDIF
       ELSE
          // or any other day in October (if it's Monday or it's the first day of the week, then it's a new week)
          IF (OpenDayOfWeek >= 1) AND (OpenDayOfWeek <> OpenDayOfWeek[1]) AND ((OpenDayOfWeek < OpenDayOfWeek[1]) OR (OpenDayOfWeek[1] = 0)) THEN
             Wcount = Wcount + 1
          ENDIF
       ENDIF
       // not October
    ELSE
       Wcount = 0
    ENDIF
    RETURN Wcount AS "Week"

    and this is the strategy:

    DEFPARAM CumulateOrders = false
    ONCE Wcount = 0
    TradeON     = 1
    // only check October
    IF OpenMonth = 10 THEN
    // check if it's the first week
    IF Wcount = 0 THEN
    // check if it's the first trading day of October
    IF OpenMonth <> OpenMonth[1] THEN
    // it's the first week of October of the firtst day is Thursday or earlier
    IF OpenDayOfWeek <= 4 THEN
    Wcount = 1
    ENDIF
    ELSE
    // if the first day was later than Thursday, then we have to wait for the next Monday
    IF (OpenDayOfWeek >= 1) AND (OpenDayOfWeek <> OpenDayOfWeek[1]) AND ((OpenDayOfWeek < OpenDayOfWeek[1]) OR (OpenDayOfWeek[1] = 0)) THEN
    Wcount = 1
    ENDIF
    ENDIF
    ELSE
    // or any other day in October (if it's Monday or it's the first day of the week, then it's a new week)
    IF (OpenDayOfWeek >= 1) AND (OpenDayOfWeek <> OpenDayOfWeek[1]) AND ((OpenDayOfWeek < OpenDayOfWeek[1]) OR (OpenDayOfWeek[1] = 0)) THEN
    Wcount = Wcount + 1
    ENDIF
    ENDIF
    // not October
    ELSE
    Wcount = 0
    ENDIF
    // disable trading in weeks 1 and 3 of October
    IF Wcount = 1 OR Wcount = 3 THEN
    TradeON = 0
    ENDIF
    IF close CROSSES OVER average[20,0](close) AND Not LongOnMarket AND TradeON THEN
    BUY 1 CONTRACT AT MARKET
    ELSIF close CROSSES UNDER average[20,0](close) AND Not ShortOnMarket AND TradeON THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    Paolo.B and Midlanddave thanked this post
    x-2.jpg x-2.jpg
    #173312 quote
    Paolo.B
    Participant
    Average

    Thanks Roberto,
    Very kind as always.

    #173315 quote
    Paolo.B
    Participant
    Average

    It works wonders
    Thanks again

    #173335 quote
    robertogozzi
    Moderator
    Master

    I modified it so that you can either:

    • choose any month (and any week in it)
    • choose any week of the year based solely on its year week ID
    • choose any whole month
    DEFPARAM CumulateOrders = false
    ////////////////////////////////////////////////////////////////////////////////
    //                              Week Count
    //
    //returned data:
    //
    // - Wcount  Week num of the month
    // - LastMM  Month to which the week belongs (can be different from that on chart)
    // - Wyear   Week num of the year
    //
    ONCE Wcount = 0
    ONCE Wyear  = 0
    ONCE LastMM = 06
    mm          = OpenMonth
    wd          = OpenDayOfWeek
    yy          = OpenYear
    dd          = OpenDay
    DayMax      = 31           //each month has 31 days by default
    //                          (below, exceptions will be dealt with)
    // check for any month with less than 31 days
    IF mm = 4 OR mm = 6 OR mm = 9 OR mm = 11 THEN  //Apr, Jun, Sep and Nov have 30 days
    DayMax   = 30
    ELSIF mm = 2 THEN                              //Feb has 28 days (29 on Leap Years, which occur on almost all 4-year boundaries)
    // check whether it's a Leap Year for February
    DayMax = 28                    //let's default to 28
    IF yy MOD 4 = 0 THEN
    IF yy MOD 100 = 0 THEN      //NOT a Leap if it, as it can be divided by 100, but not by 400
    IF yy MOD 400 = 0 THEN
    DayMax = 29           //it's a Leap Year, since it can be divded by both 100 and 400
    ENDIF
    ELSE
    DayMax    = 29           //can be divided by 4 and is not a century start, so it's a Leap Year
    ENDIF
    ENDIF
    ENDIF
    // check & count every Monday (or later, if Monday is not a trading day)
    IF (wd >= 1) AND (wd <> wd[1]) AND ((wd < wd[1]) OR (wd[1] = 0)) THEN
    Wyear  = Wyear + 1
    Diff   = (DayMax - dd) + 1
    LastMM = mm
    IF LastMM <> LastMM[1] THEN
    Wcount = 1                     //restart from 1 each new month when Monday os the 1st day
    ELSE
    IF Diff >= 4 THEN              //if not a full week, then it belongs to the prior month if at least 4
    Wcount = Wcount + 1         //  days belong to the prior month
    ELSE
    Wcount = 1                  //  otherwise it's the 1st week of the new month
    LastMM = LastMM + 1
    IF LastMM > 12 THEN
    LastMM = LastMM - 12     //when computing the new month, make sure it's not greater than 12, if
    ENDIF                       //  it is, then it's January
    ENDIF
    ENDIF
    ENDIF
    // restart year count on Jan. 1st
    IF (LastMM = 1) AND (LastMM <> LastMM[1]) THEN
    Wyear = 1
    ENDIF
    ////////////////////////////////////////////////////////////////////////////////
    TradeON = 1
    // disable trading in weeks 1 and 3 of October
    IF (LastMM = 10) AND (Wcount = 1 OR Wcount = 3) THEN
    TradeON = 0
    ENDIF
    IF close CROSSES OVER average[20,0](close) AND Not LongOnMarket AND TradeON THEN
    BUY 1 CONTRACT AT MARKET
    ELSIF close CROSSES UNDER average[20,0](close) AND Not ShortOnMarket AND TradeON THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF

    you change line 62 to use more months:

    IF ((LastMM = 10) AND (Wcount = 1 OR Wcount = 3)) OR (LastMM = 1 AND Wcount < 3) THEN
       TradeON = 0
    ENDIF

    or use it for yearly week ID (1 to max 53):

    IF Wyear < 3 OR Wyear > 50 THEN
       TradeON = 0
    ENDIF

    or use any other combination of the three values:

    IF (LastMM = 10 AND Wcount = 1) OR (Wyear > 51) OR (LastMM = 8) THEN
       TradeON = 0
    ENDIF
    #173340 quote
    Paolo.B
    Participant
    Average

    Definitely complete.
    Thanks now I try it.

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

Prevent you from operating a specific month and week


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Paolo.B @paolo-b Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Paolo.B
4 years, 7 months ago.

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