Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Sortino Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.487
Tracking Error
0.147
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
from AlgorithmImports import *
from QuantConnect.DataSource import *
import numpy as np
import math

class ETFConstituentsDataAlgorithm(QCAlgorithm):

    def Initialize(self) -> None:
        self.SetStartDate(2021, 1, 1)
        self.SetEndDate(2023, 12, 10)
        self.SetCash(100000)

        res = Resolution.Daily
        self.SetWarmup(1000)
     
        self.symbol = self.AddEquity("PEP",res).Symbol
        self.PBRatio_window = RollingWindow[float](1000)
        

        self.Schedule.On(self.DateRules.MonthStart("PEP"),
                 self.TimeRules.AfterMarketOpen("PEP",30),
                 self.Rebalance)

    def Rebalance(self) -> None:
        if self.PBRatio_window.IsReady and not self.IsWarmingUp:
            self.Log(f"{self.Time} Mean PBRatio: {sum([x for x in self.PBRatio_window]) / self.PBRatio_window.Count} ------ LastPBRatio: {[x for x in self.PBRatio_window][0]}")
    
    def OnData(self, slice):
        if slice is None or slice[self.symbol] is None:
            return

        close_price = slice[self.symbol].Close
        book_value = self.Securities[self.symbol].Fundamentals.ValuationRatios.BookValuePerShare
        
        if not math.isnan(close_price/book_value): # check for nan values
            self.PBRatio_window.Add(close_price/book_value)