Overall Statistics
Total Trades
38
Average Win
6.61%
Average Loss
-2.57%
Compounding Annual Return
0.229%
Drawdown
21.000%
Expectancy
0.127
Net Profit
2.315%
Sharpe Ratio
0.069
Probabilistic Sharpe Ratio
0.057%
Loss Rate
68%
Win Rate
32%
Profit-Loss Ratio
2.57
Alpha
0.006
Beta
0.009
Annual Standard Deviation
0.1
Annual Variance
0.01
Information Ratio
-0.678
Tracking Error
0.166
Treynor Ratio
0.756
Total Fees
$428.47
# documentation : 
#https://www.quantconnect.com/lean/documentation/topic28737.html
#https://www.quantconnect.com/forum/discussion/4507/output-of-ichimoku-indicator/p1
import numpy as np
from datetime import timedelta
from datetime import datetime
import pandas as pd


from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
from System.Drawing import Color


class inchimoku(QCAlgorithm):

 

     def Initialize(self):
        self.SetStartDate(2010, 1, 1)
        self.SetEndDate(2020, 1, 1)
        self.SetCash(100000)
        self.symbol = "FSLR"
        
        
        self.AddEquity(self.symbol, Resolution.Daily)
        self.ichimoku = self.ICHIMOKU(self.symbol,9, 26, 26, 52, 26, 26, Resolution.Daily) 

        
       
        #self.SetWarmUp(52,Resolution.Daily)
        
        
        ichimokuPlot = Chart('ichimokuPlot')
        
        ichimokuPlot.AddSeries(Series('Tenkan', SeriesType.Line, "", Color.Blue))
        ichimokuPlot.AddSeries(Series('Kijun', SeriesType.Line, "", Color.Purple))
        ichimokuPlot.AddSeries(Series('SenkouA', SeriesType.Line, "", Color.Orange))
        ichimokuPlot.AddSeries(Series('SenkouB', SeriesType.Line, "", Color.Yellow))
        ichimokuPlot.AddSeries(Series('Chikou', SeriesType.Line, "", Color.Pink))
        ichimokuPlot.AddSeries(Series('Price', SeriesType.Line, "", Color.Gray))
        ichimokuPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Green, ScatterMarkerSymbol.Triangle))
        ichimokuPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.TriangleDown))
        
        self.AddChart(ichimokuPlot)
        
        self.highestPrice = 0
        self.buyPrice = 0
        
        self.rwPrice = RollingWindow[float](26)
        self.rwKijun = RollingWindow[float](2)
        self.rwTenkan = RollingWindow[float](2)
        self.rwSenkouA = RollingWindow[float](26)
        self.rwSenkouB = RollingWindow[float](26)
        self.rwHigh = RollingWindow[float](52)
        self.rwLow = RollingWindow[float](52)


        


     def OnData(self,data):
        if data[self.symbol] is None: return
        
        self.lastClose = data[self.symbol].Close
        self.lastHigh = data[self.symbol].High
        self.lastLow = data[self.symbol].Low
        

        
        #if self.IsWarmingUp: return
    
        if self.ichimoku.IsReady: 
            self.rwPrice.Add(self.lastClose)
            self.rwKijun.Add(self.ichimoku.Kijun.Current.Value)
            self.rwTenkan.Add(self.ichimoku.Tenkan.Current.Value)
            self.rwSenkouA.Add(self.ichimoku.SenkouA.Current.Value)
            self.rwSenkouB.Add(self.ichimoku.SenkouB.Current.Value)
            self.rwHigh.Add(self.lastHigh)
            self.rwLow.Add(self.lastLow)


            
            if self.rwKijun.IsReady and self.rwPrice.IsReady and self.rwTenkan.IsReady and self.rwSenkouA.IsReady and self.rwSenkouB.IsReady  and self.rwLow.IsReady and self.rwHigh.IsReady:
            
                self.Plot("ichimokuPlot", "Tenkan", self.ichimoku.Tenkan.Current.Value)
                self.Plot("ichimokuPlot", "Kijun", self.ichimoku.Kijun.Current.Value)
                self.Plot("ichimokuPlot", "SenkouA", self.ichimoku.SenkouA.Current.Value)
                self.Plot("ichimokuPlot", "SenkouB", self.ichimoku.SenkouB.Current.Value)
                self.Plot("ichimokuPlot", "Price", self.lastClose)
    
                self.Plot("ichimokuPlot", "Chikou", self.ichimoku.Chikou.Current.Value)
                
                # hard trading rules. 
                
                '''
                1 asic foundation strategy, the conservative safe way:
                • Price above the Kuma Cloud.
                
                • Tenkan Sen greater Ulan the Kijun Sen.
                • Chikou Span is greater than the price from 26 periods ago.
                • Future Senkou A is greater than the Future Senkou B.
                • Price is not far from Kijun Sen and Tenkan Sen.
                • Tenkan Sen, Kijun Sen, and Chikou Span should not be in a Thick Kuma
                    Cloud.
                
                '''
                '''
                
                # softer trading rules
                2 Future Senkou Crossover strategy page 156
                The basic bullish rules for this strategy are:
                
                • Current Senkou A is greater than current Senkou B.
                
                • Price closes above the Kuma Cloud.
                
                • If Tenkan Sen is less than Kijun Sen then Tenkan Sen should be point-
                i.ng upward while the Kijun Sen is flat.
                Or Tenkan Sen is greater than the Kijun Sen.
                
                •Chikou Span is in "open space."
                
                •Future Senkou B is flat or pointing upward.
                •If Future Senkou A is less than Future Senkou B then Future Senkou A
                must be pointing upward.
                
                •Price, Tenkan Sen, Kijun Sen, and Chikou should not be in the Kumo
                Cloud. lf they are, it should be a thick cloud.
                •Price is not far from Tenkan Sen and KijWl Sen.
                •Optional: Future Kumo Cloud is not thick.
                
                '''
                
            
                '''
                2 Future Senkou Crossover strategy page 156
                The basic bullish rules for this strategy are:
                
                • Current Senkou A is greater than current Senkou B.
                '''
                if self.ichimoku.SenkouA.Current.Value > self.ichimoku.SenkouB.Current.Value:
                    
                    
                    #• Price closes above the Kuma Cloud.
                    if self.lastClose > self.ichimoku.SenkouA.Current.Value and self.lastClose > self.ichimoku.SenkouB.Current.Value:
                        
    
                        
                        # if Tenkan Sen is greater than the Kijun Sen continue .
                        #reset
                        TenkanUpAndKijunFlat = False
                        TenkanGreaterThanKijun = False
                        
                        if self.ichimoku.Tenkan.Current.Value > self.ichimoku.Kijun.Current.Value:
                            TenkanGreaterThanKijun = True
                        
                        #• If Tenkan Sen is less than Kijun Sen then Tenkan Sen should be point-
                        #i.ng upward while the Kijun Sen is flat.    
                        if self.ichimoku.Tenkan.Current.Value < self.ichimoku.Kijun.Current.Value:
                            TenkanGreaterThanKijun = False
                            self.tenkanYesterday = self.rwTenkan[self.rwTenkan.Count-1]
                            self.kijunYesterday = self.rwKijun[self.rwKijun.Count-1]
                            #tenkan up? and kijun flat?
                            if self.ichimoku.Tenkan.Current.Value > self.tenkanYesterday and self.ichimoku.Kijun.Current.Value == self.kijunYesterday: 
                            
                                TenkanUpAndKijunFlat = True
                            
                        
                        
                        # (if tenkan > kijun ) or ( kijun is flat and tenkan moves upward  )
                        if TenkanUpAndKijunFlat or TenkanGreaterThanKijun   : 
                                
                          
                             
                            
                            
                            
                            #•Chikou Span is in "open space." 
                            # if it higher than the cloud back then and is it higher than the price
                            # chikou current close is chikou 26 days ago. 
                            self.chikou26DaysAgo = self.lastClose
                            
                            self.SenkouA26daysAgo = self.rwSenkouA[self.rwSenkouA.Count-1]
                            self.SenkouB26daysAgo = self.rwSenkouB[self.rwSenkouB.Count-1]
                            self.price26DaysAgo = self.rwPrice[self.rwSenkouB.Count-1]

                            self.CloudTop26daysAgo = 0
                            if self.SenkouB26daysAgo > self.SenkouA26daysAgo: 
                                self.CloudTop26daysAgo = self.SenkouB26daysAgo
                            
                            if self.SenkouA26daysAgo > self.SenkouB26daysAgo: 
                                self.CloudTop26daysAgo = self.SenkouA26daysAgo
                            
                            
                            if self.chikou26DaysAgo  >  self.CloudTop26daysAgo and self.chikou26DaysAgo > self.price26DaysAgo: 
                               
                               

                                
                                
                                #•Future Senkou B is flat or pointing upward.
                                #Senkou Span B = (52-period high + 52-period low) / 2.
                                SenkouBFuture = (self.rwHigh[self.rwHigh.Count -1] + self.rwLow[self.rwLow.Count -1]) / 2.
                                
                                '''
                                
                                
                                
                                
                                •If Future Senkou A is less than Future Senkou B then Future Senkou A
                                must be pointing upward.
                                
                                •Price, Tenkan Sen, Kijun Sen, and Chikou should not be in the Kumo
                                Cloud. lf they are, it should be a thick cloud.
                                •Price is not far from Tenkan Sen and KijWl Sen.
                                •Optional: Future Kumo Cloud is not thick.
                                '''
                            
                            
                            
                            
                            
                            
                            
                                #BUY
                                #self.TenkanYesterday = self.rwTenkan[self.rwTenkan.Count-1] 
                                        #self.kijunYesterday = self.rwKijun[self.rwKijun.Count-1]
                                        # if price yester is lower than Kijun yesterday  AND price today is > than kijunYesterday.  
                                        #self.TenkanYesterday < self.kijunYesterday and self.Ichimoku.Tenkan.Current.Value > self.Ichimoku.Kijun.Current.Value:
                                        
                                self.priceYesterday = self.rwPrice[1] 
                                self.kijunYesterday = self.rwKijun[1]
                                
                                
                                # if price yester is lower than Kijun yesterday  AND price today is > than kijunYesterday.  
                                
                                # buy at crossing point when price is larger than kijun
                                if self.priceYesterday < self.kijunYesterday and  self.lastClose > self.ichimoku.Kijun.Current.Value:
                    
                                    if not self.Portfolio.Invested:  
                                        self.SetHoldings(self.symbol, 1)
                                        self.Plot("ichimokuPlot", "Buy", self.lastClose)   
                                        self.buyPrice = self.lastClose
                                        
                                        self.highestPrice = 0
                                        self.stopPrice = self.buyPrice
                                        
                            
                #SELL
                        
                # trailing stop
                #self.oldHighestPrice = self.highestPrice
                
                # update highest price
                if self.lastClose > self.highestPrice: 
                    self.highestPrice = self.lastClose 
                    # and change limit
                    self.stopPrice = self.highestPrice * 0.90
                
                
                #self.lastClose < self.ichimoku.SenkouB.Current.Value
                        #or self.lastClose <= self.buyPrice or self.lastClose < self.ichimoku.Kijun.Current.Value
                if self.lastClose <= self.stopPrice or self.lastClose <= self.buyPrice or self.lastClose <= self.ichimoku.Kijun.Current.Value :
                        if self.Portfolio.Invested:
                            self.Liquidate()
                            self.Plot("ichimokuPlot", "Sell", self.lastClose)