Overall Statistics
Total Trades
576
Average Win
2.65%
Average Loss
-1.26%
Compounding Annual Return
16.019%
Drawdown
20.200%
Expectancy
0.383
Net Profit
254.519%
Sharpe Ratio
0.926
Loss Rate
56%
Win Rate
44%
Profit-Loss Ratio
2.11
Alpha
0.114
Beta
2.512
Annual Standard Deviation
0.177
Annual Variance
0.031
Information Ratio
0.813
Tracking Error
0.177
Treynor Ratio
0.065
Total Fees
$970.07
import numpy as np
from datetime import timedelta

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2010,1, 1)  #Set Start Date
        self.SetEndDate(2018,4,1)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.symbol = 'GOOGL'
        self.AddEquity(self.symbol, Resolution.Daily)
        #self.AddForex(self.symbol, Resolution.Daily, Market.Oanda)
        self.alma = self.ALMA(self.symbol, 75, 6, 0.85)
        self.ha = self.HeikinAshi(self.symbol, Resolution.Daily)
        
        consolidator = TradeBarConsolidator(timedelta(1))
        consolidator.DataConsolidated += self.ConsolidatorHandler
        self.SubscriptionManager.AddConsolidator(self.symbol, consolidator)
        
        self.ichimoku = self.ICHIMOKU(self.symbol, 9, 26, 26, 52, 26, 26, Resolution.Daily)
        
        self.barWindow = RollingWindow[TradeBar](10)
        
        self.SetWarmUp(timedelta(days=100))
        
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol, -1), Action(self.BeforeMarketOpen))
        
    def BeforeMarketOpen(self):
        if self.IsWarmingUp: return
        position = self.Portfolio[self.symbol].Quantity
        
        self.Debug(f"cur ha close: {self.ha.CurrentBar.Close}")
    
        if self.ha.CurrentBar.Close > self.ha.CurrentBar.Open:
            self.SetHoldings(self.symbol, 1)
            
        if position >= 0 and self.ha.CurrentBar.Close < self.ha.CurrentBar.Open:
            #self.SetHoldings(self.symbol, -1, True)
            self.Liquidate(self.symbol)
        
    def ConsolidatorHandler(self, sender, bar):
        self.barWindow.Add(bar)
        
        if self.IsWarmingUp: return
    
            
        #position = self.Portfolio["SPY"].Quantity
        #self.Debug(f"time: {bar.Time}, close: {bar.Close}")
        #if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
        #else: 
        #    self.Liquidate()

    def OnData(self, data):
        bar = data[self.symbol]
        
        if bar is None: return
        
        position = self.Portfolio[self.symbol].Quantity
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

        Arguments:
            data: Slice object keyed by symbol containing the stock data
        '''
        if self.IsWarmingUp: return
    
        self.Debug(f"close: {bar.Close}, tenkan: {self.ichimoku.Tenkan}")
        
            
        """
          if position == 0 and ha_bar.Close > ha_bar.Open and (abs(ha_bar.Close - ha_bar.Open) / abs(prev_ha_bar.Close - prev_ha_bar.Open) - 1 >= 0.004 
        or prev_ha_bar.Close > prev_ha_bar.Open): #and self.mmi[symbol].Value < 55: 
            #self.SetHoldings(symbol, self.weights[symbol])
            quantity = self.CalculateOrderQuantity(symbol, 1)
            if quantity > 0:
                self.MarketOnOpenOrder(symbol, quantity)
                
        if position == 0 and ha_bar.Close > self.alma[symbol].Current.Value: #and self.mmi[symbol].Value < 55: 
            self.SetHoldings(symbol, self.weights[symbol])
            quantity = self.CalculateOrderQuantity(symbol, 1)
            if quantity > 0:
                self.MarketOnOpenOrder(symbol, quantity)   
        """
    
        #if bar.Close > self.alma.Current.Value:
        #    self.SetHoldings(self.symbol, 1, True)
            
        #if bar.Close < self.alma.Current.Value:
        #    self.SetHoldings(self.symbol, -1, True)
            
        #if position == 1 and bar.Close < self.ichimoku.Tenkan.Current.Value:
        #    self.Liquidate(self.symbol)
            
        #if position == -1 and bar.Close > self.ichimoku.Tenkan.Current.Value:
        #    self.Liquidate(self.symbol)