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
-12.465
Tracking Error
0.024
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# region imports
from AlgorithmImports import *

from collections import deque
# endregion

class VirtualFluorescentPinkSeahorse(QCAlgorithm):
    data_bar = None
    Ticker = 'SPY'
    c=0
    def Initialize(self):
        self.SetStartDate(2022, 7, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash

        desired_resolution = Resolution.Hour
        if desired_resolution == Resolution.Minute:
            self.seconds_delay = 1
            self.minute_delay = 0
        else:
            self.seconds_delay = 0
            self.minute_delay = 1

        self.ticker = self.AddEquity("SPY", desired_resolution)

        # Definition of the Figure
        self.chart = Chart("My Indicators")
        self.AddChart(self.chart)
        
        # Candles creation
        self.candles = {}
        self.candles['heikin'] = Series("Heikin-Ashi", SeriesType.Candle,index=1)
        self.chart.AddSeries(self.candles['heikin'])

        self.candles[self.Ticker] = Series(self.Ticker, SeriesType.Candle,index=0)
        self.chart.AddSeries(self.candles[self.Ticker])

        # Indicator Initialization
        self.heikin = CustomHeikinAshi('CustHeikinAshi')
        self.RegisterIndicator(self.ticker.Symbol, self.heikin, desired_resolution)
            
    def OnData(self, data: Slice):
        if self.heikin.IsReady:
            self.AddCandle(self.Ticker, data[self.ticker.Symbol])
            self.AddCandle('heikin', self.heikin)
    
    def AddCandle(self,indicator,tradeBar):
        if tradeBar == None:
            # There is some missing data in hourly resilution
            return
        if indicator=='SPY':
            self.c+=1
            self.Debug(f'Time: {self.Time},\tNumber:{self.c}')
        self.candles[indicator].AddPoint(tradeBar.EndTime + timedelta(minutes=self.minute_delay,seconds=self.minute_delay), tradeBar.Open)
        self.candles[indicator].AddPoint(tradeBar.EndTime + timedelta(minutes=self.minute_delay*2,seconds=self.minute_delay*2), tradeBar.High)
        self.candles[indicator].AddPoint(tradeBar.EndTime + timedelta(minutes=self.minute_delay*3,seconds=self.minute_delay*3), tradeBar.Low)
        self.candles[indicator].AddPoint(tradeBar.EndTime + timedelta(minutes=self.minute_delay*4,seconds=self.minute_delay*4), tradeBar.Close)

        
class CustomHeikinAshi(PythonIndicator):
    def __init__(self,name):
        self.Name = name
        self.Time = datetime.min
        self.EndTime = datetime.min
        self.Value = 0
        self.Open = 0
        self.Close = 0
        self.High = 0
        self.Low = 0
        self.que = deque(maxlen=2)
    def Update(self,input):
        self.Time = input.EndTime
        self.EndTime = input.EndTime
        self.Value = input.Value
        
        self.Close = np.mean([input.Open,input.Close,input.High,input.Low])
        self.High = np.max([input.High,input.Open,input.Close])
        self.Low = np.min([input.Low,input.Open,input.Close])  

        if not(len(self.que)==self.que.maxlen):
            self.que.append(input.Close)
            self.que.append(input.Open)
            new_open = np.mean(self.que)
            self.Open = new_open
            return False

        new_open = np.mean(self.que)
        self.que.append(self.Close)
        self.que.append(self.Open)
        self.Open = new_open

        return True