def strategy(self): needEmail = False #非交易时间,直接返回 if not stockService.isTradeTime(): return needEmail #获取持有的股票 try: holdStocks = redisService.smembers(Config.KEY_HOLD_STOCK) for stockNo in holdStocks: #获取当前股票的实时分笔 df = ts.get_realtime_quotes(stockNo) column_open = df['open'] open = float(column_open[0]) column_price = df['price'] price = float(column_price[0]) percent = abs(open - price) / open #如果波动幅度超过5%,则需要给出卖出提示 #涨幅超过5%的情况 if price > open and percent >= self.rate: needEmail = True redisService.sadd(self.buyKey, stockNo) if price < open and percent >= self.rate: needEmail = True redisService.sadd(self.sellKey, stockNo) except: print(self.strategyName, "策略出现异常:", sys.exc_info()[0]) finally: #让出线程 time.sleep(1) return needEmail return needEmail
def initHoldStocksToRedis(self): filePath = os.path.join(os.getcwd(), Config.HOLD_STOCKS_FILE_NAME) with open(filePath) as somefile: lines = somefile.readlines() for line in lines: stockNo = line.strip() if len(stockNo) > 0: try: redisService.sadd(Config.KEY_HOLD_STOCK, stockNo) except: pass
def strategy(self, stockNo): if stockNo is not None: try: nowDate = datetime.datetime.now() endDateStr = nowDate.strftime(Config.FORMAT_STR) startDateStr = ( nowDate - datetime.timedelta(days=self.dateDelta)).strftime( Config.FORMAT_STR) df = ts.get_hist_data(stockNo, start=startDateStr, end=endDateStr) if (df is not None) and (len(df.index) >= self.dateDelta): ma20 = df[u'ma20'] close = df[u'close'] underline = True for index in range(1, self.dateDelta - 1): if close[index] > ma20[index]: underline = False break if underline and close[0] > ma20[0]: #存入买入股票编码 redisService.sadd(self.buyKey, stockNo) return Signal.Signal(stockNo=stockNo, buy=True, dateStr=endDateStr) upline = True for index in range(1, self.dateDelta - 1): if close[index] < ma20[index]: upline = False break if upline and close[0] < ma20[0]: #存入买入股票编码 redisService.sadd(self.sellKey, stockNo) return Signal.Signal(stockNo=stockNo, sell=True, dateStr=dateStr) #如果前3天的收盘价在20天均线下方,而当前收盘价在均线上方,则发出买入信号 # if close[1] < ma20[1] and close[2] < ma20[2] and close[3] < ma20[3] and close[0] > ma20[0] : #存入买入股票编码 # redisService.sadd(self.buyKey,stockNo) # return Signal.Signal(stockNo=stockNo,buy=True,dateStr=dateStr) #如果前3天的收盘价在20天均线上方,而当前收盘价在均线上方,则发出买入信号 # if close[1] > ma20[1] and close[2] > ma20[2] and close[3] > ma20[3] and close[0] < ma20[0] : # redisService.sadd(self.sellKey,stockNo) # return Signal.Signal(stockNo=stockNo,sell=True,dateStr=dateStr) return None except: print(self.strategyName, "策略出现异常:", sys.exc_info()[0]) finally: #让出线程 time.sleep(1) return None return None
def strategy(self): try: calc_year, cal_quarter = dateTimeUtils.getCalYearAndQuarter() df = ts.fund_holdings(calc_year, cal_quarter) for index, row in df.iterrows(): code = row.code #同上期相比,基金在增仓,且增幅大于50% nlast = float(row.nlast) #基金持股流通占比大于30% ratio = float(row.ratio) if nlast > self.compare_last_increment and ratio > self.fund_hold_rate: redisService.sadd(self.buyKey, code) return None except: print(self.strategyName, "策略出现异常:", sys.exc_info()[0]) finally: #让出线程 time.sleep(1) return None return None
def strategy(self,stockNo): if stockNo is not None: try: nowDate=datetime.datetime.now() endDateStr=nowDate.strftime(Config.FORMAT_STR) startDateStr=(nowDate-datetime.timedelta(days=self.dateDelta)).strftime(Config.FORMAT_STR) df = ts.get_hist_data(stockNo,start=startDateStr,end=endDateStr) #最少需要28行数据 if df and (len(df.index) >= 28) : front_EMA12=cal_EMA(df.ix[1:-1,:],12,12) front_EMA26=cal_EMA(df.ix[1:-1,:],26,26) #前日差离率 front_DIF=EMA12-EMA26 EMA12=cal_EMA(df,12,12) EMA26=cal_EMA(df,26,26) #今日差离率 DIF=EMA12-EMA26 DEA=cal_DEA(df) BAR=2*(DIF-DEA) #根据离差率判断是否属于上升趋势 if front_DIF < DIF and DIF >= DEA : #如果离差率上穿DEA则为金叉,发出买入信号 #存入买入股票编码 redisService.sadd(self.buyKey,stockNo) #如果离差率下破DEA则为死叉,发出卖出信号 if front_DIF > DIF and DIF <= DEA : #如果当前股票在持股里面,则发出卖出信号 holdStocks=redisService.smembers(Config.KEY_HOLD_STOCK) for holdNo in holdStocks: if holdNo == stockNo : redisService.sadd(self.sellKey,stockNo) return None except: print(self.strategyName,"策略出现异常:", sys.exc_info()[0]) finally: #让出线程 time.sleep(1) return None return None
def testRedis(): print('str') redisService.set('string','str') print(redisService.keys('s*')) print(redisService.get('string')) print('list') redisService.rpush('list',1) redisService.rpush('list',2) redisService.rpush('list',3) redisService.rpush('list',3) print(redisService.lrange('list',0,-1)) print('set') redisService.sadd('set',1) redisService.sadd('set',2) redisService.sadd('set',3) redisService.sadd('set',3) print(redisService.smembers('set'))