Overall Statistics
Total Trades
9
Average Win
8.26%
Average Loss
-2.36%
Compounding Annual Return
-3.160%
Drawdown
14.500%
Expectancy
0.124
Net Profit
-3.415%
Sharpe Ratio
-0.037
Probabilistic Sharpe Ratio
11.780%
Loss Rate
75%
Win Rate
25%
Profit-Loss Ratio
3.50
Alpha
-0.007
Beta
0
Annual Standard Deviation
0.196
Annual Variance
0.038
Information Ratio
-0.515
Tracking Error
0.354
Treynor Ratio
-103.151
Total Fees
$51.10
# kijun= (26 period high + 26 periode low) /2

# 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(2020, 1, 1)
        self.SetEndDate(2021, 2, 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('TenkanCal', SeriesType.Line, "", Color.Red))

        ichimokuPlot.AddSeries(Series('Kijun', SeriesType.Line, "", Color.Purple))
        #ichimokuPlot.AddSeries(Series('KijunCal', SeriesType.Line, "", Color.Green))

        ichimokuPlot.AddSeries(Series('SenkouA', SeriesType.Line, "", Color.Orange))
        ichimokuPlot.AddSeries(Series('SenkouB', SeriesType.Line, "", Color.Yellow))
        #ichimokuPlot.AddSeries(Series('SenkouBtest', SeriesType.Line, "", Color.Yellow))

        ichimokuPlot.AddSeries(Series('SenkouBCal', SeriesType.Line, "", Color.White))

        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](26)
        self.rwTenkan = RollingWindow[float](26)
        self.rwSenkouA = RollingWindow[float](26)
        self.rwSenkouB = RollingWindow[float](26)
        self.rwHigh = RollingWindow[float](52)
        self.rwLow = RollingWindow[float](52)
        self.rwSenkouBCal = RollingWindow[float](28) # to high? should be 26
        self.rwSenkouACal = RollingWindow[float](27)


        


     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
        #data[self.symbol].Time
        

        
        #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:
                
                lowestLow  = min(list(self.rwHigh))
                highestHigh = max(list(self.rwHigh))
     
     
                self.SenkouBCal = (highestHigh + lowestLow) / 2
                
                self.rwSenkouBCal.Add(self.SenkouBCal)
                
                
                self.SenkouACal = ( self.ichimoku.Tenkan.Current.Value + self.ichimoku.Kijun.Current.Value) / 2 
                self.rwSenkouACal.Add(self.SenkouACal)
                
                if self.rwSenkouBCal.IsReady and self.rwSenkouACal.IsReady: 
         
         
         
                    # tenkan= (9 period high + 9 periode low) /2
                    #Tenkan-sen=(Last 9 period high + Last 9 period low)/2
                    #tenkanHighValues= sum(list(self.rwHigh)[:9])
                    #tenkanLowValues= sum(list(self.rwLow)[:9])
    
                    #TenkanCal = (tenkanHighValues +tenkanLowValues) /2 /9
                    self.Plot("ichimokuPlot", "Tenkan", self.ichimoku.Tenkan.Current.Value)
                    #self.Plot("ichimokuPlot", "TenkanCal", TenkanCal)
                    # kijun= (26 period high + 26 periode low) /2
                    #kijunHighValues= sum(list(self.rwHigh)[:26])
                    #kijunLowValues= sum(list(self.rwLow)[:26])
                    
                    #KijunCal = ( kijunHighValues +kijunLowValues) /2 / 26
                    self.Plot("ichimokuPlot", "Kijun", self.ichimoku.Kijun.Current.Value)
                    #self.Plot("ichimokuPlot", "KijunCal", KijunCal)
    
                    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)
                    #max_value = max(list(self.priceWindow)[20:40])
                    #highestHigh = max(list(self.rwHigh))
                    
                    
                    #self.SenkouBCal = (highestHigh + lowestLow) / 2
                    
                    #self.rwSenkouBCal.Add(self.SenkouBCal)
    
                    
    
                    
                    #self.SenkouACal = ( KijunCal + TenkanCal) / 2 # plotted 26 days intothe future
    
                    
                    self.Plot("ichimokuPlot", "SenkouBCal", self.rwSenkouBCal[self.rwSenkouBCal.Count-1])
                    self.Plot("ichimokuPlot", "SenkouACal", self.rwSenkouACal[self.rwSenkouACal.Count-1])
                    #self.Plot("ichimokuPlot", "SenkouBtest", self.ichimoku.SenkouB)
                    #self.Debug(self.ichi)
                    
                    
    
    
                    
                    # hard trading rules. 
                    
                    '''
                    1 basic foundation strategy, the conservative safe way:
                    • Price above the Kuma Cloud.
                    
                    • Tenkan Sen greater than 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[1]
                                self.kijunYesterday = self.rwKijun[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.
                                    
                                    '''
                                    
                                    DelayedTenkanSenkouA
                                    
                                    
                                    •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)
# Your New Python File