def get_trading_date(): from dHydra.core.Functions import get_vendor sina = get_vendor("Sina") sh000300 = sina.get_quote(symbols=["sh000300"]) sh000300_date = sh000300.iloc[0].date return sh000300_date
def init_accounts(self, accounts="easyuncle.json"): """ 初始化acctouns中配置的所有帐号 @return dict { "account_name": { "type": "ctp/easytrader/other", "trader": "生成的实例对象", "timestamp": "对象生成时间", } } """ result = {} if accounts[0] != '/': accounts = os.getcwd() + '/' + accounts accounts_dict = util.read_config(accounts) for account in accounts_dict.keys(): result[account] = dict() result[account]["type"] = accounts_dict[account]["type"] if result[account]["type"] == "ctp": result[account]["trader"] = get_vendor( "CTPTraderApi", account=copy.deepcopy(accounts_dict[account]) console_log=True, # 关闭它的log info_log=False, error_log=False, ) result[account]["timestamp"] = datetime.now() return result
def kline_history( self, symbols=None, end=None, types=["normal", "before", "after"], dbName="stock", collectionName="kline_history", host="localhost", port=27017 ): if symbols is None: # 我选择从新浪获取一份股票列表 sina = get_vendor("Sina") symbolList = sina.get_symbols() elif isinstance(symbols, str): symbolList = symbols.split(',') else: symbolList = list(symbols) for symbol in symbolList: self.kline_to_mongodb( symbol, types=types, end=end, dbName=dbName, collectionName=collectionName, host=host, port=port ) return True
def init_mdapi(self): self.mdapi = get_vendor("CtpMdApi", account=self.__account__, instrument_ids=self.instrument_ids) self.mdapi.OnRtnDepthMarketData = self.OnRtnDepthMarketData self.logger.info("开启CtpMd") self.mdapi.Init()
def kline_to_mongodb( self, symbol, types=["normal", "before", "after"], end=None, dbName='stock', collectionName='kline_history', host='localhost', port=27017 ): if end is None: end = datetime.now().date() else: end = util.string_to_date(end) if self.mongodb is None: self.mongodb = get_vendor("DB").get_mongodb(host=host, port=port) if self.mongodb is False: self.logger.error("没有连接上mongodb") return False for fqType in types: # 先找到mongodb中这条股票的最新记录 latest = self.mongodb[dbName][collectionName].find_one( {"symbol" : symbol, "type": fqType}, sort=[("time", -1)] ) if latest is not None: begin = (latest["time"] + timedelta(days=1) ).strftime("%Y-%m-%d") self.logger.info("symbol = {}, {}\t最近更新记录为 {}".format( symbol, fqType, latest["time"])) if latest["time"].date() >= end: self.logger.info("不需要更新") return True else: begin = None self.logger.info( "symbol = {}, {}\t无最近更新记录".format(symbol, fqType)) self.logger.info("开始更新symbol = {} \t {}".format(symbol, fqType)) kline = False while kline is False: try: kline = self.get_kline(symbol, begin=begin) except Exception as e: self.logger.warning(e) if len(kline) > 0: kline["type"] = fqType kline = kline.iloc[0:len(kline)].to_dict(orient="records") result = self.mongodb[dbName][ collectionName].insert_many(kline) self.logger.info( "更新完毕symbol = {} \t {} \t 插入结果:{}" .format(symbol, fqType, result) ) else: self.logger.info("没有新的记录") return True
def init_mdapi(self): self.mdapi = get_vendor( "CtpMdApi", account=self.__account__, instrument_ids=self.instrument_ids ) self.mdapi.OnRtnDepthMarketData = self.OnRtnDepthMarketData self.logger.info("开启CtpMd") self.mdapi.Init()
def get_trading_date(): from dHydra.core.Functions import get_vendor print("实例化Sina") sina = get_vendor("Sina") print("尝试获取交易日") sh000300 = sina.get_quote(symbols=["sh000300"],timeout=5) sh000300_date = sh000300.iloc[0].date return sh000300_date
def __init__(self, **kwargs): super().__init__(**kwargs) self.redis = get_vendor("DB").get_redis() self.worker_names = list() self.worker_info = dict() self.updating = False if "auto_remove_terminated" in kwargs: self.auto_remove_terminated = kwargs["auto_remove_terminated"] else: self.auto_remove_terminated = -1
def init_ctp(self, account="ctp.json"): self.ctp = get_vendor("CTPTraderApi", account=account) # 初始化持仓情况 self.ctp.OnRspQryInvestorPosition = self.OnRspQryInvestorPosition self.ctp.OnRspQryInvestorPositionDetail =\ self.OnRspQryInvestorPositionDetail self.ctp.OnRspQryTradingAccount = self.OnRspQryTradingAccount self.ctp.OnRspQryOrder = self.OnRspQryOrder self.get_position_detail_ctp() self.get_position_ctp()
def __producer__(self): """ 定时更新redis中各个worker的状态 :return: """ import time # wm: short for "worker_manager" self.wm = get_vendor("WorkerManager", auto_remove_terminated=3600 * 24) while True: time.sleep(1) self.wm.update_workers()
def __producer__(self): self.mdapi = get_vendor("CTPMdApi", account=self.__account__, instrument_ids=self.instrument_ids) self.mdapi.OnRtnDepthMarketData = self.OnRtnDepthMarketData self.logger.info("开启CTPMd") self.mdapi.Init() try: while 1: time.sleep(10) except Exception as e: self.logger.warning(e)
def __producer__(self): """ 定时更新redis中各个worker的状态 :return: """ import time # wm: short for "worker_manager" self.wm = get_vendor( "WorkerManager", auto_remove_terminated=3600*24 ) while True: time.sleep(1) self.wm.update_workers()
def init_mdapi(self): if self.mdapi is not None: try: self.mdapi.Release() except Exception as e: self.logger.error("MdApi Release Failed: {}".format(e)) instrument_ids = self.get_all_instruments() self.logger.info("成功获取Instruments: {}".format(len(instrument_ids))) self.mdapi = get_vendor("CtpMdApi", account=self.__account__, instrument_ids=instrument_ids) self.mdapi.OnRtnDepthMarketData = self.OnRtnDepthMarketData self.logger.info("开启CtpMd") self.mdapi.Init()
def get_all_instruments(self): trader = get_vendor("CtpTraderApi", account=self.__account__) result = list() instruments = list() while instruments == list(): time.sleep(3) try: instruments = list(trader.instruments.InstrumentID) except Exception as e: trader.prepare_instruments_info() print("获取instruments失败,1秒后重试") trader.Release() for item in instruments: if ("&" in item) or (" " in item): continue else: result.append(item) # 确保我们配置文件中规定的instruments必须在列表中 result = list(set(result) | set(self.instrument_ids)) return result
def hail(what=None): import threading import time import dHydra.web try: if what: if what[0] != "dHydra": print("Hail What??") exit(0) else: print("Welcome to dHydra! Following is the " "Architecture of dHydra") doc = \ """ "hail dHydra" | | ┌────┴─────┐ ┌────────────┐ | dHydra | | Tornado | | Server ├─────────┤ Web Server ├──http://127.0.0.1:5000────┐ └────┬─────┘ └──────┬─────┘ | | | 默认两种url映射规则,例如: ┌────┴────┐ | /api/Worker/BackTest/method/ | Redis ├─────────────────┘ /Worker/BackTest/index └──┬──────┘ 详情参考文档 | | ├─────Publish────┬─────Subscribe──────┬─────Publish───┐────┤ | | | | ┌──────┴──┐ ┌────┴─────┐ ┌────┴─────┐ ┌────┴─────┐ | (Worker)| | (Worker) | | (Worker) | | (Worker) | | CTP | | Strategy | | BackTest | | Sina L2 | └─────────┘ └──────────┘ └──────────┘ └──────────┘ """ print(doc) # open a thread for the Worker of Monitor start_worker("Monitor") logger.info("Monitor has started") # open a thread for webserver thread_tornado = threading.Thread( target=dHydra.web.start_server) thread_tornado.setDaemon(True) thread_tornado.start() logger.info("Tornado webserver has started") # 绑定退出信号 bind_quit_signals() redis_conn = get_vendor("DB").get_redis() command_listener = redis_conn.pubsub() channel_name = "dHydra.Command" command_listener.subscribe([channel_name]) while True: msg_command = command_listener.get_message() if msg_command: if msg_command["type"] == "message": __command_handler__(msg_command["data"]) else: time.sleep(0.1) else: print("Hail What?") except Exception as e: print("Hail What?") traceback.print_exc()
from dHydra.console import logger, get_worker_class from dHydra.core.Functions import get_vendor import click import signal import multiprocessing import time import os import json import traceback import sys __redis__ = get_vendor("DB").get_redis() __current_process__ = multiprocessing.current_process() worker_dict = dict() def __on_termination__(sig, frame): logger.info("The dHydra Server is about to terminate, pid:{}".format( os.getpid())) sys.exit(0) def bind_quit_signals(): shutdown_signals = [ signal.SIGQUIT, # quit 信号 signal.SIGINT, # 键盘信号 signal.SIGHUP, # nohup 命令 signal.SIGTERM, # kill 命令 ] for s in shutdown_signals:
from dHydra.console import logger, get_worker_class from dHydra.core.Functions import get_vendor import click import multiprocessing import time import os import json import traceback import sys import pickle __redis__ = get_vendor("DB").get_redis() __current_process__ = multiprocessing.current_process() worker_dict = dict() def __on_termination__(sig, frame): logger.info("The dHydra Server is about to terminate, pid:{}" .format(os.getpid()) ) sys.exit(0) def bind_quit_signals(): import signal shutdown_signals = [ "SIGQUIT", # quit 信号 "SIGINT", # 键盘信号 "SIGHUP", # nohup 命令 "SIGTERM", # kill 命令
def hail(what=None): import threading import time import dHydra.web try: if what: if what[0] != "dHydra": print("Hail What??") exit(0) else: print( "Welcome to dHydra! Following is the " "Architecture of dHydra" ) doc = \ """ "hail dHydra" | | ┌────┴─────┐ ┌────────────┐ | dHydra | | Tornado | | Server ├─────────┤ Web Server ├──http://127.0.0.1:5000────┐ └────┬─────┘ └──────┬─────┘ | | | 默认两种url映射规则,例如: ┌────┴────┐ | /api/Worker/BackTest/method/ | Redis ├─────────────────┘ /Worker/BackTest/index └──┬──────┘ 详情参考文档 | | ├─────Publish────┬─────Subscribe──────┬─────Publish───┐────┤ | | | | ┌──────┴──┐ ┌────┴─────┐ ┌────┴─────┐ ┌────┴─────┐ | (Worker)| | (Worker) | | (Worker) | | (Worker) | | CTP | | Strategy | | BackTest | | Sina L2 | └─────────┘ └──────────┘ └──────────┘ └──────────┘ """ print(doc) # open a thread for the Worker of Monitor start_worker(worker_name="Monitor",nickname="Monitor") logger.info("Monitor has started") # 开启Tornado if len(what) == 1: # 没指定http端口,不开启Tornado pass else: port = int(what[1]) start_worker(worker_name="Web", nickname="Tornado") # 绑定退出信号 bind_quit_signals() redis_conn = get_vendor("DB").get_redis() command_listener = redis_conn.pubsub() channel_name = "dHydra.Command" command_listener.subscribe([channel_name]) while True: msg_command = command_listener.get_message() if msg_command: if msg_command["type"] == "message": __command_handler__(msg_command["data"]) else: time.sleep(0.1) else: print("Hail What?") except Exception as e: traceback.print_exc() logger.error("{}".format(e)) print("Hail What?")