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
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
import numpy as np
import pandas as pd
import talib as tb

class UnfilledGap(QCAlgorithm):
    stopMarketTicket = None
    StopPrice = 0
    MarketTicket = None
    
    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2019, 8, 30)
        self.SetCash(100000)
        self.AddEquity("KEYS", Resolution.Minute)
        OneDayATR = self.ATR("KEYS", 1, MovingAverageType.Simple, Resolution.Daily)
        OneDayATR.Updated += self.ATRUpdated
        self.ATRWindow = RollingWindow[IndicatorDataPoint](5)
        self.Window = RollingWindow[TradeBar](5)
        self.SetWarmup(20, Resolution.Daily) 
        
    def ATRUpdated(self, sender, updated):
        self.ATRWindow.Add(updated)
    def TradeBarHandler(self, TradeBar):
        self.Window.Add(TradeBar);        

    def OnData(self, data):
        if not self.Window.IsReady and not self.ATRWindow.IsReady:
            return
        
        TwentyDayATR = self.ATR("KEYS", 20, MovingAverageType.Simple, Resolution.Daily)
        if not TwentyDayATR.IsReady:
            return
        Volume = self.History("KEYS", 20, Resolution.Daily).Volume
        AverageVolume = np.average(Volume)
        
        if not self.keys.Invested:
            if self.ATRWindow[0].Value > TwentyDayATR.Value and self.Securities["KEYS"].Volume > AverageVolume.Value and self.Securities["KEYS"].Price > self.Window[0].High:
                self.MarketTicket = self.MarketOrder("KEYS", 100, True, '1st day')
                self.StopMarketTicket = self.StopMarketOrder("KEYS", -100, self.Window[0].Low, 'Stop Order')
            if self.ATRWindow[1].Value > TwentyDayATR.Value and self.Securities["KEYS"].Volume > AverageVolume.Value and self.Securities["KEYS"].Price > self.Window[0].High:
                self.MarketTicket = self.MarketOrder("KEYS", 100, True, '2nd day')
                self.StopMarketTicket = self.StopMarketOrder("KEYS", -100, self.Window[0].Low, 'Stop Order')
            if self.ATRWindow[2].Value > TwentyDayATR.Value and self.Securities["KEYS"].Volume > AverageVolume.Value and self.Securities["KEYS"].Price > self.Window[0].High:                
                self.MarketTicket = self.MarketOrder("KEYS", 100, True, '3rd day')
                self.StopMarketTicket = self.StopMarketOrder("KEYS", -100, self.Window[0].Low, 'Stop Order')
        else:
            if self.Window[0].Low > self.Window[1].Low:
                self.StopPrice = self.Window[0].Low    
                UpdateFields = UpdateOrderFields()
                UpdateFields.StopPrice = self.StopPrice
                self.stopMarketTicket.Update(UpdateFields)
                
        self.Log(AverageVolume)