| Overall Statistics |
|
Total Trades 1485 Average Win 2.31% Average Loss -1.70% Compounding Annual Return -24.716% Drawdown 88.700% Expectancy -0.143 Net Profit -86.312% Sharpe Ratio -0.653 Probabilistic Sharpe Ratio 0.000% Loss Rate 64% Win Rate 36% Profit-Loss Ratio 1.36 Alpha -0.124 Beta -0.277 Annual Standard Deviation 0.237 Annual Variance 0.056 Information Ratio -0.891 Tracking Error 0.299 Treynor Ratio 0.559 Total Fees $11509.21 Estimated Strategy Capacity $68000000.00 Lowest Capacity Asset AAPL R735QTJ8XC9X |
# region imports
from AlgorithmImports import *
# endregion
# Import necessary libraries
# from quantconnect.algorithm import QCAlgorithm
# from quantconnect.indicators import SimpleMovingAverage
class SimpleSMATradingStrategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 1)
self.SetEndDate(2022,1,1)
# Set the cash we'd like to use for our backtest
self.SetCash(100000)
# Set the symbol we'd like to use for our backtest
self.symbol = self.AddEquity("AAPL").Symbol
self.benchmark_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 = self.SMA(self.symbol,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)
def Trade(self):
# Get the current price of the asset
current_price = self.Securities[self.symbol].Price
# Check if the moving average indicator is ready to be used
if self.sma.IsReady:
# 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
else:
# If the moving average indicator is not ready, do nothing
pass
def OnEndOfDay(self):
self.Plot("Strategy", "SPY", self.Portfolio.TotalPortfolioValue)
self.Plot("Benchmark", "SPY", self.Securities[self.benchmark_symbol].Price)