Пример #1
0
 def getEmailContent(self):
     content = '策略名称:' + self.strategyName
     content = content + '\r\n'
     content = content + '买入:\r\n'
     content = content + ','.join(redisService.smembers(self.buyKey))
     content = content + '\r\n'
     content = content + '卖出:\r\n'
     content = content + ','.join(redisService.smembers(self.sellKey))
     return content
Пример #2
0
 def getEmailContent(self):
     content = None
     buyList = redisService.smembers(self.buyKey)
     sellList = redisService.smembers(self.sellKey)
     if len(buyList) > 0 or len(sellList) > 0:
         content = '策略名称:' + self.strategyName
         content = content + '<br/>'
         content = content + '买入(' + str(len(buyList)) + '支):<br/>'
         content = content + ','.join(buyList)
         content = content + '\r\n'
         content = content + '卖出(' + str(len(sellList)) + '支):<br/>'
         content = content + ','.join(sellList)
     return content
Пример #3
0
 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
Пример #4
0
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'))
Пример #5
0
	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
Пример #6
0
    def isTradeTime(self):
        struct_time = time.localtime()
        tm_wday = struct_time[6]
        tm_hour = struct_time[3]
        tm_min = struct_time[4]
        #周一到周五
        if 0 <= tm_wday and tm_wday <= 4:
            if 9 < tm_hour or (9 == tm_hour and 30 <= tm_min):
                return True
            if tm_hour >= 13 and tm_hour <= 15:
                return True
        return False

    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


stockService = StockService()
if __name__ == '__main__':
    stockService.initHoldStocksToRedis()
    print(redisService.smembers(Config.KEY_HOLD_STOCK))