def testMA(self): barDs = self.__loadBarDS() self.assertAmountsAreEqual( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_Type.SMA)[1], 93.16) # Original value 93.15 self.assertAmountsAreEqual(indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_Type.SMA)[2], 94.59, precision=1) self.assertAmountsAreEqual(indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_Type.SMA)[3], 94.73, precision=1) self.assertAmountsAreEqual( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_Type.SMA)[-1], 108.31)
def testMA(self): barDs = self.__loadBarDS() self.assertTrue( compare( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_SMA)[1], 93.16)) # Original value 93.15 self.assertTrue( compare( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_SMA)[2], 94.59)) self.assertTrue( compare( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_SMA)[3], 94.73)) self.assertTrue( compare( indicator.MA(barDs.getCloseDataSeries(), 252, 2, talib.MA_SMA)[-1], 108.31))
def onBars(self, bars): # If a position was not opened, check if we should enter a long position. closeDs = self.getFeed().getDataSeries(instrument).getCloseDataSeries() self.__ma1 = indicator.MA(closeDs, 40, self.__malength1) self.__ma2 = indicator.MA(closeDs, 40, self.__malength2) if self.__ma2[-1] is None: return if self.__position is not None: if not self.__position.exitActive() and cross.cross_below( self.__ma1, self.__ma2) > 0: self.__position.exitMarket() #self.info("sell %s" % (bars.getDateTime())) if self.__position is None: if cross.cross_above(self.__ma1, self.__ma2) > 0: shares = int(self.getBroker().getEquity() * 0.2 / bars[self.__instrument].getPrice()) self.__position = self.enterLong(self.__instrument, shares) print bars[self.__instrument].getDateTime(), bars[ self.__instrument].getPrice()
def select_instruments(self, instruments, period): selected_instruments = [] for instrument in instruments: hist_h = self.history(instrument, 'high', period) hist_l = self.history(instrument, 'low', period) hist_c = self.history(instrument, 'close', period) hist_o = self.history(instrument, 'open', period) hist_v = self.history(instrument, 'volume', period) ''' { 30个交易日内最低点到最高点涨幅超过50%,30日内最少有两个涨停板,上市日期超过60日 } HP:=HHV(HIGH,N);{N日内最高价} LP:=LLV(LOW,N);{30日内最低价} HD:=HHVBARS(HIGH,N);{出现最高价离现在的时间} LD:=LLVBARS(LOW,N);{出现最低价离现在的时间} OP1 := ((HP-LP)/LP > M/100);{N内涨幅超过M%} OP2 := (COUNT(CLOSE/REF(CLOSE,1)>1.097,N)>=2);{N日内出现2个以上涨停板} OP3 := LD>HD;{30日内最低点出现在最高点前面} OP4 := (BARSCOUNT(C)+1)>60;{排除最近60天上市的次新股} OP5 := DYNAINFO(4)>0; 条件选股:OP1 AND OP2 AND OP3 AND OP4 AND OP5; ''' hp_arr = indicator.MAX(hist_h, period) hp = hp_arr[-1] lp_arr = indicator.MIN(hist_l, period) lp = lp_arr[-1] hd_arr = indicator.MAXINDEX(hist_h, period) ld_arr = indicator.MININDEX(hist_l, period) hd = len(hist_h) - hd_arr[-1] ld = len(hist_l) - ld_arr[-1] if (hp - lp) / lp < 0.4: continue if self.count_limit_up(hist_c) < 2: continue if hd > ld: continue last_open = hist_o[-1] last_close = hist_c[-1] pre_last_close = hist_c[-2] raise_percent = (pre_last_close - last_close) / pre_last_close raise_percent2 = (last_close - last_open) / last_open if raise_percent > 0.03 or raise_percent < -0.03: continue if raise_percent2 > 0.03 or raise_percent2 < -0.02: continue ''' OP1 := ABS((LOW-TROUGH(2,15,1))/TROUGH(2,15,1))<=0.03; OP11 := ABS((LOW-TROUGH(2,15,2))/TROUGH(2,15,2))<=0.03 AND (TROUGH(2,15,2) <= TROUGH(2,15,1)); OP111 := ABS((LOW-TROUGH(2,15,3))/TROUGH(2,15,3))<=0.03 AND (TROUGH(2,15,3) <= TROUGH(2,15,1)); OP2 := V < MA(V,30) OR V < HHV(V,30)/3; OP3 := TROUGHBARS(2,15,1) > 3; 条件选股:(OP1 OR OP11 OR OP111) AND OP2 AND OP3; ''' ma_volume = indicator.MA(hist_v, period) max_volume = indicator.MAX(hist_v, period) if hist_v[-1] >= ma_volume[-1] and hist_v[-1] >= max_volume[-1] / 3: continue trough, troughbars = self.get_trough(instrument) if len(trough) == 0 or len(troughbars) == 0: continue if troughbars[0] <= 3: continue last_low = hist_l[-1] op1 = abs((last_low - trough[0]) / trough[0]) <= 0.03 op2 = len(trough) >= 2 and abs( (last_low - trough[1]) / trough[1]) <= 0.03 op3 = len(trough) >= 3 and abs( (last_low - trough[2]) / trough[2]) <= 0.03 if op1: self.__stop_loss_price[instrument] = trough[0] else: continue ''' elif op2: self.__stop_loss_price[instrument] = trough[1] elif op3: self.__stop_loss_price[instrument] = trough[2] ''' selected_instruments.append(instrument) return selected_instruments