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
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.786
Tracking Error
0.141
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from datetime import datetime
import numpy as np
import pandas as pd

class FatRedOrangeLion(QCAlgorithm):

    def Initialize(self):
        self.period = 63
        self.SetStartDate(2014, 1, 1)
        self.SetEndDate(2022, 1, 1)
        self.SetCash(100000)
        self.SetWarmup(self.period)
        
        self.stock = self.AddEquity('AAPL', Resolution.Daily).Symbol
        self.volatility = Volatility(self.period)
        
        self.RegisterIndicator(self.stock, self.volatility, Resolution.Daily)
        # self.RegisterIndicator(self.stock, self.volatility, Resolution.Daily, Field.Volume)
        
        chart = Chart('Results')
        chart.AddSeries(Series('Price', SeriesType.Line, 0))
        chart.AddSeries(Series('Volatility', SeriesType.Line, 1))
        self.AddChart(chart)

    def OnData(self, data):
        if data.ContainsKey(self.stock) and data[self.stock]:
            self.Plot('Results', 'Price', data[self.stock].Close)
            self.Plot('Results', 'Volatility', self.volatility.Current.Value)
        
class Volatility:
    
    def __init__(self, period):
        self.Period = period
        self.Values = RollingWindow[float](period)
        self.Value = 0
        self.Current = IndicatorDataPoint(datetime.min, self.Value)
        self.IsReady = False

    def Update(self, input):
        self.Values.Add(input.Value)
        value = pd.Series(self.Values).iloc[::-1].pct_change().std() * np.sqrt(252)
        self.Current = IndicatorDataPoint(input.Time, value)
        self.IsReady = self.Values.Count == self.Period
        
        return self.IsReady