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
-18.451
Tracking Error
0.067
Treynor Ratio
0
Total Fees
$0.00
from sklearn.linear_model import LinearRegression
import numpy as np
import pandas as pd

class CalibratedHorizontalThrustAssembly(QCAlgorithm):

    def Initialize(self):

        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)

        self.SetStartDate(2017, 1, 1)
        self.SetEndDate(2017, 1, 5)
        # self.SetEndDate(2020, 5, 2)

        self.SetCash(100000)

        self.AddUniverse(self.CoarseSelectionFilter)
        self.UniverseSettings.Resolution = Resolution.Hour

        self.spy = self.AddEquity("SPY", Resolution.Hour).Symbol
        
        self.market_rw = RollingWindow[float](20)
        
        self.rws = {}

    def CoarseSelectionFilter(self, coarse):

        sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
        filteredByPrice = [x.Symbol for x in sortedByDollarVolume if x.Price > 10 and x.HasFundamentalData == True]

        self.filteredByPrice = filteredByPrice[:10]
        return self.filteredByPrice

    def OnData(self, data):

        portfolio_invested = [x.Symbol for x in self.Portfolio.Values if x.Invested]

        symbols = self.filteredByPrice

        if data.Bars.ContainsKey(self.spy):
            self.market_rw.Add(data.Bars[self.spy].Close)

        for x in symbols:
            if data.Bars.ContainsKey(x):
                self.rws[x].Add(data.Bars[x].Close)
            
            closes = list(self.rws[x])  # cast to list when using
            # if you want time-based ordering:
            # closes = list(self.rws[x])[::-1]
            
            self.Log(closes)
            
    def OnSecuritiesChanged(self, changed):
        for security in changed.AddedSecurities:
            symbol = security.Symbol
            if symbol not in self.rws:
                self.rws[symbol] = RollingWindow[float](20)
            
            hist = self.History([symbol], 20, Resolution.Hour).loc[symbol]
            for close in hist['close']:
                self.rws[symbol].Add(close)
            
        for security in changed.RemovedSecurities:
            symbol = security.Symbol
            self.rws.pop(symbol, None)