Example #1
0
    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
Example #2
0
 def checkTrade(self):
     if self.lastrange.ready() and self.lastminrange.ready() \
         and self.lastrange.value() < self.lastminrange.value():
         
         # we have an N-bar, take a break long or short and hold to close
         if self.lastdd.high > self.lasthigh.value():
             entry = max(self.lastdd.open, self.lasthigh.value() + 0.01)
             stop = self.lastlow.value() - 0.01
             if entry < 0:
                 entry = 0
             if stop < 0:
                 stop = 0
             if entry > stop and entry > 0:
                 trade = Trade(self.lastdd.stock, self.lastdd.date, entry, stop)
                 trade.exit = self.lastdd.date
                 if self.lastdd.low <= stop:
                     trade.exitPrice = stop
                 else:
                     trade.exitPrice = self.lastdd.close
                 return trade
         elif self.lastdd.low < self.lastlow.value():
             entry = min(self.lastdd.open,self.lastlow.value() - 0.01)
             stop = self.lasthigh.value() + 0.01
             if entry < 0:
                 entry = 0
             if stop < 0:
                 stop = 0
             if entry < stop and entry > 0:
                 trade = Trade(self.lastdd.stock, self.lastdd.date, entry, stop)
                 trade.exit = self.lastdd.date
                 if self.lastdd.high >= stop:
                     trade.exitPrice = stop
                 else:
                     trade.exitPrice = self.lastdd.close
                 return trade
     return None
Example #3
0
	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()
	def findsetups(self, fromdt, todt):
		stocks = self._getTickers(fromdt, datastore)
		for stock in stocks:
			close = Close()
			sma = None
			if self.dailysmatrendfilter != None:
				sma = SimpleMovingAverage(close, period=self.dailysmatrendfilter)
			# padded extra to make sure 50 day sma has enough trading days to work with before our window
			dailydata = datastore.getDailyData(stock, fromdt - timedelta(days=100), todt)
			
			prevpd = None
			for pd in dailydata:
				if prevpd != None and pd.date >= fromdt:
					trade =  None
					if pd.open == prevpd.close or prevpd.close == 0:
						gapsize=0
					else:
						gapsize = (prevpd.close - pd.open)/prevpd.close
					if gapsize >= self.mingap and (sma == None or (sma.ready() and pd.open < sma.value())) and pd.open >= self.minprice:
						intrafromdt = pd.date
						intratodt = pd.date + timedelta(hours=24)
						intradaydata = iter(datastore.getIntradayData(stock, 300, intrafromdt, intratodt))
						try:
							intradaypd = intradaydata.next()
							entry = None
							lod = intradaypd.low
							prevpd = intradaypd
							high = High()
							highesthigh = Highest(metric=high, period=self.donchianstop)
							high.handle(intradaypd)
							highesthigh.handle(intradaypd)
							for intradaypd in intradaydata:
								if trade == None \
										and highesthigh.ready() \
										and intradaypd.date.hour >= self.minhour \
										and (intradaypd.date.hour < self.maxhour \
											or (intradaypd.date.hour == self.maxhour and intradaypd.date.minute==0)) \
										and intradaypd.low < lod:
									entry = lod - 0.01
									trade = Trade(stock, intradaypd.date, min(entry, intradaypd.open), highesthigh.value()+0.01)
									if self.target != None:
										trade.target = trade.entryPrice + (self.target * (trade.entryPrice-trade.stop))
								if trade != None and trade.exit == None:
									if intradaypd.high >= trade.trailingstop:
										trade.exit = intradaypd.date
										trade.exitPrice = max(trade.stop, intradaypd.open)
									if trade.exit == None and trade.target != None and intradaypd.low <= trade.target:
										trade.exit = intradaypd.date
										trade.exitPrice = min(intradaypd.open, trade.target)
								if intradaypd.low < lod:
									lod = intradaypd.low
								high.handle(intradaypd)
								highesthigh.handle(intradaypd)
						except StopIteration:
							pass
						# eod, close it if it is still open
						if trade != None and trade.exit == None:
							trade.exit = intradaypd.date
							trade.exitPrice = intradaypd.close
						if trade != None:
							self.tradeManager.addTrade(trade)

				# now we can update the indicators - since we finished trading the day
				close.handle(pd)
				if sma != None:
					sma.handle(pd)

				prevpd = pd

		return self.tradeManager.getStats()
Example #5
0
	def findsetups(self, fromdt, todt):
		numstopouts = 0
		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
			dailydata = datastore.getDailyData(stock, fromdt - timedelta(days=(self.slowma*2)), todt)
			close = Close()
			fastma = SimpleMovingAverage(period=self.fastma)
			slowma = SimpleMovingAverage(period=self.slowma)
			lastfastma = HistoricMetric(metric=fastma, period=1)
			lastslowma = HistoricMetric(metric=slowma, period=1)
			atr = ATR(period=14)

			lastdd = None
			trade = None

			for pd in dailydata:
				close.handle(pd)
				fastma.handle(pd)
				slowma.handle(pd)
				lastfastma.handle(pd)
				lastslowma.handle(pd)
				atr.handle(pd)

				# check for long stopout
				if trade != None and pd.low < trade.trailingstop:
					trade.exit = pd.date
					trade.exitPrice = min(pd.open, trade.trailingstop)
					self.tradeManager.addTrade(trade)
					numstopouts = numstopouts + 1
					trade = None

				# check for long exit
				if trade != None and fastma.value() < slowma.value():
					trade.exit = pd.date
					trade.exitPrice = pd.close
					self.tradeManager.addTrade(trade)
					trade = None

				if fastma.ready() and slowma.ready() and lastfastma.ready() and lastslowma.ready() and atr.ready():
					pass

				# check for new long
				if trade == None and fastma.ready() and slowma.ready() \
						and lastfastma.ready() and lastslowma.ready() \
						and (self.atrStop == None or atr.ready()) \
						and pd.date >= fromdt \
						and fastma.value() > slowma.value() \
						and lastfastma.value() <= lastslowma.value() \
						and pd.close >= self.minprice:
					stop = 0
					if self.atrStop != None:
						stop = max(0,pd.close - (float(self.atrStop) * atr.value()))
					if self.percentStop != None:
						stop = max(0, pd.close * (1.0 - self.percentStop))
					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
		print "num stopouts was %d" % numstopouts
		return self.tradeManager.getStats()