Overall Statistics Total Trades25Average Win13.41%Average Loss-4.47%Compounding Annual Return11.030%Drawdown17.500%Expectancy2.331Net Profit289.590%Sharpe Ratio0.83Loss Rate17%Win Rate83%Profit-Loss Ratio3.00Alpha0.194Beta-6.232Annual Standard Deviation0.111Annual Variance0.012Information Ratio0.682Tracking Error0.111Treynor Ratio-0.015Total Fees\$229.00
import numpy as np
from datetime import datetime

### <summary>
### Upbias Tactical Switch Strategy - a simple strategy that can beat the stock market.
### detailed explanations at https://www.upbias.com/blog/beat-the-stock-market-strategy
### </summary>
class UpbiasTacticalSwitch(QCAlgorithm):

def __init__(self):
self.previous = None
self._sma = None
self.position = None
self.lastMonth = -1

def Initialize(self):
self.SetStartDate(2005,01,03)  #Set Start Date
self.SetEndDate(2017,12,29)    #Set End Date
self.SetCash(100000)           #Set Strategy Cash

self._sma = self.SMA("SPY", 220, Resolution.Daily)

def OnData(self, data):

# wait for the sma to fully initialize
return

if not data.ContainsKey("SPY"):
return

if self.lastMonth == self.Time.month:
return

if data["SPY"].Close > self._sma.Current.Value:
if self.position == None:
self.SetHoldings("SPY", 1)
else:
if self.position == "IEF":
self.Liquidate("IEF")
self.SetHoldings("SPY", 1)
self.position = "SPY"
else:
if self.position == None:
self.SetHoldings("IEF", 1)
else:
if self.position == "SPY":
self.Liquidate("SPY")
self.SetHoldings("IEF", 1)
self.position = "IEF"

self.lastMonth = self.Time.month