Back

Trading a specific day of the month

Hi. Using Python. I've been through the DateTime rules section of the website docs as well as the Github algos, but haven't found a way to trade on a day of the month, eg on the 10th of every month, the way I could in Q. Does anyone know how to do this?

In Q it looked like this:

    d = get_datetime().day
    m = get_datetime().month
    w = get_datetime().weekday()
    y = get_datetime().year

Then use this code to select a specific day:

       if (d==24 or d==25 or d==26 or d==27 or d==28 or d==29 or d==30 or d==31):

          #trade instructions

Thanks in advance!

Update Backtest








Following up on this Python coding question. Has anyone found a way to code specific days of the month, eg, the 10th, 15th, 20th? In Quantopian, I used... 

d = get_datetime().day
m = get_datetime().month
w = get_datetime().weekday()
y = get_datetime().year

combined with if/then logic to trade certain calendar days (d), months (m), etc...

if (d==24 or d==25 or d==26):

     # do something

if (m==1 or m==2 or m==3):

     # do something

Any help would be greatly appreciated. I see how to hardcode days/months/years, but am trying to create a generic version that is dynamic year to year.

0

Recommend you post the work you've tried so far Brett; people are more receptive to help when they see you've put in a solid effort.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Sure. Let's say I want to trade every month on the 10th. As noted, I could hard code a scheduled function on the 10th (see backtest). For a full year, I would need 12 scheduled functions. But let's say I want to trade every month on the 10th, 15th and 20th. Now I would need to hard code 36 scheduled events for the year. And I'd still have to update the code annually.

In Quantopian, there was an easy way to identify a day, weekday, month, year (see above posts) for calendar-based strategies. Is there anything comparable in Quantconnect?

Thank you.

0


You can put any simple filters you want into your rebalance logic.

self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(9, 31),
Action(self.Rebalance))

def Rebalance(self):
if self.Time.Day != 15: return
if self.Time.Day != 25: return

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Jared, thanks for the reply! I haven't seen self.Time.Day (15 = hour of day?) and self.Time.DayofMonth (25 = day of month?) in the Python docs -- website and Github have been my two main sources. Where would I find this, and others (eg, is there a self.Time.DayofWeek)? 

Separate, but related, how do I use event scheduler to repeat a function over and over, but start cycling at, say 10am, instead of the market open, which I understand to be the default?

Thanks again.

0

Sorry they were both Day -- Time is a DateTime object under the hood -- you can read all of the DateTime properties;

https://msdn.microsoft.com/en-us/library/system.datetime.day(v=vs.110).aspx

 

In future this should be in the API tab but ironically specifically the Time property isn't being shown. The UtcTime property is there for an example.

https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Got it. Thx, Jared!

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed