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.308
Tracking Error
0.35
Treynor Ratio
0
Total Fees
$0.00
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from collections import deque

class TransdimensionalCalibratedChamber(QCAlgorithm):

    def Initialize(self):

        # parameters for one stock
        self.symbol = Symbol.Create("AAPL", SecurityType.Equity, Market.USA)
        self.cash_invested = 10000
        self.confidence_threshold = 1.1
        
        # date, equity, brokerage and bencmark
        self.SetStartDate(2020, 10, 1)
        self.SetEndDate(2020, 10, 10)
        self.SetCash(self.cash_invested)
        
        
        #self.AddEquity(self.symbol, Resolution.Hour, fillDataForward=True).SetDataNormalizationMode(DataNormalizationMode.Adjusted)
        
        self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None))
        self.UniverseSettings.Resolution = Resolution.Hour
        
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        self.SetBenchmark(self.symbol)

        # warmp up period
        self.lookback = 300
        self.SetWarmUp(self.lookback)
        
        
    def CoarseSelectionFunction(self, coarse):
        return [self.symbol]
    
    def FineSelectionFunction(self, fine):
        return [self.symbol]
        
    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            self.sadf = MyIndicator('sadf', self.lookback, self, security.Symbol)
            self.RegisterIndicator(self.symbol, self.sadf, Resolution.Daily)
        
class MyIndicator(PythonIndicator):
    def __init__(self, name, period, algorithm, symbol):
        self.Name = name
        self.Time = datetime.min
        self.Value = 0
        # self.IsReady = False
        self.queue = deque(maxlen=period)
        self.queueTime = deque(maxlen=period)
        self.CurrentReturn = 0
        
        self.algorithm = algorithm
        self.security = algorithm.Securities[symbol]
        
    def Update(self, input):
        self.algorithm.Log(f"Input type: {type(input)}")
        
        pe_ratio = self.security.Fundamentals.ValuationRatios.NormalizedPERatio
        self.algorithm.Plot("Normalized PE", "Ratio", pe_ratio)
        
        self.queue.appendleft(input.Price)
        self.queueTime.appendleft(input.EndTime)
        self.Time = input.EndTime
        if len(self.queue) > 100:
            close_ = pd.Series(self.queue, index=self.queueTime).rename('close').sort_index()
            self.CurrentReturn = close_.pct_change(periods=1)[-1]
            self.PreviousReturn = close_.pct_change(periods=1)[-2]
            self.Value = self.sadf_last(close=close_)
        count = len(self.queue)
        # self.IsReady = count == self.queue.maxlen
        return count == self.queue.maxlen