Back

Initialize/AddSecurity Five Minutes After Open

Hey
How do you add a security after the algorithm has been initialized? I've tried to schedule an event within the initialize method but it wont load the data.
What does post initialize do and how does one go about using it?

Appreciate it,
-Travis

Update Backtest








Travis,

That's interesting that it isn't feeding you data. Theoretically, it should work when you AddSecurity, because I think Coarse Universe basically does what you're asking it to do.

That said, you got me thinking about how you would go about charging history (warmup) to a newly added security. Obviously if you add a new security halfway through your backtest, it may require you to charge indicators anew for that particular security, which can be a pain if you use long-ranging indicators (like SMA of 200 days or something). I implemented something similar to the following when dealing with this in the Coarse Universe selection. It allowed me to get a hundred days of data for a newly added security without ever having to wait for it.

The example method below takes a string "symbol" (the symbol you want to add), the number of history periods you need to precharge any indicators, and a resolution. It adds the security, then pre-charges a rolling window called ClosingPrices, and an SMA indicator called SMAClosingPrices.

public void PostInitAddSecurity(string symbol, int historyperiods, Resolution resolution)
{
AddSecurity(SecurityType.Equity, symbol, resolution);

IEnumerable history = History(symbol, historyperiods, resolution);

foreach (TradeBar tradeBar in history)
{
ClosingPrices.Add(tradeBar.Close);
SMAClosingPrices.Update(tradeBar.EndTime, tradeBar.Close)
}
}


This doesn't help your current situation, but if and when you get to the point where you're seeing data feed through your algo, this might help. :-)

0

The code I posted above was formatted badly, so the "Tradebar" may give you some issues if you copy paste it into an algo.

A second thought occurred to me, Travis. You may be required to create a custom universe in order to Add/Remove Securities at will. The base algorithm may not be set up properly to handle adding and removing securities if you aren't using a custom defined universe.

Here's a thread where Michael H shows an example algo that Adds/Removes securities based on remote server selection. You could possibly leverage this to properly pass securities through the universe.

https://www.quantconnect.com/forum/discussion/919/feature-universe-using-remote-symbol-selection#Item_11

0

Thanks Stephen

I'm still having trouble even after checking out those links. I did find some good info here https://www.quantconnect.com/forum/discussion/945/universe-selection#Item_2 but the problem is that it uses AddSecurity to add SPY. Now I'm really getting confused about how to accomplish this.
I might investigate more into the warmup periods but it seems like I've checked out every thread on this website at this point. :(

0

You can call AddSecurity and RemoveSecurity at any time during your algorithm. If you're backtesting and you have selected zero securities then it will stop running because of no data. I modified your code by adding SPY, this ensures we won't end the algo early since there's no data subscriptions.

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.


My apologies for putting up wrong information (regarding being able to pass securities in and out of the algo without using a universe).

As a curiosity, is this the reason why SPY is typically added to the coarse universe selection at all times? To keep the algo from halting from potential no securities being selected?

0

Thanks guys I figured it out, check it! I had to add SPY then use this.AddSecurity and x.Symbol != "SPY". :) I was trying AddSecurity from initialize and the TradeBars methods but the "this" keyword makes it work

0

SPY is set as the default benchmark, so we'll automatically add a subscription to SPY for you if it isn't already specified. You could set your benchmark to another symbol, or you can even make the benchmark a flat line using:SetBenchmark(dateTime => 1m);

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.


Gotcha, thanks Michael

0



https://www.quantconnect.com/docs/Algorithms#5.1
0

Your algorithm run settings are sent from within the Initialize() Method. It is required to setup the your algorithm with the following methods: AddSecurity(), SetCash(), SetStartDate(), and SetEndDate(). You must call those functions from within Initialize(). After the algorithm has been initialized they are disabled.
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