Overall Statistics
Total Trades
25
Average Win
13.41%
Average Loss
-4.47%
Compounding Annual Return
11.030%
Drawdown
17.500%
Expectancy
2.331
Net Profit
289.590%
Sharpe Ratio
0.83
Loss Rate
17%
Win Rate
83%
Profit-Loss Ratio
3.00
Alpha
0.194
Beta
-6.232
Annual Standard Deviation
0.111
Annual Variance
0.012
Information Ratio
0.682
Tracking Error
0.111
Treynor Ratio
-0.015
Total 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.AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily)
        self.AddSecurity(SecurityType.Equity, "IEF", Resolution.Daily)
    
        self._sma = self.SMA("SPY", 220, Resolution.Daily)

    def OnData(self, data):
        
        # wait for the sma to fully initialize
        if not self._sma.IsReady:
            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