def __init__(self, settings=None, name=None): self.name = name ExitManager.__init__(self, settings) bb_period = self._getintsetting("BollingerBreakoutExitManager", "bbPeriod") bb_stdevs = self._getfloatsetting("BollingerBreakoutExitManager", "bbStdevs") self.close = AdjustedClose() self.bb = BollingerBands(metric=self.close, period=bb_period, stdev=bb_stdevs) self._addMetrics(self.close, self.bb)
class ExitManager: def __init__(self, settings): bbperiod = settings.getint("BollingerFadeEntry", "bbperiod") stdev = settings.getfloat("BollingerFadeEntry", "stdev") self.bb = BollingerBands(period=bbperiod, stdev=stdev) def handle(self, perioddata): self.perioddata = perioddata self.bb.handle(self.perioddata) def checkTrade(self, trade): if ( trade != None and self.bb.ready() and (self.perioddata.close >= self.bb.upperBand() or self.perioddata.close <= self.bb.lowerBand()) ): trade.exit = self.perioddata.date trade.exitPrice = self.perioddata.close return trade
class BollingerBreakoutExitManager(ExitManager): def __init__(self, settings=None, name=None): self.name = name ExitManager.__init__(self, settings) bb_period = self._getintsetting("BollingerBreakoutExitManager", "bbPeriod") bb_stdevs = self._getfloatsetting("BollingerBreakoutExitManager", "bbStdevs") self.close = AdjustedClose() self.bb = BollingerBands(metric=self.close, period=bb_period, stdev=bb_stdevs) self._addMetrics(self.close, self.bb) def checkTrade(self, trade): if trade is not None and trade.is_open(): if trade.is_long(): if self.close.value() < self.bb.lowerBand(): trade.close(self.perioddata.date, self.perioddata.adjustedClose) else: if self.close.value() > self.bb.upperBand(): trade.close(self.perioddata.date, self.perioddata.adjustedClose) return trade
class BollingerBreakoutEntryManager(NoScaleInEntryManager): def __init__(self, settings=None, name=None): NoScaleInEntryManager.__init__(self, settings, name) self.min_price = self._getfloatsetting("BollingerBreakoutEntryManager", "minPrice") self.min_vol = self._getintsetting("BollingerBreakoutEntryManager", "minVolume") bb_period = self._getintsetting("BollingerBreakoutEntryManager", "bbPeriod") bb_stdevs = self._getfloatsetting("BollingerBreakoutEntryManager", "bbStdevs") self.do_long = self._getboolsetting("BollingerBreakoutEntryManager", "doLong") self.do_short = self._getboolsetting("BollingerBreakoutEntryManager", "doShort") sma_period = self._getintsetting("BollingerBreakoutEntryManager", "smaPeriod") if sma_period is not None: self.sma = SimpleMovingAverage(period=sma_period) else: self.sma = None self.raw_close = Close() self.close = AdjustedClose() self.vol = AverageVolume() self.bb = BollingerBands(metric=self.close, period=bb_period, stdev=bb_stdevs) self._addMetrics(self.sma, self.raw_close, self.close, self.vol, self.bb) def _checkTradeNoScale(self): trade = None if self.raw_close.value() >= self.min_price and self.vol.value() >= self.min_vol \ and self.close.value() > self.bb.upperBand() and self.do_long \ and (self.sma is None or self.close.value() > self.sma.value()): entry_price = self.close.value() stop = 0 trade = Trade(stock=self.periodData.stock, entry=self.periodData.date, entryPrice=entry_price, stop=stop) if self.raw_close.value() >= self.min_price and self.vol.value() >= self.min_vol \ and self.close.value() < self.bb.upperBand() and self.do_short \ and (self.sma is None or self.close.value() < self.sma.value()): entry_price = self.close.value() stop = entry_price * 10 trade = Trade(stock=self.periodData.stock, entry=self.periodData.date, entryPrice=entry_price, stop=stop) return trade
def __init__(self, settings, triggerLongPrice=None, trigerShortPrice=None): bbperiod = settings.getint("BollingerFadeEntry", "bbperiod") stdev = settings.getfloat("BollingerFadeEntry", "stdev") minHourStr = settings.get("BollingerFadeEntry", "minHour") maxHourStr = settings.get("BollingerFadeEntry", "maxHour") if minHourStr == None or minHourStr == "None": self.minHour = None else: self.minHour = int(minHourStr) if maxHourStr == None or maxHourStr == "None": self.maxHour = None else: self.maxHour = int(maxHourStr) self.bb = BollingerBands(period=bbperiod, stdev=stdev) self.mode = None
def __init__(self, settings=None, name=None): NoScaleInEntryManager.__init__(self, settings, name) self.min_price = self._getfloatsetting("BollingerBreakoutEntryManager", "minPrice") self.min_vol = self._getintsetting("BollingerBreakoutEntryManager", "minVolume") bb_period = self._getintsetting("BollingerBreakoutEntryManager", "bbPeriod") bb_stdevs = self._getfloatsetting("BollingerBreakoutEntryManager", "bbStdevs") self.do_long = self._getboolsetting("BollingerBreakoutEntryManager", "doLong") self.do_short = self._getboolsetting("BollingerBreakoutEntryManager", "doShort") sma_period = self._getintsetting("BollingerBreakoutEntryManager", "smaPeriod") if sma_period is not None: self.sma = SimpleMovingAverage(period=sma_period) else: self.sma = None self.raw_close = Close() self.close = AdjustedClose() self.vol = AverageVolume() self.bb = BollingerBands(metric=self.close, period=bb_period, stdev=bb_stdevs) self._addMetrics(self.sma, self.raw_close, self.close, self.vol, self.bb)
class BollingerFadeEntryManager(NoScaleInEntryManager): def __init__(self, settings, triggerLongPrice=None, trigerShortPrice=None): bbperiod = settings.getint("BollingerFadeEntry", "bbperiod") stdev = settings.getfloat("BollingerFadeEntry", "stdev") minHourStr = settings.get("BollingerFadeEntry", "minHour") maxHourStr = settings.get("BollingerFadeEntry", "maxHour") if minHourStr == None or minHourStr == "None": self.minHour = None else: self.minHour = int(minHourStr) if maxHourStr == None or maxHourStr == "None": self.maxHour = None else: self.maxHour = int(maxHourStr) self.bb = BollingerBands(period=bbperiod, stdev=stdev) self.mode = None def handle(self, perioddata): self.bb.handle(perioddata) if self.bb.ready() and perioddata.low < self.bb.lowerBand(): self.mode = "short" elif self.bb.ready() and perioddata.high > self.bb.upperBand(): self.mode = "long" self.lastpd = perioddata def _checkTradeNoScale(self): trade = None if self.bb.ready() and self.lastpd.date.hour >= self.minHour and self.lastpd.date.hour < self.maxHour: if self.mode == "long" and self.lastpd.close <= self.bb.sma.value(): trade = Trade(self.lastpd.stock, self.lastpd.date, self.lastpd.close, self.bb.lowerBand() - 0.01) if self.mode == "short" and self.lastpd.close >= self.bb.sma.value(): trade = Trade(self.lastpd.stock, self.lastpd.date, self.lastpd.close, self.bb.upperBand() + 0.01) return trade
def __init__(self, settings): bbperiod = settings.getint("BollingerFadeEntry", "bbperiod") stdev = settings.getfloat("BollingerFadeEntry", "stdev") self.bb = BollingerBands(period=bbperiod, stdev=stdev)
def findsetups(self, fromdt, todt): stocks = self._getTickers(fromdt, datastore) for stock in stocks: # padded extra to make sure 200 day sma has enough trading days to work with before our window smafromdt = fromdt - timedelta(days=self.period+1) dailydata = datastore.getData(stock, self.timescale, smafromdt, todt) bb = BollingerBands(period=self.period, stdev=float(self.bandwidth)) outerbb = None if self.bandStop != None: outerbb = BollingerBands(period=self.period, stdev=self.bandStop) sma = None if self.sma: sma = SimpleMovingAverage(period=self.sma) lastdd = None trade = None for pd in dailydata: bb.handle(pd) if outerbb != None: outerbb.handle(pd) if sma != None: sma.handle(pd) # check for long stopout if trade != None and trade.stop < trade.entryPrice and pd.low <= trade.trailingstop: trade.exit = pd.date trade.exitPrice = min(pd.open, trade.trailingstop) self.tradeManager.addTrade(trade) trade = None # check for long exit if trade != None and ((self.exitAtOtherBand == False and pd.close > bb.movingAverage()) or (self.exitAtOtherBand and pd.close >= bb.upperBand())): trade.exit = pd.date trade.exitPrice = pd.close self.tradeManager.addTrade(trade) trade = None # check for short stopout if trade != None and trade.stop > trade.entryPrice and pd.high >= trade.trailingstop: trade.exit = pd.date trade.exitPrice = max(pd.open, trade.trailingstop) self.tradeManager.addTrade(trade) trade = None # check for short exit if trade != None and ((self.exitAtOtherBand == False and pd.close < bb.movingAverage()) or (self.exitAtOtherBand and pd.close <= bb.lowerBand())): trade.exit = pd.date trade.exitPrice = pd.close self.tradeManager.addTrade(trade) trade = None # check for new long if trade == None and self.doLong and pd.close > self.minprice and bb.ready() and pd.date >= fromdt and \ pd.close < bb.lowerBand() and (sma == None or sma.value() < pd.close): stop = 0 if self.percentStop != None: stop = pd.close * (1-self.percentStop) if self.bandStop != None: stop = outerbb.lowerBand() trade = Trade(stock=stock, entry=pd.date, entryPrice=pd.close, stop=stop) # check for new short if trade == None and self.doShort and pd.close > self.minprice and bb.ready() and pd.date >= fromdt and \ pd.close > bb.upperBand() and (sma == None or sma.value() > pd.close): stop = 0 if self.percentStop != None: stop = pd.close * (1+self.percentStop) if self.bandStop != None: stop = outerbb.upperBand() trade = Trade(stock=stock, entry=pd.date, entryPrice=pd.close, stop=stop) if trade != None and trade.entry == None: trade.exit = lastdd.date trade.exitPrice = lastdd.close self.tradeManager.addTrade(trade) trade = None return self.tradeManager.getStats()