public async Task <SyncState> SyncStepAsync(Action syncProgressChanged) { switch (SyncState) { case SyncState.Start: SyncState = SyncState.FindingUser; break; case SyncState.FindingUser: var user = await FindUserAsync(); UserKeyStore = new RdioUserKeyStore(user); ObjectStore.Add(user); SyncState = SyncState.FoundUser; break; case SyncState.FoundUser: SyncState = SyncState.SyncingUserKeys; break; case SyncState.SyncingUserKeys: foreach (var kind in new [] { "owned", "favorites", "subscribed", "collab" }) { await api.LoadUserPlaylists(kind, UserKeyStore); } await api.LoadFavoritesAndSyncedKeysAsync(UserKeyStore); SyncState = SyncState.SyncedUserKeys; break; case SyncState.SyncedUserKeys: SyncState = SyncState.SyncingObjects; break; case SyncState.SyncingObjects: keysProcessor = new ItemsProcessor <string> ( UserKeyStore.AllKeys.Where(k => !ObjectStore.ContainsKey(k)), KeyProcessorAsync, chunkCount => { TotalObjects = keysProcessor.TotalEnqueued; SyncedObjects = keysProcessor.TotalProcessed; syncProgressChanged?.Invoke(); } ); TotalObjects = keysProcessor.TotalEnqueued; SyncedObjects = 0; await keysProcessor.ProcessAsync(CancellationToken); SyncState = SyncState.SyncedObjects; break; case SyncState.SyncedObjects: SyncState = SyncState.Finished; break; } return(SyncState); }
public override void Initialize() { SetStartDate(2013, 10, 07); SetEndDate(2013, 10, 11); SPY = AddEquity("SPY", Resolution.Minute).Symbol; // define indicators on SPY daily closing prices SPY_Close = Identity(SPY, Resolution.Daily); SPY_Close_EMA10 = SPY_Close.EMA(10); SPY_Close_EMA50 = SPY_Close.EMA(50); // each time an indicator is updated, push the value into our history rolling windows SPY_Close.Updated += (sender, args) => { // each time we receive new closing price data, push our window to the object store SPY_Close_History.Add(args); }; SPY_Close_EMA10.Updated += (sender, args) => SPY_Close_EMA10_History.Add(args); SPY_Close_EMA50.Updated += (sender, args) => SPY_Close_EMA50_History.Add(args); if (ObjectStore.ContainsKey(SPY_Close_ObjectStore_Key)) { // our object store has our historical data saved, read the data // and push it through the indicators to warm everything up var values = ObjectStore.ReadJson <IndicatorDataPoint[]>(SPY_Close_ObjectStore_Key); Debug($"{SPY_Close_ObjectStore_Key} key exists in object store. Count: {values.Length}"); foreach (var value in values) { SPY_Close.Update(value); } } else { Debug($"{SPY_Close_ObjectStore_Key} key does not exist in object store. Fetching history..."); // if our object store doesn't have our data, fetch the history to initialize // we're pulling the last year's worth of SPY daily trade bars to fee into our indicators var history = History(SPY, TimeSpan.FromDays(365), Resolution.Daily); foreach (var tradeBar in history) { SPY_Close.Update(tradeBar.EndTime, tradeBar.Close); } // save our warm up data so next time we don't need to issue the history request var array = SPY_Close_History.Reverse().ToArray(); ObjectStore.SaveJson(SPY_Close_ObjectStore_Key, array); // Can also use ObjectStore.SaveBytes(key, byte[]) // and to read ObjectStore.ReadBytes(key) => byte[] // we can also get a file path for our data. some ML libraries require model // weights to be loaded directly from a file path. The object store can provide // a file path for any key by: ObjectStore.GetFilePath(key) => string (file path) } }
async Task KeyProcessorAsync(IEnumerable <string> keys) { await api.LoadObjectsAsync( ObjectStore, keys.Where(k => !ObjectStore.ContainsKey(k)), obj => obj.AcceptVisitor(this) ); }
void IRdioObjectKeyVisitor.VisitObjectKey(string objectKey) { if (keysProcessor != null && keysProcessor.IsProcessing && !ObjectStore.ContainsKey(objectKey)) { keysProcessor.Add(objectKey); } }
// def CoarseFilter(self, coarse): public IEnumerable <Symbol> CoarseFilter(IEnumerable <CoarseFundamental> coarse) { // # load data from ObjectStore // if len(self.tech_ROA) == 0 and self.ObjectStore.ContainsKey(self.tech_ROA_key): if (this.tech_ROA.Count == 0 && ObjectStore.ContainsKey(tech_ROA_key)) { // tech_ROA = self.ObjectStore.ReadBytes(self.tech_ROA_key) var tech_ROAstr = ObjectStore.ReadJson <Dictionary <string, Queue <decimal> > >(tech_ROA_key); // tech_ROA = pickle.loads(bytearray(tech_ROA)) // self.tech_ROA = { Symbol.Create(ticker, SecurityType.Equity, Market.USA):ROA for ticker, ROA in tech_ROA.items()} //Don't have pickle in C# so instead, deserialize a Dictionary with string tickers and convert them into Symbol keys this.tech_ROA.Clear(); foreach (string key in tech_ROAstr.Keys) { this.tech_ROA.Add(QuantConnect.Symbol.Create(key, SecurityType.Equity, Market.USA), tech_ROAstr[key]); } //return list(self.tech_ROA.keys()) return(this.tech_ROA.Keys.ToArray()); } //if self.curr_month == self.Time.month: if (this.curr_month == this.Time.Month) { //return Universe.Unchanged return(Universe.Unchanged); } //self.curr_month = self.Time.month __this.curr_month = this.Time.Month; // we only want to update our ROA values every three months //if self.Time.month % 3 != 1: if (this.Time.Month % 3 != 1) { //return Universe.Unchanged return(Universe.Unchanged); } //self.quarters += 1 __this.quarters += 1; //return [c.Symbol for c in coarse if c.HasFundamentalData] return(coarse.Where(c => c.HasFundamentalData).Select(c => c.Symbol)); }
// def Initialize(self): public override void Initialize() { __this = this; // ### IMPORTANT: FOR USERS RUNNING THIS ALGORITHM IN LIVE TRADING, // ### RUN THE BACKTEST ONCE //self.tech_ROA_key = 'TECH_ROA' __this.tech_ROA_key = "TECH_ROA_CS"; // we need 3 extra years to warmup our ROA values //self.SetStartDate(2012, 9, 1) __this.SetStartDate(2012, 9, 1); //self.SetEndDate(2020, 9, 1) __this.SetEndDate(2019, 9, 1); //self.SetCash(100000) # Set Strategy Cash __this.SetCash(100000); //self.SetBrokerageModel( AlphaStreamsBrokerageModel()) __this.SetBrokerageModel(new AlphaStreamsBrokerageModel()); //self.SetAlpha( ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(days = 31))) __this.SetAlpha(new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromDays(31))); //self.SetExecution( ImmediateExecutionModel()) //__this.SetExecution(new ImmediateExecutionModel()); //Be more picky about execution: __this.SetExecution(new StandardDeviationExecutionModel(30, 0.25m, Resolution.Daily)); //self.SetPortfolioConstruction( EqualWeightingPortfolioConstructionModel(lambda time: None)) __this.SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel(time => null)); //Not in the original example: __this.SetRiskManagement(new TrailingStopRiskManagementModel(0.45m)); //self.AddUniverseSelection( __this.AddUniverseSelection( // FineFundamentalUniverseSelectionModel(self.CoarseFilter, self.FineFilter) new FineFundamentalUniverseSelectionModel(this.CoarseFilter, this.FineFilter) //) ); //self.UniverseSettings.Resolution = Resolution.Daily __this.UniverseSettings.Resolution = Resolution.Daily; //self.curr_month = -1 __this.curr_month = -1; //store ROA of tech stocks //self.tech_ROA = { } __this.tech_ROA = new Dictionary <Symbol, Queue <decimal> >(); //self.symbols = None object symbols = null; // if self.LiveMode and not self.ObjectStore.ContainsKey(self.tech_ROA_key): if (LiveMode && !ObjectStore.ContainsKey(tech_ROA_key)) { //self.Quit('QUITTING: USING LIVE MOVE WITHOUT TECH_ROA VALUES IN OBJECT STORE') __this.Quit("QUITTING: USING LIVE MOVE WITHOUT TECH_ROA VALUES IN OBJECT STORE"); } //self.quarters = 0 __this.quarters = 0; //ObjectStore.Delete(tech_ROA_key); #if DEBUG //If error happened in testing get rid of bad data. Comment this out once close to production //ObjectStore.Delete(tech_ROA_key); #endif }