Overall Statistics
Total Trades
3029
Average Win
0.48%
Average Loss
-0.62%
Compounding Annual Return
66.455%
Drawdown
30.800%
Expectancy
0.395
Net Profit
4891.116%
Sharpe Ratio
2.125
Probabilistic Sharpe Ratio
99.098%
Loss Rate
21%
Win Rate
79%
Profit-Loss Ratio
0.77
Alpha
0.433
Beta
0.214
Annual Standard Deviation
0.214
Annual Variance
0.046
Information Ratio
1.454
Tracking Error
0.244
Treynor Ratio
2.122
Total Fees
$118061.74
Estimated Strategy Capacity
$880000.00
Lowest Capacity Asset
TBF UF9WRZG9YA1X
Portfolio Turnover
14.12%
from AlgorithmImports import *
from datetime import datetime, timedelta

class CustomDataWeighAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 1, 1)
       # self.SetEndDate(2023, 2, 1)
        self.SetCash(100000)

        # Define the symbol and "type" of our generic data:
        self.AddData(Weigh, "Weigh")
        self.symbols = [
            "SHY", "VIXM", "BND", "BIL", "TLT", "TECL", "TQQQ", "UPRO",
            "TMF", "QID", "TBF", "USDU", "IEI", "GLD", "TIP", "BSV", "SPY"
        ]

        # Inizializza un dizionario per memorizzare le allocazioni iniziali degli asset
        

        # Aggiungi tutti gli asset con risoluzione giornaliera (daily)
        for symbol in self.symbols:
            self.AddEquity(symbol, Resolution.Daily)

    def OnData(self, data):
        if not data.ContainsKey("Weigh"):
            return

        index_data = data["Weigh"]

        # Stampa i valori degli indici
        self.Debug(f"Data Time: {index_data.Time}, SHY: {index_data['SHY']}")
        self.Debug(f"Data Time: {index_data.Time}, VIXM: {index_data['VIXM']}")
        self.Debug(f"Data Time: {index_data.Time}, BND: {index_data['BND']}")
        self.Debug(f"Data Time: {index_data.Time}, BIL: {index_data['BIL']}")
        self.Debug(f"Data Time: {index_data.Time}, TLT: {index_data['TLT']}")
        self.Debug(f"Data Time: {index_data.Time}, TECL: {index_data['TECL']}")
        self.Debug(f"Data Time: {index_data.Time}, TQQQ: {index_data['TQQQ']}")
        self.Debug(f"Data Time: {index_data.Time}, UPRO: {index_data['UPRO']}")
        self.Debug(f"Data Time: {index_data.Time}, TMF: {index_data['TMF']}")
        self.Debug(f"Data Time: {index_data.Time}, QID: {index_data['QID']}")
        self.Debug(f"Data Time: {index_data.Time}, TBF: {index_data['TBF']}")
        self.Debug(f"Data Time: {index_data.Time}, USDU: {index_data['USDU']}")
        self.Debug(f"Data Time: {index_data.Time}, IEI: {index_data['IEI']}")
        self.Debug(f"Data Time: {index_data.Time}, GLD: {index_data['GLD']}")
        self.Debug(f"Data Time: {index_data.Time}, BSV: {index_data['BSV']}")
        self.Debug(f"Data Time: {index_data.Time}, SPY: {index_data['SPY']}")
        
        
        self.SetHoldings("SHY",index_data['SHY'])
        self.SetHoldings("VIXM",index_data['VIXM'])
        self.SetHoldings("BND",index_data['BND'])
        self.SetHoldings("BIL",index_data['BIL'])
        self.SetHoldings("TLT",index_data['TLT'])
        self.SetHoldings("TECL",index_data['TECL'])
        self.SetHoldings("TQQQ",index_data['TQQQ'])
        self.SetHoldings("UPTO",index_data['UPRO'])
        self.SetHoldings("TMF",index_data['TMF'])
        self.SetHoldings("QID",index_data['QID'])
        self.SetHoldings("TBF",index_data['TBF'])
        self.SetHoldings("USDU",index_data['USDU'])
        self.SetHoldings("IEI",index_data['IEI'])
        self.SetHoldings("GLD",index_data['GLD'])
        self.SetHoldings("TIP",index_data['TIP'])
        self.SetHoldings("BSV",index_data['BSV'])
        self.SetHoldings("SPY",index_data['SPY'])
        

        
class Weigh(PythonData):
    '''Weigh Custom Data Class'''
    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource("https://www.dropbox.com/scl/fi/potp7czdbwk0erwjbybq2/Weights_.csv?rlkey=2c60wr2ulqs4by7kdhnj61nk3&dl=1", SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
        if not (line.strip() and line[0].isdigit()):
            return None

        index = Weigh()
        index.Symbol = config.Symbol

        try:
            # Example File Format:
            data = line.split(',')
            index.Time = datetime.strptime(data[0], "%Y-%m-%d")
            index.EndTime = index.Time + timedelta(days=1)
            index.Value = data[4]
            index["SHY"] = float(data[1])
            index["VIXM"] = float(data[2])
            index["BND"] = float(data[3])
            index["BIL"] = float(data[4])
            index["TLT"] = float(data[5])
            index["TECL"] = float(data[6])
            index["TQQQ"] = float(data[7])
            index["UPRO"] = float(data[8])
            index["TMF"] = float(data[9])
            index["QID"] = float(data[10])
            index["TBF"] = float(data[11])
            index["USDU"] = float(data[12])
            index["IEI"] = float(data[13])
            index["GLD"] = float(data[14])
            index["TIP"] = float(data[15])
            index["BSV"] = float(data[16])
            index["SPY"] = float(data[17])
        except ValueError as e:
            self.Debug(f"Errore nella lettura dei dati: {str(e)}")
            return None

        return index