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.012
Tracking Error
0.437
Treynor Ratio
0
Total Fees
$0.00
import numpy as np
import datetime
from scipy import stats
from collections import deque


class RegimRanking(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020,1,11)
        #self.SetEndDate(2020,3,30)
        
        self.SetCash(10000)
        self.reso = Resolution.Daily
        self.spy = self.AddEquity("SPY",self.reso).Symbol
        
        # Indicators
        self.std = self.STD("SPY",21,self.reso) # Standard deviation of 21-day closes => pipe these data poitns to....

        # 252 / 21 = 12
        self.std_252 = RollingWindow[float](12)
        self.std.Updated += self.UpdateStd252
        
        self.SetWarmup(252)
        
    def UpdateStd252(self, sender, updated):
        if self.std.IsReady:
            self.std_252.Add(self.std.Current.Value)
    
    def OnData(self, data):
        if self.std_252.Count == 12:
            percentile_rank = stats.percentileofscore(list(self.std_252), self.std.Current.Value)
            self.Plot('Custom', 'Percentile Rank', percentile_rank)