Strategy: monthly close exit with daily entries

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #257633 quote
    theunpredictable
    Participant
    New

    Hi,


    I am looking for a solution that allows me to display entries on a daily basis at the open and exits at the close. The close should always take place on the last day of the month. If it does not work on a daily basis, I can also switch to a smaller time unit. Are there any solutions for this?


    Thanks in advance


    Greets Björn

    #257647 quote
    robertogozzi
    Moderator
    Master

    This code will enter at the opening of each new day and will close at the end of the day.

    If you want to close at a different time, just let me know.

    It must be used on an intraday timeframe:

    Timeframe(Daily,default)
    IF abs(CountOfPosition) = 0 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    //
    Timeframe(default)
    IF Day <> Day[1] THEN
       SELL AT MARKET
    ENDIF
    
    Iván González thanked this post
    #257701 quote
    theunpredictable
    Participant
    New

    Thanks for your reply Roberto!


    If do it like that it doesn’t work:



    Timeframe(Daily,default)

    IF abs(CountOfPosition) = 0 and day >20 and day<26 THEN

    BUY 1 CONTRACT AT MARKET

    ENDIF

    //

    Timeframe(default)



    IF Day <> Day[1] THEN

    SELL AT MARKET

    ENDIF


    In the picture you can see that the exit is on 26. of January not at the end of mont per close.

    Do you have an idea why?


    Timeframe is 1H.


    Thank you.

    Timeframe(Daily,default)
    IF abs(CountOfPosition) = 0 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    //
    Timeframe(default)
    IF Day <> Day[1] THEN
       SELL AT MARKET
    ENDIF
    


    #257852 quote
    robertogozzi
    Moderator
    Master

    Try this version:

    Timeframe(Daily,default)
    IF abs(CountOfPosition) = 0 and day >20 and day<26 THEN
       BUY 1 CONTRACT AT MARKET
    ENDIF
    //
    Timeframe(Monthly,default)
    IF Month <> Month[1] THEN
       SELL AT MARKET
    ENDIF
    
    #258129 quote
    justisan
    Participant
    Junior

    hi Björn, can you define mathematically „end of the month“? so definition is valid 10 (or 1000) years into the past – and also into the future?

    #258142 quote
    robertogozzi
    Moderator
    Master


    justisan wrote: hi Björn, can you define mathematically „end of the month“? so definition is valid 10 (or 1000) years into the past – and also into the future?

    Yes, if the month is Apr, Jun, Sep or Nov, then it’s the 30th, while on Feb. it’s usually the 28th unless it’s a leap year in which case it’s the 29th. For all other months it’s the 31st.

    What is difficult to find out is to know whether the last two days of the month are trading days, because if you want to close any trade on the very last day, you must determine whether it’s a trading day (which sometimes is impossible due to holidays, unless you, on Jan.1st, hard code them), but since the order to close a trade must be placed the PRIOR bar, then you have to determine also whether the PRIOR day is a trading day or not (and scan days backwards until you find both, which are not necessarily consecutive). To make it more difficult is that for some timeframes the Sunday is a trading day, while for others it is not. To make things worse is that, for some timeframes, the day number changes at 00:00am, while the DayOfTheWeek changes at 01:00am.

    Indeed it’s quite simple to find the last day of the month mathematically, while it’s fairly difficult to determine whether on that day it is possible to close a trade for which the order had to be placed the day before!

    #258143 quote
    PeterSt
    Participant
    Master

    And when you finally sorted all out, IG changes the lot into an even mere mess (Sep 2025).

    robertogozzi thanked this post
    #258260 quote
    justisan
    Participant
    Junior

    yep roberto, what counts is of course not last calendar day of the month – which is easy to define yet leads to no solution, but the last trading day of the month (working day of specific exchange) – which to define is a totally different task. and according my knowledge there is simply no way one can capture that day by prc. in order to capture it prc would need to have exchange calendar database in the background with all weekends and exchange holidays, and a function which makes it possible to count working days of the month ideally forward and backward. in the miraculous cfd-world it is even more complicated since broker might allow trading days and hours when the corresponding exchange is in fact closed.

    still I can think of at least 2 work-arounds for Björn’s issue (and in fact for my own…).

    1. for the backtest as well as for the future (if going live with particular algo) to capture manually last working days (or second last or x-last…) of each month for relevant exchange for period of interest and put it into code. well, it takes some hours, but it might be worth the effort. and corresponding prompt for some AI tool might accelerate the process.
    2. much less / kind of no effort but gives just approximation as solution: replace the rule “close the trade last working day of the month” by “close the trade first working day of the month”. while prc does not “know” which one is the last working day, it absolutely precisely “knows” which is the first working day of the month: put into the code “if day = 1 then / sell at market / endif” – and here you go, your trade is closed after the first bar is closed (what ever timeframe you chose lower than daily) on the first working day of the month.

    happy trading!

    justisan

    #258267 quote
    JS
    Participant
    Senior

    Day = 1 refers to the first calendar day of the month, not to the first trading day of the month…

    When “Day = 1” falls on a holiday or during the weekend: “If Day = 1 then / Sell at Market / EndIf” will not work…

    justisan thanked this post
    #258270 quote
    justisan
    Participant
    Junior

    correct, JS

    but it seems to work with:

    if month <> month[1]

    then sell at market

    endif

    cheers


    #258273 quote
    JS
    Participant
    Senior

    “Seems to work” is not good enough… 🙂

    #258289 quote
    justisan
    Participant
    Junior

    …is it not? 😀

    works perfectly as expected for dax futures for last ~20 year. up to those interested if it works for those 50+k instruments available on cfds…

    #258290 quote
    justisan
    Participant
    Junior

    i meant fifteen+ k instruments (not fifty), but who cares. i mean – i have no aim to check everything for everybody, everybody has to check the idea on his own, at his own risk 🙂 not believing me, not believing anybody in the world, using his/her own brains.

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

Strategy: monthly close exit with daily entries


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 12 replies,
has 5 voices, and was last updated by justisan
2 weeks, 1 day ago.

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