Overall Statistics Total Trades239Average Win1.10%Average Loss-0.65%Compounding Annual Return10.589%Drawdown17.400%Expectancy0.398Net Profit32.296%Sharpe Ratio0.669Loss Rate48%Win Rate52%Profit-Loss Ratio1.70Alpha0.181Beta-4.839Annual Standard Deviation0.146Annual Variance0.021Information Ratio0.551Tracking Error0.146Treynor Ratio-0.02Total Fees\$1812.63
```from clr import AddReference

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from System.Collections.Generic import List
import decimal as d
import numpy as np
import time
from datetime import timedelta
### <summary>
##Remvoing an if statement increased the returns by 4 %
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''

def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.SetStartDate(2010,10,07)  #Set Start Date
self.SetEndDate(2012,10,01)    #Set End Date
self.SetCash(100000)           #Set Strategy Cash
## N=Normal (Manufacturing), M=Mining, U=Utility, T=Transportation, B=Bank, I=Insurance
self.UniverseSettings.Resolution = Resolution.Daily
self.spy_sma = self.SMA('SPY',200,Resolution.Daily)
self.first = 0
self.month = 0
self.Top_sector = ''
self.The_sectors = {"XME|M":0,"KIE|I":0,"XLU|U":0,"XLF|B":0,"IYT|T":0,}
for symbol in self.The_sectors:
self.The_sectors[symbol]= self.MOM(symbol.split('|')[0],30,Resolution.Daily)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(3,45),Action(self.rebalance))
self.SetWarmUp(200)

def CoarseSelectionFunction(self, coarse):
filtered_stocks = filter(lambda x: x.HasFundamentalData,coarse)
filtered_stocks = list(filter(lambda x: x.DollarVolume >250000,filtered_stocks))
filtered_stocks.sort(key = lambda x: x.DollarVolume)
return [stock.Symbol for stock in filtered_stocks[:50]]
def FineSelectionFunction(self, fine):
return
if not self.first:
self.Top_sector = max(self.The_sectors,key = self.The_sectors.get).split('|')[-1]
self.first +=1
filtered_stocks = list(filter(lambda x: self.Top_sector in str(x.CompanyReference.IndustryTemplateCode),fine))
return [stock.Symbol for stock in filtered_stocks[:5]]
def OnSecuritiesChanged(self, changes):
if self.Securities[self.spy].Price >self.spy_sma.Current.Value:
for stock in self.Portfolio.Values:
bond =[stock.Symbol for stock in self.Portfolio.Values if stock.Invested and str(stock.Symbol).split(' ')[0] == 'TLT']
if bond:
self.Liquidate('TLT')
self.Log("Bonds were here")
else:
oo = len(self.Transactions.GetOpenOrders(stock.Symbol))
cash = float(self.Portfolio.Cash)
if not oo and cash >0 and str(stock.Symbol).split(' ')[0] != 'SPY':
self.SetHoldings(stock.Symbol,0.10)

def rebalance(self):
self.month+=1
if self.month % 25 == 0:
curr_best_sector = max(self.The_sectors,key = self.The_sectors.get).split('|')[-1]
if self.Top_sector!= curr_best_sector:
self.Top_sector= curr_best_sector
bond =[stock.Symbol for stock in self.Portfolio.Values if stock.Invested and str(stock.Symbol).split(' ')[0] == 'TLT']
if not bond:
self.Liquidate()
time.sleep(20)