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
-13.54
Tracking Error
0.076
Treynor Ratio
0
Total Fees
$0.00
'''
Chande Momentum Oscillator 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 ChandeAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2020, 8, 1)  # Set Start Date
        self.SetEndDate(2020, 8, 27)  # Set End Date
        self.SetCash(10000)  # Set Strategy Cash
        
        self.tradebar_period = 5

        # SET THE INSTRUMENTS WE ARE GOING TO USE IN OUR UNIVERSE
        self.long_symbol  = self.AddEquity("SPXL", Resolution.Minute,Market.USA, True, 1, True).Symbol
        self.short_symbol = self.AddEquity("SPXS", Resolution.Minute).Symbol
    
        #set up Chande Momentum Oscillator
        self.Chande_OverBought  =  50
        self.Chande_OverSold    = -50
        self.Chande_buy_signal  = False
        self.Chande_sell_signal = False
        
        self.Chande = self.CMO(self.long_symbol, 9, Resolution.Minute)
        self.RegisterIndicator(self.long_symbol, self.Chande, timedelta(minutes = self.tradebar_period))
        
        #set up the Paraboic Stop and Reverse indicator
        self.ParabolicSAR = self.PSAR(self.long_symbol, 0.02, 0.02, 0.2, Resolution.Minute)
        self.RegisterIndicator(self.long_symbol, self.ParabolicSAR, timedelta(minutes = self.tradebar_period))
        
        #  set up Ichimoku Cloud
        TenkanPeriod  = 9
        KijunPeriod   = 26
        SenkouAPeriod = 26
        SenkouBPeriod = 52
        SenkouADelay  = 26
        SenkouBDelay  = 26
        
        self.Ichi = IchimokuKinkoHyo(self.long_symbol, TenkanPeriod, KijunPeriod, SenkouAPeriod, SenkouBPeriod, SenkouADelay, SenkouBDelay)
        self.RegisterIndicator(self.long_symbol, self.Ichi, timedelta(minutes = self.tradebar_period))
        
        # 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.CloudTop          = 0
        self.CloudBottom       = 0
        self.Price_AboveCloud  = False
        self.Price_InsideCloud = False
        self.Price_BelowCloud  = False
        self.TK_AboveCloud     = False
        self.TK_BelowCloud     = False
        self.ToverK            = False
        self.TunderK           = False
        
       
                
       # Warmup those indicators
        self.SetWarmup(SenkouBPeriod * self.tradebar_period)
        
        # Consolidate time and call the handler     
        Consolidator = TradeBarConsolidator(timedelta(minutes = self.tradebar_period))
        Consolidator.DataConsolidated += self.TradebarHandler
        self.SubscriptionManager.AddConsolidator(self.long_symbol, Consolidator)

        self.marketisopen = False
        self.Schedule.On(self.DateRules.EveryDay(self.long_symbol), self.TimeRules.AfterMarketOpen(self.long_symbol), self.OnMarketOpen)
        self.Schedule.On(self.DateRules.EveryDay(self.long_symbol), self.TimeRules.BeforeMarketClose(self.long_symbol), self.OnMarketClose)

    def OnData(self, data):

        if self.IsWarmingUp:
            return
        
         # checks to make sure we have data to trade with
        if (not data.ContainsKey(self.long_symbol) or data[self.long_symbol] is None) or (not data.ContainsKey(self.short_symbol) or data[self.short_symbol] is None):
            return
        


    def TradebarHandler(self, sender, bar):
    
        if self.IsWarmingUp:
            return
        
        #we have data feeding the indicators in the pre-market, but don't need to see it in our logs
        if not self.marketisopen:
            return

        
        self.Debug("Price: " + str(round(self.Securities[self.long_symbol].Price,2)))
    
        #self.Debug("long symbol TradeBar Open "    + str(round(bar.Open,2)))
        #self.Debug("long symbol TradeBar Close "   + str(round(bar.Close,2)))
        #self.Debug("long symbol TradeBar High "    + str(round(bar.High,2)))
        #self.Debug("long symbol TradeBar Low "     + str(round(bar.Low,2)))
        #self.Debug("long symbol TradeBar Volume "  + str(round(bar.Volume,2)))
        #self.Debug("long symbol TradeBar EndTime " + str(bar.EndTime))
        #self.Debug("long symbol TradeBar Period "  + str(bar.Period))
        
  
        
        self.Chande_Analysis()
        self.PSAR_Analysis()
        self.Ichimoku_Analysis()
        
        self.Debug("----------")
 
        
    def Chande_Analysis(self):
        
        # make sure the algos are ready
        if self.IsWarmingUp:
            return
        
        self.Debug("Chande value: " +str(round(self.Chande.Current.Value,2)))
        
        if (self.Chande.Current.Value >= 0):
            self.Debug("Chande is positive")
            
        elif (self.Chande.Current.Value < 0):
            self.Debug("Chande is negative")
            
            
    def PSAR_Analysis(self):
        
        # make sure the algos are ready
        if self.IsWarmingUp:
            return
        
        PSAR = round(self.ParabolicSAR.Current.Value,2)
        self.Debug("PSAR " + str(PSAR))
        
        
        if (self.ParabolicSAR.Current.Value < self.Securities[self.long_symbol].Price):
            self.Debug("PSAR below price")

            
        elif (self.ParabolicSAR.Current.Value > self.Securities[self.long_symbol].Price):
              self.Debug("PSAR above price")
              
              
    def Ichimoku_Analysis(self):
        
        # make sure the algos are ready
        if self.IsWarmingUp:
            return
        
        
        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)
        
        self.Debug("Cloud Top "    + str(round(self.CloudTop,2)))
        self.Debug("Cloud Bottom " + str(round(self.CloudBottom,2)))

                    
        if (self.Securities[self.long_symbol].Price > self.CloudTop):
            self.Debug("Price above cloud")

        elif (self.Securities[self.long_symbol].Price < self.CloudBottom):
            self.Debug("Price below cloud")
            
        else:
            self.Debug("Price inside cloud")
            
            
            
        if (min(self.Ichi.Tenkan.Current.Value, self.Ichi.Kijun.Current.Value) > self.CloudTop):
            self.Debug("Tenkan/Kijun above cloud")
            
        if (max(self.Ichi.Tenkan.Current.Value, self.Ichi.Kijun.Current.Value) < self.CloudBottom):
            self.Debug("Tenkan/Kijun below cloud")
 
 
            
        if (self.Ichi.Tenkan.Current.Value > self.Ichi.Kijun.Current.Value):
            self.Debug("Tenkan over Kijun")

            
        elif (self.Ichi.Tenkan.Current.Value < self.Ichi.Kijun.Current.Value):
            self.Debug("Kijun over Tenkan")

            
        else: #Tenkan and Kijun are overlaying each other, stay with whichever over/under it was before
            return
            

    def OnMarketOpen(self):
        
        self.marketisopen = True
        
        
    def OnMarketClose(self):
        
        self.marketisopen = False