| Overall Statistics |
|
Total Trades 2 Average Win 0% Average Loss 0.00% Compounding Annual Return 18.062% Drawdown 33.600% Expectancy -1 Net Profit 129.444% Sharpe Ratio 0.867 Probabilistic Sharpe Ratio 31.285% Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.001 Beta 0.996 Annual Standard Deviation 0.156 Annual Variance 0.024 Information Ratio -0.66 Tracking Error 0.003 Treynor Ratio 0.136 Total Fees $3.46 Estimated Strategy Capacity $69000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X |
# region imports
from AlgorithmImports import *
# Import necessary librarie
# Import necessary libraries
# from quantconnect.algorithm import QCAlgorithm
# from quantconnect.indicators import SimpleMovingAverage
# import matplotlib.pyplot as plt
class SimpleMovingAverageStrategy(QCAlgorithm):
def Initialize(self):
# Set the cash we'd like to use for our backtest
self.SetStartDate(2017,1,1)
self.SetEndDate(2022,1,1)
self.SetCash(100000)
# Set the symbol we'd like to use for our backtest
self.symbol = self.AddEquity("SPY").Symbol
# Set the lookback period for our moving average
self.lookback_period = 20
# Create a Simple Moving Average indicator
self.sma = SimpleMovingAverage(self.lookback_period)
# Schedule an event to fire every trading day at market close
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(17, 0), self.Trade)
# Set up a plot for the strategy and benchmark
# self.strategy_plot = plt.subplot(2, 1, 1)
# self.benchmark_plot = plt.subplot(2, 1, 2)
# self.SetPlotOrder(self.benchmark, self.strategy)
def Trade(self):
# Get the current price of the asset
current_price = self.Securities[self.symbol].Price
# Get the value of the moving average indicator
sma_value = self.sma.Current.Value
# If the current price is greater than the moving average, buy the asset
if current_price > sma_value:
self.SetHoldings(self.symbol, 1)
# If the current price is less than the moving average, sell the asset
elif current_price < sma_value:
self.SetHoldings(self.symbol, -1)
# If the current price is equal to the moving average, do nothing
else:
pass