Back

Set Holdings on a specific date

Hello,

I would like to know how can I set holdings (self.SetHoldings) for a security (Say, SPY) in one specific date only once, and not set holdings unless it is that specific date.

Thank you.

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.


Sorry, I also need to clarify that I want the algorithm to set holdings once at that date but also if that date already has passed (Like when I'm not using historical data, but live data) then set holdings only once right when the algorithm is deployed and not attempt to set holdings again via that code.

Any help would be highly appreciated!

0

Hi Marvin,

We can call SetHoldings when a specific date has been reached or passed by first specifying the purchase date in Initialize

self.purchase_date = datetime(2020, 3, 5)

Then, we check if this date is less than or equal to the current slice's date before calling SetHoldings.

def OnData(self, data):
if not self.Portfolio.Invested and data.Time >= self.purchase_date and data.ContainsKey("SPY"):
self.SetHoldings("SPY", 1)

See the attached backtest for reference.

Best,
Derek Melchin

1

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.


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