| Overall Statistics |
|
Total Trades 155 Average Win 0.29% Average Loss -0.23% Compounding Annual Return -79.850% Drawdown 12.600% Expectancy -0.763 Net Profit -12.593% Sharpe Ratio -7.843 Probabilistic Sharpe Ratio 0% Loss Rate 90% Win Rate 10% Profit-Loss Ratio 1.28 Alpha -0.681 Beta 0.015 Annual Standard Deviation 0.086 Annual Variance 0.007 Information Ratio -9.05 Tracking Error 0.109 Treynor Ratio -45.455 Total Fees $155.00 Estimated Strategy Capacity $10000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X Portfolio Turnover 413.97% |
# region imports
from AlgorithmImports import *
# endregion
import numpy as np
import pandas as pd
class EmaCrossoverAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 7, 1)
# self.SetEndDate(2023, 7, 29)
self.SetCash(1000)
self.AddEquity("SPY")
self.resolution = Resolution.Minute
# Create custom indicators for 20-period and 30-period EMAs
self.ema_20 = self.EMA("SPY", 20, Resolution.Minute)
self.ema_30 = self.EMA("SPY", 30, Resolution.Minute)
# Variables to track previous crossover state
self.prev_ema_crossover = 0
# Schedule the event to check for crossovers
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(3)), self.CheckForCrossovers)
def OnData(self, slice):
# Do whatever you need with the incoming data
pass
def CheckForCrossovers(self):
if not self.ema_20.IsReady or not self.ema_30.IsReady:
return
ema_20_value = self.ema_20.Current.Value
ema_30_value = self.ema_30.Current.Value
# Check for the EMA crossover
ema_crossover = 0
if ema_20_value > ema_30_value:
ema_crossover = 1
elif ema_20_value < ema_30_value:
ema_crossover = -1
# Check for changes in the crossover state
if ema_crossover != self.prev_ema_crossover:
self.prev_ema_crossover = ema_crossover
# Issue new signals only when the crossover direction changes
if ema_20_value > ema_30_value:
self.SetHoldings("SPY", 1)
self.Debug(f"Buy: EMA(20) = {ema_20_value:.2f}, EMA(30) = {ema_30_value:.2f}")
elif ema_20_value < ema_30_value:
self.Liquidate()
self.Debug(f"Sell: EMA(20) = {ema_20_value:.2f}, EMA(30) = {ema_30_value:.2f}")