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
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
'''
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(2020, 8, 17)  # Set Start Date
        self.SetEndDate(2020, 8, 17)  # Set End Date
        self.SetCash(10000)  # Set Strategy Cash
        
        

        # SET THE INSTRUMENTS WE ARE GOING TO USE IN OUR UNIVERSE
        self.long_symbol  = self.AddEquity("SPXL", Resolution.Minute).Symbol
    
                
        # Ichimoku Cloud
        TenkanPeriod = 9
        KijunPeriod = 26
        SenkouAPeriod = 26
        SenkouBPeriod = 52
        SenkouADelay = 26
        SenkouBDelay = 26
        
        
        self.Ichi = self.ICHIMOKU(self.long_symbol, TenkanPeriod, KijunPeriod, SenkouAPeriod, SenkouBPeriod, SenkouADelay, SenkouBDelay, Resolution.Minute)
        self.RegisterIndicator(self.long_symbol, self.Ichi,   timedelta(minutes=5))
        
        # 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 * 5)
        
        # Consolidate time into 5 min bars and call the handler     
        Consolidator = TradeBarConsolidator(timedelta(minutes=5))
        Consolidator.DataConsolidated += self.OnBarHandler
        self.SubscriptionManager.AddConsolidator(self.long_symbol, Consolidator)
        
        # Add a custom chart
        self.chart = Chart('Trade Chart')
        self.chart.AddSeries(Series('Price', SeriesType.Line, 0))
        self.chart.AddSeries(Series('Sentiment', SeriesType.Line, 1))
        self.AddChart(self.chart)

    def OnData(self, data):

        if self.IsWarmingUp:
            return


    def OnBarHandler(self, sender, bar):
    
        if self.IsWarmingUp:
            return
        
        self.IchiHandler()
        
        if self.Sentiment == "Neutral":
            SentimentNumber = 1
        if self.Sentiment == "Bullish":
            SentimentNumber = 2
        if self.Sentiment == "Bearish":
            SentimentNumber = 0
            
        self.Plot('Trade Chart', 'Price',     self.Securities[self.long_symbol].Price)
        self.Plot('Trade Chart', 'Sentiment', SentimentNumber)
        
    
    
    def IchiHandler(self):
    
        if self.IsWarmingUp:
            return
        
        '''
        This is the IICHIMOKU CLOUD evaluator. 
        This block decides whether self.Sentiment gets set to Bullish, Bearish or Neutral.  
        '''
        
        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):
            if self.BelowCloud: 
                self.Debug("Price moving Above Cloud")
            self.AboveCloud = True
        else:
            self.AboveCloud = False
            
        if (self.Securities[self.long_symbol].Price < self.CloudBottom):
            if self.AboveCloud: 
                self.Debug("Price moving Below Cloud")
            self.BelowCloud = True
        else:
            self.BelowCloud = False
            
        if (self.Ichi.Tenkan.Current.Value > self.Ichi.Kijun.Current.Value):
            if self.TunderK: 
                self.Debug("Tenkan moving over Kijun")
            self.ToverK = True
        else:
            self.ToverK = False
            
        if (self.Ichi.Tenkan.Current.Value < self.Ichi.Kijun.Current.Value):
            if self.ToverK: 
                self.Debug("Kijun moving over Tenkan")
            self.TunderK = True

        else:
            self.TunderK = False
            
        
        if (self.Sentiment == "Bearish" or self.Sentiment == "Neutral") and self.AboveCloud and self.ToverK:
            self.Sentiment = "Bullish"
            self.Debug("Sentiment turning Bullish")
            self.Debug("Price "        + str(self.Securities[self.long_symbol].Price))
            self.Debug("Cloud Top "    + str(self.CloudTop))
            self.Debug("Cloud Bottom " + str(self.CloudBottom))
            self.Debug("Tenkan "       + str(self.Ichi.Tenkan.Current.Value))
            self.Debug("Kijun "        + str(self.Ichi.Kijun.Current.Value))
            
        elif (self.Sentiment== "Bullish" or self.Sentiment == "Neutral") and self.BelowCloud and self.TunderK:
            self.Sentiment = "Bearish"
            self.Debug("Sentiment turning Bearish")
            self.Debug("Price "        + str(self.Securities[self.long_symbol].Price))
            self.Debug("Cloud Top "    + str(self.CloudTop))
            self.Debug("Cloud Bottom " + str(self.CloudBottom))
            self.Debug("Tenkan "       + str(self.Ichi.Tenkan.Current.Value))
            self.Debug("Kijun "        + str(self.Ichi.Kijun.Current.Value))
        
        elif (not self.AboveCloud and not self.BelowCloud):
            self.Sentiment = "Neutral"
            self.Debug("Sentiment turning Neutral")
            self.Debug("Price "        + str(self.Securities[self.long_symbol].Price))
            self.Debug("Cloud Top "    + str(self.CloudTop))
            self.Debug("Cloud Bottom " + str(self.CloudBottom))
            self.Debug("Tenkan "       + str(self.Ichi.Tenkan.Current.Value))
            self.Debug("Kijun "        + str(self.Ichi.Kijun.Current.Value))
            
        else:
            pass

                


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