Overall Statistics
'''
ICHIMOKU Cloud for 5 minute time buckets

'''

#import a bunch of stuff
from clr import AddReference
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Indicators")

from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Risk import *
from QuantConnect.Algorithm.Framework.Selection import *
from QuantConnect.Data.Consolidators import *

from datetime import timedelta
import numpy as np

from System.Drawing import Color

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.AddCrypto("BTCUSD", Resolution.Minute)
        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 = self.ICHIMOKU(self.long_symbol, TenkanPeriod, KijunPeriod, SenkouAPeriod, SenkouBPeriod, SenkouADelay, SenkouBDelay, Resolution.Hour)
       
        self.EMA200 = self.EMA(self.long_symbol, 55, Resolution.Hour)
       
       
              
        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"
        self.CloudTop = 0
        self.CloudBottom = 0
        self.AboveCloud = False
        self.BelowCloud = False
        self.ToverK = False
        self.TunderK = False
        
        # 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 OnData(self, data):

        if self.IsWarmingUp:
            return


    def OnBarHandler(self, sender, bar):
    
        if self.IsWarmingUp:
            return
        
        
        
        
        
        self.IchiHandler()
        
        
        
        
        
     
        holdings = self.Portfolio["BTCUSD"].Quantity
        self.CloudTop    = max(self.Ichi.SenkouA.Current.Value, self.Ichi.SenkouB.Current.Value)
        self.CloudBottom = min(self.Ichi.SenkouA.Current.Value, self.Ichi.SenkouB.Current.Value)
        

        if (self.Securities[self.long_symbol].Price > self.CloudTop):
        
            self.AboveCloud = True
        else:
            self.AboveCloud = False
            
        if (self.Securities[self.long_symbol].Price < self.CloudBottom):
            
            self.BelowCloud = True
        else:
            self.BelowCloud = False
            
        if (self.Ichi.Tenkan.Current.Value > self.Ichi.Kijun.Current.Value):
           
            self.ToverK = True
        else:
            self.ToverK = False
            
        if (self.Ichi.Tenkan.Current.Value < self.Ichi.Kijun.Current.Value):
  
            self.TunderK = True

        else:
            self.TunderK = False
            
        
        
        
        
        
            
        if (self.AboveCloud and self.Ichi.Kijun.Current.Value > self.Ichi.Tenkan.Current.Value and  bar.Close > self.EMA200.Current.Value ):
           
            if holdings <= 0:
               self.SetHoldings("BTCUSD", 1.0)
        if (   self.Ichi.Kijun.Current.Value <self.Ichi.Tenkan.Current.Value  ):
           
            if holdings > 0:
               self.Liquidate("BTCUSD")
               #self.SetHoldings("BTCUSD", -0.5)
   
        
  

    
    def IchiHandler(self):
    
        if self.IsWarmingUp:
            return
        
 

        '''
        end of the ICHIMOKU CLOUD logic
        '''