Hi all,
I want to go long at the close on the fifth last trading day of the month, and exit at the close of the third trading day of the next month. Anyone able to make script for this strategy?
Hi! here you have a code. It must be backtested in daily timeframe.
myday=openday
myDayOfWeek = opendayofweek
MyMonth=openmonth
MyYear=openyear
businessdaysleft = 0
//------------------------------------------//
//-----Check leap year----------------------//
//------------------------------------------//
//input:YEAR
LeapYear = 0
If MyYear mod 4 = 0 then
If MyYear mod 100 = 0 then
If MyYear mod 400 = 0 then
LeapYear = 1
Endif
Else
LeapYear = 1
Endif
Endif
//------------------------------------------//
//-----Calculate max days in month----------//
//------------------------------------------//
MaxDay = 31
If MyMonth = 4 or MyMonth = 6 or MyMonth = 9 or MyMonth = 11 Then
MaxDay = 30
Endif
If MyMonth = 2 then
x = LeapYear
MaxDay = 28 + x
Endif
myDayMax=MaxDay
FOR count = myday to myDayMax DO
IF myDayOfWeek >= 1 and myDayOfWeek <= 5 THEN
businessdaysleft = businessdaysleft + 1
ENDIF
myDayOfWeek = (myDayOfWeek + 1) MOD 7
NEXT
//------------------------------------------//
//-----Open long position-------------------//
//------------------------------------------//
dayscheck=5
if not longonmarket and businessdaysleft=(dayscheck+1) then
buy 1 contract at market
endif
//------------------------------------------//
//-----Close long position------------------//
//------------------------------------------//
newmonth=openmonth<>openmonth[1]
if onmarket then
n=n+1
if n>=3+dayscheck then
sell at market
endif
else
n=0
endif
Hi,
finally I came to test this day counting algo, and realized that it works perfectly for months which do not contain any exchange holidays, and so it does not count correctly for the months with exchange holidays. for this moment I assume there is no real posibility to consider in the algo correctly those months with exchange holidays: for that we would need to have “in the background” all exchange holidays of the past (for the backtesting) and for the future (for live implementation), for specific exchange, right? I assume still one could do it manually: collect past exchange holidays manually (or let collect them by AI if one trusts AI :-D) and put them in the code…
so I will probably collect exchange holidays for my exchange of interest, but anyway – I still don’t have an idea, how to consider them in the code, so counting runs correctly. do you have and idea, Ivan? or anybody else? I mean, if let’s say 1st of May is exchange holiday and this day in the year xyzw is not on weekend, how could I possibly tell it in the code, so counting is adjusted?
thanks,
justisan