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
-1.891
Tracking Error
0.104
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
AddReference("NodaTime")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Securities import *
from QuantConnect.Orders import *
from datetime import datetime
from System.Drawing import Color
from NodaTime import DateTimeZone
from QuantConnect.Brokerages import *
from QuantConnect.Data.Market import *
from QuantConnect import *


import decimal as d
import numpy as np

class BBTrend(QCAlgorithm):

    def Initialize(self):
        
        # configuration parameters (configurable inputs into the algorithm)
        DEBUG_LOG = False
        MINUTES_AFTER_OPEN = 0
        MINUTES_BEFORE_CLOSE = 1
        SYMBOL = "SPY"
        BBLENGTH = 20
        BBDEV = 2
        
        self.ORDER_MAP = ["Market", "Limit", "StopMarket", "StopLimit", "MarketOnOpen", "MarketOnClose", "OptionExercise"]
        
        self.DEBUG = DEBUG_LOG

        # initialization
        self.SetStartDate(2020, 9, 28)
        self.SetEndDate(2020, 9, 30)
        self.SetCash(100000)
        #self.stock = self.AddEquity(SYMBOL, Resolution.Second, extendedMarketHours = True)
        self.stock = self.AddEquity(SYMBOL, Resolution.Second, Market.USA, True, 1, True)
        self.SetTimeZone(TimeZones.Chicago)
        
        # Assigning Interactive Brokerage as our brokerage model
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        
        # Assigning securities custom slippage models:
        # self.Securities[SYMBOL].SetSlippageModel(CustomSlippageModel(self))
        
        self.Schedule.On(self.DateRules.EveryDay(self.stock.Symbol), self.TimeRules.AfterMarketOpen(self.stock.Symbol, MINUTES_AFTER_OPEN), self.OnMarketOpen)
        self.Schedule.On(self.DateRules.EveryDay(self.stock.Symbol), self.TimeRules.BeforeMarketClose(self.stock.Symbol, MINUTES_BEFORE_CLOSE), self.OnMarketClose)
        # set the trade flag to False. we'll only start trading when the flag flips to True (after the market open event)
        self.tradeFlag = False
        self.bb = BollingerBands(BBLENGTH, 2, MovingAverageType.Exponential)
        
        self.lastPrice = 0.0
        self.pl = 0.0
        self._lo = None
        
        # self.SetWarmUp(BBLENGTH * 5, Resolution.Minute)
        
        self.Consolidate(SYMBOL, timedelta(minutes=5), self.OnStockBarConsolidated)
        
        
    # OnMarketOpen event, callback from our TimeRules.AfterMarketOpen initialization        
    def OnMarketOpen(self):
        # start trading!
        self.tradeFlag = True
        
    # OnMarketClose event, callback from our TimeRules.BeforeMarketClose initialization
    def OnMarketClose(self):
        # liquidate all holdings
        if self.stock.Invested:
            self.Liquidate(self.stock.Symbol, "EOD Liquidate")
        else:
            self.Transactions.CancelOpenOrders()

        # reset trade flag for following day
        self.tradeFlag = False
        
        if self.DEBUG:
            self.Debug("Profit/Loss as of " + str(self.Time) + ": " + str(self.pl) + " | Portfolio Value: " + str(self.Portfolio.TotalPortfolioValue))
        
   
    def OnStockBarConsolidated(self, consolidated):
        self.bb.Update(consolidated.EndTime, consolidated.Close)
        self.Plot("IsReady", "Val", int(self.bb.IsReady))
        
        if self.IsWarmingUp or not self.tradeFlag:
            return