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
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
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
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
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?
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!
And when you finally sorted all out, IG changes the lot into an even mere mess (Sep 2025).
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…).
- 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.
- 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
JSParticipant
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…
correct, JS
but it seems to work with:
if month <> month[1]
then sell at market
endif
cheers
JSParticipant
Senior
“Seems to work” is not good enough… 🙂
…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…
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.