init_time = item['init_time'] #设置这个交易对的Kline初始时间 self.db_conn.hset(symbol,'init_time',init_time) #如果当前Kline时间不存在就设置这个交易对的当前Kline时间为初始时间 for cur_time_key in cur_times: if not self.db_conn.hexists(symbol,cur_time_key) or overwrite: time_t = time.strptime(init_time, "%Y-%m-%d %H:%M:%S") cur_time = int(time.mktime(time_t)) self.db_conn.hset(symbol,cur_time_key, cur_time) #添加一个开关方便动态进行数据获取 if overwrite: self.db_conn.hset(symbol,'enabled',1) elif not self.db_conn.hexists(symbol,'enabled'): self.db_conn.hset(symbol,'enabled',0) except Exception as e: logging.error("[init] 2 " + str(e)) if __name__ == "__main__": overwrite = False if len(sys.argv) > 1: if sys.argv[1] == 'overwrite': overwrite = True kline_common.init_logging('kline_init', True) main = Main() main.start(overwrite)
logging.info('[runtime] resub : ' + str(self.count)) symbols = self._get_symbols() period = '1min' for symbol in symbols: request = """{"sub": "market.%s.kline.%s","id": "%s"}""" \ % (symbol, period, symbol + '_' + period) self.data_conn.send(request) def _get_symbols(self): symbols = self.db_conn.lrange('symbols', 0, -1) symbols_ret = [] for s in symbols: symbols_ret.append(s.decode('utf-8')) return symbols_ret def on_message(self, msg): self.db_conn.publish('tick_data', msg) if __name__ == "__main__": kline_common.init_logging('kline_runtime') main = Main() main.start() try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: logging.error('[runtime] exit .')
# -*- coding: utf-8 -*- # author: shubo import time import logging import kline_common # 交易模块负责进行发送交易指令 class Main: def __init__(self): self.db_conn = kline_common.DBConnection() def start(self): self.db_conn.start(True, False) pubsub = self.db_conn.subscribe('trade_cmd') for data in pubsub.listen(): if data['channel'] == b'trade_cmd': logging.info(data['data']) if __name__ == "__main__": kline_common.init_logging('kline_trade') main = Main() main.start()
self.producer = KlineTaskProducer(self.db_conn,self.data_conn,self.is_init) self.data_conn.on_message = self.producer.on_message self.producer.start() else: self.data_conn.on_message = self.producer.on_message self.producer.release_all() except Exception as e: msg = traceback.format_exc() # 方式1 logging.error(msg) if __name__ == "__main__": init_run = False if len(sys.argv) > 1: if sys.argv[1] == 'init': init_run = True suffix = 'init' if not init_run: suffix = 'runtime' kline_common.init_logging('kline_storager_' + suffix, init_run)#init_run main = Main(init_run) main.start() try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: logging.error('[runtime] exit .') main.stop()
def _process_data(self, data): #print(data) splits = data['ch'].split('.') coin = splits[1] close = data['tick']['close'] self.day_open_cache.update(coin, close) #print(self.day_open_cache.get_up_ratio()) self.all_cache.update(coin,'1min') self.all_cache.update(coin,'60min') self.all_cache.update(coin,'1day') high = self.all_cache.get_high_in_past(coin,'1min') high1 = self.all_cache.get_high_in_past(coin,'60min') if high1 > high: high = high1 if close < high * 0.75: logging.warning('[buy] in up -->' + coin + ':' + str(close) ) if self.day_open_cache.get_up_ratio() > 0.8: day_open = self.day_open_cache.get_day_open(coin) if close > day_open*1.015 and close < high * 0.8: logging.warning('[buy] in down -->' + coin + ':' + str(close) ) if __name__ == "__main__": kline_common.init_logging('kline_analysis') main = Main() main.start()