Back

Trading only at end of day

Trying to have algorithm adjust / rebalance holdings at close of each day. However, the code I am using to try to force this is not working as expected:

if (DateTime.Now.TimeOfDay <= System.TimeSpan.Parse("15:45:00")) return;


Is there a better way to do this? Perhaps I could use MarketOnClose orders, however, because of the daily rebalancing, using SetHoldings is a very convenient way to define what I am trying to do.
Update Backtest








Hey @Reuben,

There's a few issues here. First, it appears you're using DateTime.Now to set your sampledToday variable. DateTime.Now will give you the system's current time in that time zone. You should instead be using the Time property of QCAlgorithm:sampledToday = Time;
Also, it appears you're comparing your timespan again with DateTime.Now instead of the algorithm's Time property.

The last thing I modified was setting your custom data to use fill forward behavior. This will copy the daily data onto the minute bars. I've also defined the exchange property so it doesn't perform fill forward over weekends and during not equity trading hours.

I've modified and attached the algorithm. Let me know if you have further questions on algorithm Time.
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.


@MichaelH, this was a big help. Thank you.
0

I'll aim to break SetHoldings into two functions, one with the meat and potatoes (read: math) and the other will just submit the order. Maybe something like:
public int GetSetHoldingsOrderQuantity(symbol, decimal targetPercentage) This way you could call the above function and use the result to submit any order type.
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.


That would really help to simplify re-balancing portfolios on a regular basis.
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