def main(): from mantis.sg.fisher.stutils import get_trade_database_name # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host=mongodb_host, dbname=get_trade_database_name()) # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('TDX')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 market = TDX_StockMarket().init(**conn_url.dict()) # 添加行情记录器 # market.setupRecorder( strecoder.MarketMongoDBRecorder(db_prefix='TDX_'+current_date_string(), host='192.168.1.252')) # 安裝行情記錄器 # 装备行情对象到股票产品 stbase.stocks.setupMarket(market) # 初始化交易对象 trader = TDX_StockTrader().init(**conn_url.dict()) stbase.stocks.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id, stbase.stocks).init() #设置策略日志对象 # strategy.getLogger().addAppender(strecoder.StragetyLoggerMongoDBAppender(db_prefix='StrategyLogs_{}_{}'.format(strategy_id,current_date_string() ),host=mongodb_host)) strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender( db_prefix=get_trade_database_name(), host=mongodb_host)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 stbase.controller.run()
def main(): strategy_id ='AJ_Test1' # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host='192.168.1.252') # 策略控制器 stbase.controller.init('z:/ams/tdx') # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('TDX')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 market = TDX_StockMarket().init(**conn_url.dict()) # 添加行情记录器 market.setupRecorder( strecoder.MarketMongoDBRecorder(db_prefix='TDX_'+current_date_string(), host='192.168.1.252')) # 安裝行情記錄器 # 装备行情对象到股票产品 stbase.stocks.setupMarket(market) # 初始化交易对象 trader = TDX_StockTrader().init(**conn_url.dict()) stbase.stocks.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id,stbase.stocks).init() #设置策略日志对象 strategy.getLogger().addAppender(strecoder.StragetyLoggerMongoDBAppender(db_prefix='{}_'.format(strategy_id)+current_date_string()+"_",host='192.168.1.252')) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 stbase.controller.run()
def main(): # from mantis.sg.fisher.stutils import get_trade_database_name # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host= mongodb_host,dbname= dbname) # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('CTP')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 market = CtpMarket().init(**conn_url.dict()) # 装备行情对象到股票产品 stbase.futures.setupMarket(market) # 初始化交易对象 trader = CtpTrader().init(**conn_url.dict()) stbase.futures.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id,stbase.futures).init() #设置策略日志对象 strategy.getLogger().addAppender(strecoder.StragetyLoggerMongoDBAppender(db_prefix= dbname,host=mongodb_host)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 stbase.controller.run()
def main(): # from mantis.sg.fisher.stutils import get_trade_database_name # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host= mongodb_host,port= mongodb_port,dbname= dbname) # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('CTP')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 # conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 params = dict(db_conn= quotas_db_conn ,cycle='d',symbol=SYMBOL, # start='2015-6-18 9:0' , end='2019-6-19 15:0',freq=0,wait=2) start=SAMPLE_DATE_RANGE[0] , end=SAMPLE_DATE_RANGE[1],freq=0,wait=2) # params.update( conn_url.dict() ) market = CtpMarketBarBackTest().init(**params) # 配置历史行情记录加载器 # 装备行情对象到股票产品 stbase.controller.futures.setupMarket(market) # 初始化交易对象 # trader = CtpTrader().init(**conn_url.dict()) # stbase.controller.futures.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id,stbase.controller.futures).init() #设置策略日志对象 strategy.getLogger().addAppender(strecoder.StragetyLoggerMongoDBAppender(db_prefix= dbname,host=mongodb_host,port=mongodb_port)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 stbase.controller.run().waitForShutdown() # 开始运行 ,加载k线数据
def backtest(): """历史行情回测""" stbase.controller.init('z:/ams/backtest') stbase.controller.getLogger().addAppender( stbase.FileLogAppender('BackTest_1')) # generator = stgenerator.MarketGeneratorFileTDX().init(src_file= os.path.join(stbase.controller.getDataPath(),'tdx.txt')) # generator = stgenerator.MarketGeneratorTushare().init(code='600000',start='2018-10-04',end='2019-02-01',ktype='5') strategy = ASM_Strategy('ASM_Strategy_Test1', stbase.stocks).init() # 设置策略日志 strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender(db_prefix='AMS_Stocks', host='192.168.1.252')) stbase.controller.addStrategy(strategy) # 创建模拟行情发生器对象 generator = stgenerator.MarketGeneratorMongoDBBar().init( code=CODE, # ktype='5m', # play_start='20190311 09:20:00', # play_end='20190311 15:10:00', host='192.168.1.252', db='AMS_Stocks_Ticks', async_send=False # db='AMS_Stocks_Bars', ) generator.onEnd = stbase.controller.stop # 行情播报结束 stbase.stocks.setup(stsim.BackStockMarket().setupGenerator(generator), stsim.BackStockTrader()) stbase.controller.run() generator.open()
def main(): if len(sys.argv) <= 1: print 'Error: Need More Args!' return False # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host=mongodb_host, port=mongodb_port, dbname=dbname) # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('CTP')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 # conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 # params.update( conn_url.dict() ) # market = CtpMarketBarBackTest().init(**params) # 配置历史行情记录加载器 market = CtpMarketBarBackTest().init() # 配置历史行情记录加载器 # 装备行情对象到股票产品 # stbase.controller.futures.setupMarket(market) # 初始化交易对象 # trader = CtpTrader().init(**conn_url.dict()) # stbase.controller.futures.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id, stbase.controller.futures).init() cfgs = init_env(strategy) params = dict(db_conn=quotas_db_conn, cycle='d', symbol=cfgs['SYMBOL'], dataset=cfgs['DATASET'], start=cfgs['SAMPLE_DATE_RANGE'][0], end=cfgs['SAMPLE_DATE_RANGE'][1], freq=0, wait=2) strategy.bar_list = market.loadHistBars(**params) #设置策略日志对象 strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender(db_prefix=dbname, host=mongodb_host, port=mongodb_port)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 # stbase.controller.run().waitForShutdown() # 开始运行 ,加载k线数据 stbase.controller.run()
def init(context): stbase.controller.init('z:/ams/fisher') stbase.controller.getLogger().addAppender( stbase.FileLogAppender('ASM_Strategy_Test2')) stbase.stocks.setup(ams.ASM_StockMarket().init(context), ams.ASM_StockTrader().init(context)) # stbase.stocks.market.setupRecorder(stbase.MarketFileRecorder(stbase.controller.data_path)) # 安裝行情記錄器 # stbase.stocks.market.setupRecorder(strecoder.MarketMongoDBRecorder(db_prefix='AMS_Stocks', host='192.168.1.252')) # 安裝行情記錄器 strategy = ASM_Strategy('ASM_Strategy_Test1', stbase.stocks).init() #设置策略日志 strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender(db_prefix='AMS_Stocks', host='192.168.1.252')) stbase.controller.addStrategy(strategy) stbase.controller.run()
def init(context): from mantis.sg.fisher.stutils import get_trade_database_name # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() dbname = get_trade_database_name('AMS') paramctrl.open(host=mongodb_host, dbname=dbname) # 策略控制器 stbase.controller.init(data_path) stbase.controller.getLogger().addAppender( stbase.FileLogAppender(strategy_id)) stbase.controller.setParamController(paramctrl) stbase.stocks.setup(ams.ASM_StockMarket().init(context), ams.ASM_StockTrader().init(context)) # stbase.stocks.market.setupRecorder(stbase.MarketFileRecorder(stbase.controller.data_path)) # 安裝行情記錄器 # stbase.stocks.market.setupRecorder(strecoder.MarketMongoDBRecorder(db_prefix='AMS_Stocks', host='192.168.1.252')) # 安裝行情記錄器 strategy = ASM_Strategy(strategy_id, stbase.stocks).init() #设置策略日志 strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender(db_prefix=dbname, host=mongodb_host)) stbase.controller.addStrategy(strategy) stbase.controller.run()
def main(): if len(sys.argv) <= 1: print 'Error: Need More Args!' return False strategy_id = sys.argv[1] # if not file_lock(os.path.join('/tmp',strategy_id)): # print 'Error: Process Lock File Failed. Maybe Same Process Is Running. ' # return False # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host= mongodb_host,port= mongodb_port,dbname= dbname) init_database(paramctrl.conn) # 初始化数据库model # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender(stbase.FileLogAppender('CTP')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 if not param: print 'Error: Strategy {} Not Found.'.format(strategy_id) return False if param.up_time: distance = datetime.datetime.now() - param.up_time if distance.seconds < 5: print 'Error: Same Strategy Is Running.. ' return conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 conn_url.td_api_url = config.TRADE_API_URL conn_url.md_broker = config.MD_BROKER conn_url.pubsub_event_name = config.TRADE_PUBSUB_EVENT_NAME # 交易服务事件通知通道名称 conn_url.save() # conn_url 存放交易和行情的所有运行配置信息 # 初始化行情对象 params = dict(db_conn=paramctrl.conn,md_broker=config.MD_BROKER) params.update(conn_url.dict()) market = CtpMarket().init(**params) # 装备行情对象到股票产品 stbase.controller.futures.setupMarket(market) # 初始化交易对象 params = conn_url.dict() params['strategy_id'] = strategy_id trader = CtpTrader().init(**params) stbase.controller.futures.setupTrader(trader) # 初始化策略对象 strategy = MyStrategy(strategy_id,stbase.controller.futures).init() #设置策略日志对象 strategy.getLogger().addAppender(strecoder.StragetyLoggerMongoDBAppender(db_prefix= dbname,host=mongodb_host,port=mongodb_port)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) # 控制器运行 # stbase.controller.run().waitForShutdown() # 开始运行 ,加载k线数据 stbase.controller.run()
def load(self, strategy_id, strategy_cls, symbol, strategy_db=('127.0.0.1', 27017), dbname='TradeFisherCtp', quotes_db=('127.0.0.1', 27017), cycle=1, start='2019-6-18 9:0', end='2019-6-19 15:0', freq=.1): """ :param strategy_id: 策略编号 :param strategy_cls: 策略类定义 :param symbol: 商品合约代码 :param strategy_db: 策略运行数据库(host,port) :param dbname: 策略运行数据库名 :param quotes_db: 行情数据库 :param cycle: K线周期 1,5,15,30 :param start: 回测加载数据开始时间 :param end: 回测加载数据结束时间 :param freq: K线回放速度控制 .1 s :return: """ SYMBOL = symbol mongodb_host, mongodb_port = strategy_db data_path = './' + strategy_id quotas_db_conn = MongoClient(quotes_db[0], quotes_db[1]) # 历史k线数据库 # 初始化系统参数控制器 paramctrl = stbase.MongoParamController() paramctrl.open(host=mongodb_host, port=mongodb_port, dbname=dbname) # 策略控制器 stbase.controller.init(data_path) # 添加运行日志处理 stbase.controller.getLogger().addAppender( stbase.FileLogAppender('CTP')) stbase.controller.setParamController(paramctrl) param = paramctrl.get(strategy_id) # 读取指定策略id的参数 # conn_url = paramctrl.get_conn_url(param.conn_url) # 读取策略相关的交易账户信息 # 初始化行情对象 params = dict(db_conn=quotas_db_conn, cycle=cycle, symbol=SYMBOL, start=start, end=end, freq=freq) # params.update( conn_url.dict() ) market = CtpMarketBarBackTest().init(**params) # 配置历史行情记录加载器 # 装备行情对象到股票产品 stbase.controller.futures.setupMarket(market) # 初始化交易对象 # trader = CtpTrader().init(**conn_url.dict()) trader = CtpTraderBackTest().init() stbase.controller.futures.setupTrader(trader) # 初始化策略对象 strategy = strategy_cls( strategy_id, stbase.controller.futures).init().setLoader(self) # 设置策略日志对象 strategy.getLogger().addAppender( strecoder.StragetyLoggerMongoDBAppender(db_prefix=dbname, host=mongodb_host, port=mongodb_port)) # 添加策略到 控制器 stbase.controller.addStrategy(strategy) self.strategy = strategy return self