Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
33.396%
Drawdown
5.100%
Expectancy
0
Net Profit
15.417%
Sharpe Ratio
2.087
Probabilistic Sharpe Ratio
76.568%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.007
Beta
1.002
Annual Standard Deviation
0.109
Annual Variance
0.012
Information Ratio
-1.726
Tracking Error
0.004
Treynor Ratio
0.228
Total Fees
$1.35
Estimated Strategy Capacity
$88000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
#region imports
from AlgorithmImports import *

#endregion

class AlertBrownAnt(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 1, 1)
        self.SetEndDate(2021, 7, 1)
        self.SetCash(100000)

        # Create a SharpeRatio indicator
        riskFreeRate = 0.0001       # Let's assume the 10-day risk-free rate as 0.01%.
        period = 10                 # We want the SR of the recent 10 days
        self.sr = SharpeRatio(period, riskFreeRate)
        
        # Create a RollingWindow to store the last 30 datapoint of the SR indicator value.
        self.window = RollingWindow[float](30)

        # Set up a handler to update the RollingWindow when new indicator data point is received.
        self.sr.Updated += self.SRIndicatorHandler

        self.AddEquity("SPY", Resolution.Minute)

        # Create a schedule event at 00:00 everyday to update the indicator with the end-of-day portfolio value
        self.Schedule.On(self.DateRules.EveryDay("SPY"), 
                         self.TimeRules.At(0, 0),
                         self.UpdateSRIndicator)


    def UpdateSRIndicator(self):
        # Update the SR indicator with time and portfolio/asset value
        portfolio_value = self.Portfolio.TotalPortfolioValue
        self.sr.Update(self.Time, portfolio_value)

        if self.sr.IsReady:
            self.Log(f"Portfolio Value: {portfolio_value}, 10-day Rolling Sharpe Ratio: {self.sr.Current.Value}")
            self.Plot("Rolling Sharpe Ratio", "10D rolling Sharpe Ratio", self.sr.Current.Value)


    def SRIndicatorHandler(self, sender, updated):
        # Add the indicator data point value to the RollingWindow
        if self.sr.IsReady:
            self.window.Add(updated.Value)


    def OnData(self, data):
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 1.)