Hi, newbie in here. I am trying to learn proper way, so working on algorthm framework. 

My question is how (and where) do we add the securities when dynamicly select them in the coarse and fine selection. I suppose it is in "OnSecuritiesChanged" in the "AlphaModel". so I am trying to do something like this 

 

def OnSecuritiesChanged(self, algorithm, changes): #algorithm.Debug("OnSecuritiesChanged MOMA") if algorithm.IsWarmingUp or not changes.AddedSecurities: return algorithm.Debug(len(algorithm.Securities.Keys)) for security in algorithm.Securities.Keys: algorithm.Debug("Total security list: {}".format(security.Value)) for security in algorithm.ActiveSecurities.Keys: algorithm.Debug("Active security list: {}".format(security.Value)) for security in changes.RemovedSecurities: algorithm.AddEquity(security.Symbol, Resolution.Daily) if security.Invested and algorithm.Securities.ContainsKey(security.Symbol): algorithm.Debug("Sell security {} on {}".format(symbol, security.Price) ) algorithm.Liquidate(security.Symbol) for security in changes.AddedSecurities: #and algorithm.ContainsKey(security.Symbol).ToString(): algorithm.AddEquity(security.Symbol, Resolution.Daily) if not security.Invested and algorithm.Securities.ContainsKey(security.Symbol): #algorithm.Debug(algorithm.Securities) symbol = security.Symbol algorithm.Debug("Buy security {} on {}".format(symbol, security.Price) ) self.momen.append({"symbol":security.Symbol, "indicator":algorithm.MOM(symbol, self.period, Resolution.Daily)})

I put bunch of debug, looks like algorthm is doing some trade in the first day but in the second day it fails with 

 

Runtime Error: This asset symbol (MA 0) was not found in your security list. Please add this security or check it exists before using it with 'Securities.ContainsKey("MA 0")' (Open Stacktrace)

I think the asset should be "MA" not sure why I have additinal space and 0 at the end.  

2019-01-01 00:00:00 : Launching analysis for 53653f0387b1df4f72eae3c16862340d with LEAN Engine v2.4.0.0.8998 2019-01-01 00:00:00 : init inside try 2019-01-01 00:00:00 : course selection 2019-01-01 00:00:00 : Fine selection 2019-01-01 00:00:00 : Fine selection result: GE 2019-01-01 00:00:00 : Fine selection result: ABX 2019-01-01 00:00:00 : Fine selection result: MA 2019-01-01 00:00:00 : Fine selection result: NKE 2019-01-01 00:00:00 : Fine selection result: KO 2019-01-01 00:00:00 : Fine selection result: QCOM 2019-01-01 00:00:00 : Fine selection result: ABT 2019-01-01 00:00:00 : Fine selection result: V 2019-01-01 00:00:00 : Fine selection result: MCD 2019-01-01 00:00:00 : Fine selection result: MRK 2019-01-01 00:00:00 : 10 2019-01-01 00:00:00 : Total security list: MCD 2019-01-01 00:00:00 : Total security list: QCOM 2019-01-01 00:00:00 : Total security list: ABX 2019-01-01 00:00:00 : Total security list: V 2019-01-01 00:00:00 : Total security list: NKE 2019-01-01 00:00:00 : Total security list: KO 2019-01-01 00:00:00 : Total security list: MA 2019-01-01 00:00:00 : Total security list: ABT 2019-01-01 00:00:00 : Total security list: MRK 2019-01-01 00:00:00 : Total security list: GE 2019-01-01 00:00:00 : Active security list: MCD 2019-01-01 00:00:00 : Active security list: QCOM 2019-01-01 00:00:00 : Active security list: ABX 2019-01-01 00:00:00 : Active security list: V 2019-01-01 00:00:00 : Active security list: NKE 2019-01-01 00:00:00 : Active security list: KO 2019-01-01 00:00:00 : Active security list: MA 2019-01-01 00:00:00 : Active security list: ABT 2019-01-01 00:00:00 : Active security list: MRK 2019-01-01 00:00:00 : Active security list: GE 2019-01-01 00:00:00 : Buy security ABT on 70.260040424 2019-01-01 00:00:00 : Buy security ABX on 0.0 2019-01-01 00:00:00 : Buy security GE on 7.520964568 2019-01-01 00:00:00 : Buy security KO on 45.078274692 2019-01-01 00:00:00 : Buy security MA on 186.699524524 2019-01-01 00:00:00 : Buy security MCD on 171.166044492 2019-01-01 00:00:00 : Buy security MRK on 73.250521236 2019-01-01 00:00:00 : Buy security NKE on 72.968034489 2019-01-01 00:00:00 : Buy security QCOM on 54.02052998 2019-01-01 00:00:00 : Buy security V on 130.69033029 2019-01-01 00:00:00 : ABT, ABX on GE 2019-01-01 00:00:00 : Warning: all market orders sent using daily data, or market orders sent after hours are automatically converted into MarketOnOpen orders. 2019-01-01 00:00:00 : ABX: The security does not have an accurate price as it has not yet received a bar of data. Before placing a trade (or using SetHoldings) warm up your algorithm with SetWarmup, or use slice.Contains(symbol) to confirm the Slice object has price before using the data. Data does not necessarily all arrive at the same time so your algorithm should confirm the data is ready before using it. In live trading this can mean you do not have an active subscription to the asset class you're trying to trade. If using custom data make sure you've set the 'Value' property. 2019-01-02 10:00:00 : 10 2019-01-02 10:00:00 : Total security list: MCD

 

                                          

 

Author