My universe selection runs 1 minute after market open via
AddUniverseSelection(new ScheduledUniverseSelectionModel(
DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday),
TimeRules.AfterMarketOpen("TSLA",1),
SelectSymbols // selection function in algorithm.
));
and returns 10 equities.
The following OnSecuritiesChanged method is then executed.
public override void OnSecuritiesChanged(SecurityChanges changes)
{
var spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
_changes = changes;
//Debug($"{changes}");
var addedSymbols = changes.AddedSecurities;
var removedSymbols = changes.RemovedSecurities;
foreach (var c in addedSymbols){
if (c.Symbol == spy) break;
AddEquity(c.Symbol, Resolution.Minute);
//if(!c.HasData) {
if(! Securities[c.Symbol].HasData) {
Debug("no data:"+c.Symbol);
nodata++;
} else {
Debug("in:"+c.Symbol);
SetHoldings(c.Symbol, 0.05m);
data++;
}
if(nodata % 25 == 0 || data % 25 == 0){
Debug("d/nd/ni: "+data+"/"+nodata+"/"+notinvested);
}
}
foreach (var c in removedSymbols){
if (c.Invested){
Debug("out: "+c.Symbol);
Liquidate(c.Symbol);
} else {
Debug("was not invested !");
notinvested++;
}
}
}
If I run this from 2021/1/1 I get the following information:
- Data was only available in 57 cases (1 minute after market open)
- The nodata counter is at 86
- The notinvested counter is at 39, which I do not understand. The SetHoldings method is only called when data is available.
What am I doing wrong here ?
Shile Wen
Hi Petra,
Here is the order of data flow:
1 - Universe Selection, calls SelectSymbols method
2 - OnSecuritiesChanged, notifies the selection in SelectSymbols
3 - OnData, where the first data point arrives.
We will be missing data in OnSecuritiesChanged for newly added securities as we haven't received a point of data at that time, thus I suggest moving the SetHoldings Logic to OnData. Alternatively, the following line of codeÂ
SetSecurityInitializer(x => x.SetMarketPrice(GetLastKnownPrice(x)));Â Â
to the Initialize method to get data in OnSecuritiesChanged.Futhermore, Please do not call AddEquity as the Universe Selection already adds the securities to the universe.
Best,
Shile Wen
Petra Wagenbach
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!