Overall Statistics |
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
import numpy as np import pandas as pd class MyAlgo(QCAlgorithm): def Initialize(self): self.SetCash(1000000) # Start and end dates for the backtest. self.SetStartDate(2017,1,1) self.SetEndDate(2017,1,30) self.AddEquity("SPY", Resolution.Minute) # My list self.stock_list = ["QQQ", "IVV", "XLF", "IWM"] for i in range(len(self.stock_list)): self.AddEquity(self.stock_list[i],Resolution.Minute) # Schedule the rebalance function (once everyday 30 min. after market open) self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 30), Action(self.rebalance)) def OnData(self, data): pass def rebalance(self): # Close History -------------------------------------------- # Call History (Daily) history = self.History(self.stock_list, 60, Resolution.Daily) # Unstack QC dataframe price_history = history['close'].unstack(level=0) # Call Current History current = self.History(self.stock_list, 1, Resolution.Minute) if current.empty: return # if data is missing (empty), return curr_history = current['close'].unstack(level=0) # Combine all in one price_history = price_history.append(curr_history.iloc[0]) # Re-create a base DF to work on df = pd.DataFrame(price_history.values, index = price_history.index, columns = price_history.columns) df = df.dropna() # Volume History ---------------------------------------------- vol_history = history['volume'].unstack(level=0) curr_vol_history = current['volume'].unstack(level=0) vol_history = vol_history.append(curr_vol_history.iloc[0]) # Re-create a volume DF to work on df2 = pd.DataFrame(vol_history.values, index = vol_history.index, columns = vol_history.columns) df2 = df2.dropna() # ------------------------------------------------------------ # Get sma(20) sma = df[-20:].mean(0).to_frame().T # get the datetime from price history sma.index = df[-1:].reset_index().time.values sma = sma.reset_index() sma.index = ['SMA20'] # Get current close curr_close = df.iloc[-1].to_frame().T.reset_index() curr_close.index = ['Curr_Close'] # Get Volume curr_vol = df2.iloc[-1].to_frame().T.reset_index() curr_vol.index = ['Volume'] # Concat all the DFs table = pd.concat([sma, curr_close, curr_vol]) self.Log('Table :' +'\n'+ str(table) +'\n'+'\n'+ str(table.drop('index', axis=1)))