| Overall Statistics |
|
Total Trades 91 Average Win 1.30% Average Loss -0.81% Compounding Annual Return -41.607% Drawdown 15.400% Expectancy -0.422 Net Profit -14.711% Sharpe Ratio -3.097 Loss Rate 78% Win Rate 22% Profit-Loss Ratio 1.60 Alpha -0.504 Beta -0.135 Annual Standard Deviation 0.164 Annual Variance 0.027 Information Ratio -3.216 Tracking Error 0.164 Treynor Ratio 3.763 Total Fees $91.00 |
import numpy as np
import pandas as pd
class BasicTemplateAlgorithm(QCAlgorithm):
def __init__(self):
self.symbols = ['MDY','IEV','EEM','ILF','EPP','EDV','SHY']
self.back_period = 73
def Initialize(self):
self.SetCash(1000)
self.SetStartDate(2018,2,1)
self.SetEndDate(2018,4,20)
self.SetWarmUp(TimeSpan.FromDays(30))
for i in range(len(self.symbols)):
symbol = self.AddEquity(self.symbols[i], Resolution.Daily).Symbol
self.symbols[i] = symbol
# calculate historical return and volatility for each stock
def get_history(self):
history = self.History(self.back_period, Resolution.Daily)
for i in self.symbols:
bars = map(lambda x: x[i], history)
i.prices = pd.Series([float(x.Close) for x in bars])
vol = np.mean(i.prices.rolling(20).std()*np.sqrt(self.back_period/20.0))
i.volatility = vol/i.prices[0]
i.ret = (i.prices.iloc[-1] - i.prices.iloc[0])/i.prices.iloc[0]
# normalise the mesures of returns and volatilities
def normalise(self):
rets = [x.ret for x in self.symbols]
vols = [x.volatility for x in self.symbols]
self.ret_max, self.ret_min = max(rets), min(rets)
# vol_min is actually the max volatility. min means low score on this.
self.vol_min, self.vol_max = max(vols), min(vols)
# select the best one with the highest score.
def select(self):
self.get_history()
self.normalise()
for i in self.symbols:
self.Debug(str(type(i.ret)))
ret = (i.ret - self.ret_min)/(self.ret_max - self.ret_min)
vol = (i.volatility - self.vol_min)/(self.vol_max - self.vol_min)
i.score = ret*0.7 + vol*0.3
select = sorted(self.symbols, key = lambda x: x.score, reverse = True)
return select[0]
def OnData(self, slice):
target = self.select()
# if self.Portfolio[target.Value].Quantity != 0:
# return
self.Liquidate()
# self.MarketOrder(target.Value, 1)
self.SetHoldings(target,1)