Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-69.995%
Drawdown
9.700%
Expectancy
0
Net Profit
-9.719%
Sharpe Ratio
-5.319
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-1.133
Beta
1.94
Annual Standard Deviation
0.206
Annual Variance
0.043
Information Ratio
-5.409
Tracking Error
0.206
Treynor Ratio
-0.565
Total Fees
$1.72
import numpy as np
from datetime import datetime


class MovingAverage(QCAlgorithm):

    def __init__(self):
        self.previous = None
        self.ma = None
        self.position = None
        self.lastMonth = -1
    
    def Initialize(self):
    
        self.SetStartDate(2018,10,1) #Set Start Date
        self.SetEndDate(2018,10,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.SetWarmUp(440)
    
        self.ma = self.SMA("SPY", 220, Resolution.Daily)
    
    def OnData(self, data):
    
        if self.IsWarmingUp:
            return
        
        
        if not data.ContainsKey("SPY"):
            return
        
        if self.lastMonth == self.Time.month:
            return
        
        if data["SPY"].Close > self.ma.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