class GapUpEntryManager: def __init__(self, settings): self.minPrice = settings.getfloat("GapAndGoSwingEntry", "minPrice") self.minAvgVol = settings.getint("GapAndGoSwingEntry", "minAvgVol") self.minPercent = settings.getfloat("GapAndGoSwingEntry", "minPercent") targetstr = settings.get("GapAndGoSwingEntry", "target") if targetstr == "None": self.target = None else: self.target = float(targetstr) self.volume = Volume() self.avgvol = SimpleMovingAverage(metric=self.volume, period=21) self.opn = AdjustedOpen() self.close = AdjustedClose() self.lastClose = HistoricMetric(metric=self.close, period=1) self.high = AdjustedHigh() self.lastHigh = HistoricMetric(metric=self.high, period=1) self.low = AdjustedLow() self.inBottomRange=0.1 self.inTopRange=None def handle(self, perioddata): self.close.handle(perioddata) self.lastClose.handle(perioddata) self.high.handle(perioddata) self.lastHigh.handle(perioddata) self.opn.handle(perioddata) self.volume.handle(perioddata) self.avgvol.handle(perioddata) self.low.handle(perioddata) self.lastdd = perioddata def checkTrade(self): if self.close.ready() and self.lastClose.ready() \ and self.opn.ready() and self.volume.ready() \ and self.avgvol.ready(): if self.lastdd.close >= self.minPrice and self.avgvol.value() >= self.minAvgVol \ and self.lastClose.value() > 0 and self.opn.value() > 0 \ and ((self.opn.value()-self.lastClose.value())/self.lastClose.value()) >= self.minPercent \ and self.low.value() >= self.lastHigh.value() \ and (self.inBottomRange == None or ((self.lastdd.adjustedHigh != self.lastdd.adjustedLow) and (self.close.value()-self.lastdd.adjustedLow)/(self.lastdd.adjustedHigh-self.lastdd.adjustedLow)) <= self.inBottomRange) \ and (self.inTopRange == None or ((self.lastdd.adjustedHigh != self.lastdd.adjustedLow) and (self.close.value()-self.lastdd.adjustedLow)/(self.lastdd.adjustedHigh-self.lastdd.adjustedLow)) >= self.inTopRange): stop = max(0.0, self.low.value() - 0.01) # stop = max(0.0, self.lastHigh.value()) trade = Trade(self.lastdd.stock, self.lastdd.date, self.close.value(), stop) if self.target != None: target = self.close.value() + ((self.close.value()-stop)*self.target) trade.target = target return trade return None def recommendedPreload(self): return 22
class MomoPullbackDailyTrigger(DailyTrigger): def __init__(self, settings): DailyTrigger.__init__(self, settings) self.minprice = settings.getfloat("Strategy", "minprice") self.minvolume = settings.getint("Strategy", "minvolume") self.minmove = settings.getfloat("Strategy", "minmove") smatrend = settings.get("Strategy", "dailysmatrendfilter") if smatrend == "None": self.dailysmatrendfilter = None else: self.dailysmatrendfilter = int(smatrend) self.volume = Volume() self.avgvol = SimpleMovingAverage(self.volume, 21) self.close = AdjustedClose() if self.dailysmatrendfilter is not None: self.ma = SimpleMovingAverage(metric=self.close, period=self.dailysmatrendfilter) else: self.ma = None def ready(self): return (self.ma is None or self.ma.ready()) and self.avgvol.ready() and self.close.ready() def check(self): if self.ready() and self.lastpd is not None: # todo would be a lot faster if I could implement a peek at today to check the move is big enough if self.lastpd.open >= self.minprice \ and (self.ma is None or self.close.value() > self.ma.value()) \ and self.avgvol.value() >= self.minvolume \ and ((self.peekpd.high - self.peekpd.open) / self.peekpd.open) >= self.minmove: logger.debug("Saw minimum move in MomoPullbackDailyTrigger, indicating we have a good day") return True return False def handle(self, perioddata): if self.ma is not None: self.ma.handle(perioddata) self.volume.handle(perioddata) self.avgvol.handle(perioddata) self.close.handle(perioddata) self.lastpd = perioddata def recommendedPreload(self): mapreload = 0 if self.ma is not None: mapreload = self.ma.recommendedPreload() return max(mapreload, self.avgvol.recommendedPreload())
def __init__(self, settings, name=None): NoScaleInEntryManager.__init__(self, settings, name) self.minPrice = settings.getfloat("JBMarwoodSupernovaShortEntry", "minPrice") self.maxPrice = settings.getfloat("JBMarwoodSupernovaShortEntry", "maxPrice") self.minVol = settings.getint("JBMarwoodSupernovaShortEntry", "minVol") self.minPctChange = settings.getfloat("JBMarwoodSupernovaShortEntry", "minPctChange") self.numdays = settings.getint("JBMarwoodSupernovaShortEntry", "numBars") self.onOpen = settings.getboolean("JBMarwoodSupernovaShortEntry", "enterNextOpen") self.onDownClose = settings.getboolean("JBMarwoodSupernovaShortEntry", "enterNextDayDownClose") targetstr = settings.get("JBMarwoodSupernovaShortEntry", "target") if targetstr == "None": self.target = None else: self.target = float(targetstr) self.stopPercent = settings.getfloat("JBMarwoodSupernovaShortEntry", "stopPercent") self.setupYesterday = True self.rawclose = Close() self.close = AdjustedClose() self.oldClose = HistoricMetric(self.close, period=self.numdays) self.change = Subtract(self.close, self.oldClose) self.pctChange = Divide(self.change, self.oldClose) self.volume = Volume() self._addMetric(self.rawclose) self._addMetric(self.close) self._addMetric(self.oldClose) self._addMetric(self.change) self._addMetric(self.pctChange) self._addMetric(self.volume)
def __init__(self, settings): self.minPrice = settings.getfloat("GapAndGoSwingEntry", "minPrice") self.minAvgVol = settings.getint("GapAndGoSwingEntry", "minAvgVol") self.minPercent = settings.getfloat("GapAndGoSwingEntry", "minPercent") targetstr = settings.get("GapAndGoSwingEntry", "target") if targetstr == "None": self.target = None else: self.target = float(targetstr) self.volume = Volume() self.avgvol = SimpleMovingAverage(metric=self.volume, period=21) self.opn = AdjustedOpen() self.close = AdjustedClose() self.lastClose = HistoricMetric(metric=self.close, period=1) self.high = AdjustedHigh() self.lastHigh = HistoricMetric(metric=self.high, period=1) self.low = AdjustedLow() self.inBottomRange=0.1 self.inTopRange=None
def __init__(self, settings): DailyTrigger.__init__(self, settings) self.minprice = settings.getfloat("Strategy", "minprice") self.minvolume = settings.getint("Strategy", "minvolume") self.minmove = settings.getfloat("Strategy", "minmove") smatrend = settings.get("Strategy", "dailysmatrendfilter") if smatrend == "None": self.dailysmatrendfilter = None else: self.dailysmatrendfilter = int(smatrend) self.volume = Volume() self.avgvol = SimpleMovingAverage(self.volume, 21) self.close = Close() if self.dailysmatrendfilter is not None: self.ma = SimpleMovingAverage(period=self.dailysmatrendfilter) else: self.ma = None
class JBMarwoodSupernovaShortEntryManager(NoScaleInEntryManager): def __init__(self, settings, name=None): NoScaleInEntryManager.__init__(self, settings, name) self.minPrice = settings.getfloat("JBMarwoodSupernovaShortEntry", "minPrice") self.maxPrice = settings.getfloat("JBMarwoodSupernovaShortEntry", "maxPrice") self.minVol = settings.getint("JBMarwoodSupernovaShortEntry", "minVol") self.minPctChange = settings.getfloat("JBMarwoodSupernovaShortEntry", "minPctChange") self.numdays = settings.getint("JBMarwoodSupernovaShortEntry", "numBars") self.onOpen = settings.getboolean("JBMarwoodSupernovaShortEntry", "enterNextOpen") self.onDownClose = settings.getboolean("JBMarwoodSupernovaShortEntry", "enterNextDayDownClose") targetstr = settings.get("JBMarwoodSupernovaShortEntry", "target") if targetstr == "None": self.target = None else: self.target = float(targetstr) self.stopPercent = settings.getfloat("JBMarwoodSupernovaShortEntry", "stopPercent") self.setupYesterday = True self.rawclose = Close() self.close = AdjustedClose() self.oldClose = HistoricMetric(self.close, period=self.numdays) self.change = Subtract(self.close, self.oldClose) self.pctChange = Divide(self.change, self.oldClose) self.volume = Volume() self._addMetric(self.rawclose) self._addMetric(self.close) self._addMetric(self.oldClose) self._addMetric(self.change) self._addMetric(self.pctChange) self._addMetric(self.volume) def _checkTradeNoScale(self): trade = None if self.setupYesterday: if self.onOpen: entry = self.periodData.adjustedOpen stop = entry * (1 + self.stopPercent) if entry != stop: trade = Trade(self.periodData.stock, self.periodData.date, entry, stop) if self.target != None: target = self.close.value() * (1.0 - self.target) trade.target = target elif self.onDownClose and self.periodData.adjustedClose < self.periodData.adjustedOpen: stop = self.periodData.adjustedHigh + 0.01 # stop = self.close.value()*(1+self.stopPercent) if stop != self.close.value(): trade = Trade(self.periodData.stock, self.periodData.date, self.close.value(), stop) if self.target != None: target = self.close.value() * (1.0 - self.target) trade.target = target if ( self.pctChange.ready() and self.rawclose.value() >= self.minPrice and self.rawclose.value() <= self.maxPrice and self.volume.value() >= self.minVol and self.pctChange.value() >= self.minPctChange ): if not self.onDownClose and not self.onOpen: # enter immediately stop = self.close.value() * (1 + self.stopPercent) if stop != self.close.value(): trade = Trade(self.periodData.stock, self.periodData.date, self.close.value(), stop) if self.target != None: target = self.close.value() * (1.0 - self.target) trade.target = target self.setupYesterday = True else: self.setupYesterday = False if trade is not None and (trade.entryPrice == 0 or trade.entryPrice == trade.stop): return None return trade
def findsetups(self, fromdt, todt): datastore = datastorefactory.get_datastore() # stocks = datastore.filterStocksByAvgVolume(fromdt, minvolume) stocks = self._getTickers(fromdt, datastore) for stock in stocks: dailydata = list() volume = Volume() avgvolume = SimpleMovingAverage(metric=volume, period=21) dailyatr = ATR(20) atrfromdt = fromdt - timedelta(days=max(self.duration, 40)) # 40 to give the atr time to normalize dailydataiter = iter(datastore.getDailyData(stock, atrfromdt, todt)) dailydataday = None try: while dailydataday == None or dailydataday.date < fromdt: dailydataday = dailydataiter.next() # have to fix it to a real object for the atr dailyatr.handle(dailydataday) dailydata.append(dailydataday) except StopIteration: pass if len(dailydata) > self.duration: dailydata = dailydata[len(dailydata) - self.duration :] # ok, we find the highest high and lowest low first high = 0 low = None for ddhighfinder in dailydata: if high < ddhighfinder.high: high = ddhighfinder.high if low == None or ddhighfinder.low < low: low = ddhighfinder.low # great, now we find how many lower highs are within the mush factor atrmush = 0 if dailyatr.value() != None: atrmush = dailyatr.value() * self.mushinessatr taps = 0 shorttaps = 0 for ddtapfinder in dailydata: delta = high - ddtapfinder.high if delta <= atrmush or delta <= self.mushinessfixed: taps = taps + 1 shortdelta = ddtapfinder.low - low if shortdelta <= atrmush or delta <= self.mushinessfixed: shorttaps = shorttaps + 1 # ok, now we can add the next dd - we go ahead and prep some things for the next loop pass # since we are no longer using them now for dailydataday in dailydataiter: saveatr = dailyatr.value() volume.handle(dailydataday) avgvolume.handle(dailydataday) dailyatr.handle(dailydataday) dailydata.append(dailydataday) dailydata = dailydata[1:] trade = None # as a hack, now we can check our peek ahead and see for free if we # ever broke the high today. If not, we are done if ( self.doLongs and taps >= self.numtaps and dailydataday.high > high and high >= self.minprice and (self.maxprice == None or high <= self.maxprice) and avgvolume.ready() and avgvolume.value() >= self.minavgvolume and ( self.minAPR == None or (dailyatr.ready() and dailyatr.value() / dailydataday.adjustedClose) >= self.minAPR ) ): # ok, we need to scan the day low = None donchlow = None if self.donchianstop != None: low = Low() donchlow = Lowest(low, self.donchianstop) intrafromdt = dailydataday.date intratodt = intrafromdt + timedelta(hours=24) intradaydata = datastore.getIntradayData(stock, self.period, intrafromdt, intratodt) if intradaydata != None and len(intradaydata) > 1: intradaybar = intradaydata[0] intralow = intradaybar.low intrahigh = intradaybar.high taps = 1 for i in range(1, len(intradaydata)): intradaybar = intradaydata[i] if trade == None and ( self.maxintradayrangeatr == None or (saveatr != None and (intrahigh - intralow) < (saveatr * self.maxintradayrangeatr)) ): intralow = min(intralow, intradaybar.low) if ( intradaybar.high <= intrahigh and (intrahigh - intradaybar.high) <= self.mushinessfixed2m ): taps = taps + 1 if intradaybar.high > intrahigh: if ( taps >= self.taps2m and intrahigh >= high and ( self.maxhour == None or intradaybar.date.hour < self.maxhour or (intradaybar.date.hour == self.maxhour and intradaybar.date.minute == 0) ) and (self.minhour == None or intradaybar.date.hour >= self.minhour) ): # trade entry if donchlow != None and donchlow.ready(): stop = donchlow.value() - 0.01 else: stop = intralow - 0.01 entryPrice = min(intradaybar.open, intrahigh + 0.01) if entryPrice > stop: trade = Trade( stock=stock, entry=intradaybar.date, entryPrice=min(intradaybar.open, intrahigh + 0.01), stop=stop, ) if self.target: trade.target = trade.entryPrice + ( self.target * (trade.entryPrice - trade.stop) ) else: # need to recalculate taps off this new high as we had no signal yet intrahigh = intradaybar.high taps = 1 for j in range(0, i - 1): if (intrahigh - intradaydata[j].high) < self.mushinessfixed2m: taps = taps + 1 if trade and trade.exit == None: if intradaybar.low < trade.trailingstop: # taken out trade.exit = intradaybar.date trade.exitPrice = min(intradaybar.open, trade.trailingstop) if trade.target != None and intradaybar.high > trade.target: trade.exit = intradaybar.date trade.exitPrice = max(intradaybar.open, trade.target) if low != None: low.handle(intradaybar) if donchlow != None: donchlow.handle(intradaybar) if trade != None and trade.exit == None: trade.exit = intradaybar.date trade.exitPrice = intradaybar.close if trade: self.tradeManager.addTrade(trade) trade = None trade = None # SHORTS # as a hack, now we can check our peek ahead and see for free if we # ever broke the low today. If not, we are done if ( self.doShorts and shorttaps >= self.numtaps and dailydataday.low < low and low >= self.minprice and avgvolume.ready() and avgvolume.value() >= self.minavgvolume and ( self.minAPR == None or (dailyatr.ready() and dailyatr.value() / dailydataday.adjustedClose) >= self.minAPR ) ): # ok, we need to scan the day high = None donchhigh = None if self.donchianstop != None: high = High() donchhigh = Highest(high, self.donchianstop) intrafromdt = dailydataday.date intratodt = intrafromdt + timedelta(hours=24) intradaydata = datastore.getIntradayData(stock, 300, intrafromdt, intratodt) if intradaydata != None and len(intradaydata) > 1: intradaybar = intradaydata[0] intralow = intradaybar.low intrahigh = intradaybar.high taps = 1 for i in range(1, len(intradaydata)): intradaybar = intradaydata[i] if trade == None and ( self.maxintradayrangeatr == None or (saveatr != None and (intrahigh - intralow) < (saveatr * self.maxintradayrangeatr)) ): intrahigh = max(intrahigh, intradaybar.high) if ( intradaybar.low >= intralow and (intradaybar.low - intralow) <= self.mushinessfixed2m ): taps = taps + 1 if intradaybar.low < intralow: if ( taps >= self.taps2m and intralow <= low and ( self.maxhour == None or intradaybar.date.hour < self.maxhour or (intradaybar.date.hour == self.maxhour and intradaybar.date.minute == 0) ) and (self.minhour == None or intradaybar.date.hour >= self.minhour) ): # trade entry if donchhigh != None and donchhigh.ready(): stop = donchhigh.value() + 0.01 else: stop = intrahigh + 0.01 entryPrice = min(intradaybar.open, intralow - 0.01) if entryPrice < stop: trade = Trade( stock=stock, entry=intradaybar.date, entryPrice=entryPrice, stop=stop ) if self.target: trade.target = trade.entryPrice - ( self.target * (trade.stop - trade.entryPrice) ) else: # need to recalculate taps off this new high as we had no signal yet intralow = intradaybar.low taps = 1 for j in range(0, i - 1): if (intralow - intradaydata[j].low) < self.mushinessfixed2m: taps = taps + 1 if trade and trade.exit == None: if intradaybar.high >= trade.trailingstop: # taken out trade.exit = intradaybar.date trade.exitPrice = max(intradaybar.open, trade.trailingstop) if trade.target != None and intradaybar.low < trade.target: trade.exit = intradaybar.date trade.exitPrice = min(intradaybar.open, trade.target) if high != None: high.handle(intradaybar) if donchhigh != None: donchhigh.handle(intradaybar) if trade != None and trade.exit == None: trade.exit = intradaybar.date trade.exitPrice = intradaybar.close if trade: self.tradeManager.addTrade(trade) trade = None trade = None # redo daily setup for the next day, already loaded in above the intraday loop # ok, we find the highest high first high = 0 low = None for ddhighfinder in dailydata: if high < ddhighfinder.high: high = ddhighfinder.high if low == None or ddhighfinder.low < low: low = ddhighfinder.low # great, now we find how many lower highs are within the mush factor atrmush = 0 if dailyatr.value() != None: atrmush = dailyatr.value() * self.mushinessatr taps = 0 shorttaps = 0 for ddtapfinder in dailydata: delta = high - ddtapfinder.high shortdelta = ddtapfinder.low - low if delta <= atrmush or delta <= self.mushinessfixed: taps = taps + 1 if shortdelta <= atrmush or shortdelta <= self.mushinessfixed: shorttaps = shorttaps + 1 return self.tradeManager.getStats()