Trading Day of Month
Forums › ProRealTime English forum › ProOrder support › Trading Day of Month
- This topic has 16 replies, 2 voices, and was last updated 2 years ago by robertogozzi.
Tagged: day, month, of, trading, Trading Day of Month
-
-
12/14/2019 at 10:03 PM #114902
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
12/14/2019 at 10:43 PM #114905There you go (non tested):
123456789101112Once Count = 0Once TradeON = 1If OpenMonth <> OpenMonth[1] thenTradeON = 0Count = 0EndifIf OpenDay <> OpenDay[1] ThenCount = Count + 1EndifIf Count > 2 thenTradeON = 1Endifyou need to add TradeON to your conditions to enter a trade.
12/14/2019 at 11:27 PM #114909Roberto – 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!
12/15/2019 at 12:28 AM #114911He wrote “on the 3rd to last trading day”.
12/15/2019 at 9:13 AM #114922Third 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.
12/15/2019 at 10:13 AM #114923This 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.
12/15/2019 at 10:09 PM #11494312/16/2019 at 1:41 PM #114979Maybe something of interest related to this strategy here: (snippets to find last day and last trading day of the month)
12/17/2019 at 11:04 AM #115036This 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:
123456789101112131415161718ONCE DayStart = 0 //day of the month on which trading will startIF OpenMonth <> OpenMonth[1] THENDayStart = 0Count = 0LastDay = CALL DayMax[OpenMonth,OpenYear]FOR i = LastDay downto 1MyDate= (OpenYear * 10000) + (OpenMonth * 100) + iMyDOW = CALL GetDayOfWeek[MyDate]IF MyDow > 0 AND MyDow < 6 THENCount = Count + 1IF Count = 3 THENDayStart = iBreakENDIFENDIFNEXTENDIFRETURN DayStartYou 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:
1234567891011121314DEFPARAM CumulateOrders = falseONCE TradeON = 0 //trading disabled by defaultIF OpenMonth <> OpenMonth[1] THENTradeON = 0 //trading disabled at the beginnin of each monthDayStart = CALL "My Indicator"ENDIFIF OpenDay = DayStart THENTradeON = 1ENDIFIF Not OnMarket AND TradeON = 1 THENBUY 1 CONTRACT AT MARKETSET TARGET pPROFIT 100SET STOP pLOSS 50ENDIF12/17/2019 at 10:11 PM #115125Thanks 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.
1 user thanked author for this post.
01/07/2022 at 10:16 PM #184855Hi 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 dateIF (date = 20210113) and (time=220000) Then
BuyTime = 1
ENDIFIF (date = 20210118) and (time=220000) Then
SellTime = 1
ENDIFGreetings
Salah
01/07/2022 at 10:21 PM #184857What timeframe are you using?
01/08/2022 at 12:14 AM #18485801/08/2022 at 2:16 AM #184860That’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.
01/09/2022 at 10:28 PM #184984 -
AuthorPosts
Find exclusive trading pro-tools on