Overall Statistics
Total Trades
121
Average Win
3.11%
Average Loss
-4.53%
Compounding Annual Return
2.881%
Drawdown
35.700%
Expectancy
0.153
Net Profit
32.866%
Sharpe Ratio
0.224
Probabilistic Sharpe Ratio
0.150%
Loss Rate
32%
Win Rate
68%
Profit-Loss Ratio
0.69
Alpha
0.022
Beta
0.057
Annual Standard Deviation
0.123
Annual Variance
0.015
Information Ratio
-0.425
Tracking Error
0.168
Treynor Ratio
0.48
Total Fees
$486.60
Estimated Strategy Capacity
$67000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
3.24%
"""
Theory:
There is momentum assosiated with SPY.
When other people are buying, people want to buy.
We buy when there is an upward trend

Implementation:

"""
from AlgorithmImports import *

class BuyTheDip(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2010, 1, 1)
        self.SetEndDate(2020, 1, 1)
        self.SetCash(100000)
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.SetBenchmark(self.symbol)
        fast_period = 20
        slow_period = 50
        self.sma_short = self.SMA(self.symbol,fast_period,Resolution.Daily)
        self.sma_long = self.SMA(self.symbol,slow_period,Resolution.Daily)
        self.SetWarmup(slow_period,Resolution.Daily)
        chart = Chart('Benchmark')
        self.AddChart(chart)
        chart.AddSeries(Series("Buy", SeriesType.Scatter, "$", Color.Green, ScatterMarkerSymbol.Triangle))
        chart.AddSeries(Series("Sell", SeriesType.Scatter, "$", Color.Red, ScatterMarkerSymbol.Triangle))


    def OnData(self, data: Slice):
        if self.IsWarmingUp or (not self.sma_long.IsReady):
            return
        self.Plot('Benchmark',self.sma_long,self.sma_short)

        if (self.sma_short.Current.Value>= self.sma_long.Current.Value *1.005) and (not self.Portfolio[self.symbol].IsShort):
            self.Liquidate(self.symbol)
            self.SetHoldings(self.symbol,-0.95)
            self.Plot('Benchmark','Sell',data[self.symbol].Price)

        elif (self.sma_short.Current.Value < self.sma_long.Current.Value*1.005) and (not self.Portfolio[self.symbol].IsLong):
            self.Liquidate(self.symbol)
            self.SetHoldings(self.symbol,0.99)
            self.Plot('Benchmark','Buy',data[self.symbol].Price)