| Overall Statistics |
|
Total Trades 225 Average Win 7.83% Average Loss -2.59% Compounding Annual Return 19.809% Drawdown 34.400% Expectancy 1.087 Net Profit 1600.041% Sharpe Ratio 0.919 Probabilistic Sharpe Ratio 24.751% Loss Rate 48% Win Rate 52% Profit-Loss Ratio 3.03 Alpha 0 Beta 0 Annual Standard Deviation 0.161 Annual Variance 0.026 Information Ratio 0.919 Tracking Error 0.161 Treynor Ratio 0 Total Fees $187462.82 Estimated Strategy Capacity $590000000.00 Lowest Capacity Asset QQQ RIWIV7K5Z9LX Portfolio Turnover 3.88% |
# region imports
from AlgorithmImports import *
import pandas as pd
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
#self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.SetStartDate(2008, 1, 1)
# self.SetEndDate(2022, 3, 1) # Imposta la data di inizio
self.cap = 1000000
self.SetCash(self.cap) # Imposta il capitale iniziale
self.spy_symbol = self.AddEquity("SPY", Resolution.Daily).Symbol # Aggiungi SPY come asset
self.qqq_symbol = self.AddEquity("QQQ", Resolution.Daily).Symbol # Aggiungi QQQ come asset
self.xlp_symbol = self.AddEquity("XLP", Resolution.Daily).Symbol # Aggiungi XLP come asset
self.qld_symbol = self.AddEquity("QLD", Resolution.Daily).Symbol # Aggiungi QLD come asset
# self.BNC = Benchmark on chart
self.BNC = "SPY"
self.mkt = []
self.buy_new = False
# Aggiungi l'indicatore di media mobile a 200 giorni per SPY
self.spy_sma = self.SMA("SPY", 200, Resolution.Daily)
self.spy_rsi = self.RSI("SPY", 10, MovingAverageType.Wilders)
self.current_position = None # Mantieni traccia della posizione attuale
# self.AddRiskManagement(MaximumDrawdownPercentPerSecurity(0.05))
def OnData(self, slice):
if not self.spy_sma.IsReady:
return
if not self.spy_rsi.IsReady:
return
# Calcola l'RSI di SPY
self.Debug(f"RSI di SPY: {self.spy_rsi.Current.Value}")
if self.Securities[self.spy_symbol].Price > self.spy_sma.Current.Value:
new_position = "QQQ"
elif self.spy_rsi.Current.Value < 30:
new_position = "QLD"
else:
new_position = "XLP"
if self.current_position != new_position:
self.Liquidate() # Liquidate la posizione attuale
# Se la nuova condizione è diversa dalla posizione attuale, esegui la transazione
# Se la nuova condizione è diversa dalla posizione attuale, esegui la transazione
if not self.Portfolio.Invested:
self.SetHoldings(new_position, 0.99)
self.Debug("New position opened")
self.current_position = new_position
self.Debug(f"Cambio di posizione: Nuova posizione = {new_position}")
self.buy_new == False
def OnEndOfDay(self):
# plot the equity we are holding at any given time
#excl = ["SPY"]
excl = []
for sym in self.Portfolio.Keys:
if sym in excl:
continue
# check that we are invested in the symbol
if self.Portfolio[sym].Invested:
self.Plot("Holdings", sym, 1)
else:
self.Plot("Holdings", sym, 0)
# plot the benchmark equity on the equity curve
# plot the benchmark equity on the equity curve
# Put in initialize:
# self.BNC = "SPY"
# self.mkt = []
# self.cap = 1000000
if not self.LiveMode:
mkt_price = self.Securities[self.BNC].Close
# the below fixes the divide by zero error in the MKT plot
if mkt_price > 0 and mkt_price is not None:
self.mkt.append(mkt_price)
if len(self.mkt) >= 2 and not self.IsWarmingUp:
mkt_perf = self.mkt[-1] / self.mkt[0] * self.cap
self.Plot('Strategy Equity', self.BNC, mkt_perf)