Overall Statistics
from datetime import timedelta
import numpy as np

class IchimokuAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2019,1, 1)  # Set Start Date
        #self.SetEndDate(2020, 12, 17)  # Set End Date
        self.SetCash(1000)  # Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.Bitfinex,AccountType.Cash)
        #self.UniverseSettings.Resolution = Resolution.Hours
        #self.UniverseSettings.Leverage = 5;

        # SET THE INSTRUMENTS WE ARE GOING TO USE IN OUR UNIVERSE
        self.symbol = self.AddCrypto("BTCUSD", Resolution.Minute).Symbol
        self.long_symbol  ="BTCUSD"
        #self.long_symbol = self.AddForex("BTCUSD", Resolution.Minute, Market.Oanda).Symbol
                
        # Ichimoku Cloud
        TenkanPeriod = 9
        KijunPeriod = 29
        SenkouAPeriod =30
        SenkouBPeriod =60
        SenkouADelay = 30
        SenkouBDelay = 30
        
        self.Ichi = IchimokuKinkoHyo(TenkanPeriod, KijunPeriod, SenkouAPeriod, SenkouBPeriod, SenkouADelay, SenkouBDelay)
        self.EMA200 = ExponentialMovingAverage(55)
       
        self.RegisterIndicator(self.long_symbol, self.EMA200,   timedelta(hours=2))
        self.RegisterIndicator(self.long_symbol, self.Ichi,   timedelta(hours=2))
        
        # going to use three values for Sentiment:  Bullish, Bearish and Neutral
        # setting default values but these will get re-set during pre-market so not a big deal
        self.Sentiment = "Neutral"
        
        # Warmup those indicators
        self.SetWarmup(SenkouBPeriod * 120)
        
        # Consolidate time into 5 min bars and call the handler     
        Consolidator = QuoteBarConsolidator(timedelta(hours=2))
        Consolidator.DataConsolidated += self.OnBarHandler
        self.SubscriptionManager.AddConsolidator(self.long_symbol, Consolidator)


    def OnBarHandler(self, sender, bar):
    
        if self.IsWarmingUp:
            return
        
        CloudTop    = max(self.Ichi.SenkouA.Current.Value, self.Ichi.SenkouB.Current.Value)
        CloudBottom = min(self.Ichi.SenkouA.Current.Value, self.Ichi.SenkouB.Current.Value)
        
        AboveCloud = bar.Close > CloudTop
        BelowCloud = bar.Close < CloudBottom 
        ToverK = self.Ichi.Tenkan.Current.Value > self.Ichi.Kijun.Current.Value
        TunderK = self.Ichi.Tenkan.Current.Value < self.Ichi.Kijun.Current.Value
            
        if AboveCloud and \
            self.Ichi.Kijun.Current.Value > self.Ichi.Tenkan.Current.Value and \
            bar.Close > self.EMA200.Current.Value and \
            not self.Portfolio[self.symbol].IsLong:
           
           self.SetHoldings("BTCUSD", 1.0)
               
        if self.Ichi.Kijun.Current.Value < self.Ichi.Tenkan.Current.Value and self.Portfolio[self.symbol].IsLong:
           self.Liquidate("BTCUSD")