def MyInit(self): ts.set_token( '75f181a930dc581d82c0cafd633c09d582d8ac0554c74854f73a9582') my_log_filename = self.log_name + ".txt" if os.path.exists(my_log_filename): os.remove(my_log_filename) log = tools.CLogger('limitconcept', my_log_filename, 1) strMsg = 'start' log.getLogger().info(strMsg)
def __init__(self): self.log_name = '../../log/backtest_log' self.use_mul_process = 0 # 进程池里进程个数 self.process_cnt = 3 self.db = dbmgr.CDBMgr('localhost', 'root', '123', 'kdata') #以时间命名日志文件 cur_time = datetime.datetime.now() log_filename = datetime.datetime.strftime(cur_time, '%Y%m%d_%H%M%S') my_log_filename = self.log_name + log_filename + ".txt" self.log = tools.CLogger('backtest', my_log_filename, 1)
def statistics(): ############################################################################################## # 配置文件路径 cur_path = os.getcwd() print(cur_path) str_conf_path = cur_path + '/../../conf/conf.ini' # 日志唯一 g_conf = conf.CConf(str_conf_path) filename_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S') log_filename = cur_path + '/../../' + g_conf.log_dir + '/backtest_' + filename_time + '.log' log = tools.CLogger(g_conf.app_name, log_filename, 1).getLogger() ############################################################################################## backtest = CBackTestBycode(str_conf_path, log) backtest.strategy.statistics()
def main(): # 配置文件路径 cur_path = os.getcwd() print(cur_path) str_conf_path = cur_path + '/../../conf/conf.ini' # 日志唯一 g_conf = conf.CConf(str_conf_path) filename_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S') log_filename = cur_path + '/' + g_conf.log_dir + '/../../tide_system_' + filename_time + '.log' g_log = tools.CLogger(g_conf.app_name, log_filename, 1).getLogger() dataservice = CDataServiceMysql(str_conf_path, g_log) dataservice.aps_dataservice_update() # 更新的起始时间 '''str_start_day = '20171201'
def process(process_cnt, cur_idx): print(cur_idx) ############################################################################################## # 配置文件路径 cur_path = os.getcwd() print(cur_path) str_conf_path = cur_path + '/../../conf/conf.ini' # 日志唯一 g_conf = conf.CConf(str_conf_path) filename_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S') log_filename = cur_path + '/../../' + g_conf.log_dir + '/backtest_' + filename_time + '.log' log = tools.CLogger(g_conf.app_name, log_filename, 1).getLogger() ############################################################################################## backtest = CBackTestBycode(str_conf_path, log) #回测时间范围:起始日期到最近一个交易日 start_day = '20180101' backtest.process(start_day, process_cnt, cur_idx)
#任务调度,阻塞 #from flask_apscheduler import APScheduler as myScheduler #from apscheduler.schedulers.background import BackgroundScheduler as myScheduler app = Flask(__name__) #配置文件路径 cur_path = os.getcwd() print(cur_path) str_conf_path = cur_path + '/conf/conf.ini' #日志唯一 g_conf = conf.CConf(str_conf_path) filename_time = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d_%H%M%S') log_filename = cur_path+'/'+g_conf.log_dir+'/tide_system_'+filename_time+'.log' g_log = tools.CLogger(g_conf.app_name, log_filename, 1).getLogger() #任务调度 schedulermgr = schedulermgr.CSchedulerMgr(str_conf_path, g_log) schedulermgr.start() def GetDB(): myconf = conf.CConf(str_conf_path) myconf.ReadConf() db = dbmgr.CDBMgr(myconf.db_host, myconf.db_username, myconf.db_pwd, 'kdata') db.connect_db() return db def GetToday(): now_time = datetime.datetime.now() day = now_time.strftime('%Y%m%d')
def __init__(self): self.buy_price = 0 self.buy_date = "000" self.sell_price = 0 self.sell_date = "000" self.log = tools.CLogger('backtest', '../../log/backtest_log.txt', 1)
def process(start_idx, end_idx): print("start=", start_idx, " end=", end_idx) my_log_filename = log_name + str(start_idx) + ".txt" if os.path.exists(my_log_filename): os.remove(my_log_filename) #首次投入基础数据的数量 base_count = 70 # input 股票详情 str_filename = '../../data/stockdaily.h5' file_daily = h5py.File(str_filename, 'r') log = tools.CLogger('backtest', my_log_filename, 1) strMsg = 'open success, for start_idx=' + str( start_idx) + ' end_idx=' + str(end_idx) log.getLogger().info(strMsg) # 策略1 #strategy = strategy01.CStrategy01(file_daily) # 策略2 strategy = strategy06.CStrategy06(file_daily) cur_code_idx = 0 for key in file_daily.keys(): cur_code_idx += 1 #不属于自己的股票,不处理 if cur_code_idx not in range(start_idx, end_idx + 1): continue code = file_daily[key].name strategy.Init() strategy.LoadData(code, base_count) print('process ... ', cur_code_idx, ' code=', code) myStati = st.CStatistics() # 股票符号,如000001 stockSymbol = file_daily[code + '/symbol'].value # 股票名 stockName = file_daily[code + '/name'].value #取后续一天一天的数据,模拟新数据 key = '/' + code totalCnt = len(file_daily[key + '/close'][:]) for i in range(totalCnt - base_count): newIdx = i + base_count close_price = file_daily[key + '/close'][newIdx] open_price = file_daily[key + '/open'][newIdx] low_price = file_daily[key + '/low'][newIdx] high_price = file_daily[key + '/high'][newIdx] vol = file_daily[key + '/vol'][newIdx] trade_date = file_daily[key + '/trade_date'][newIdx] #输入一天数据 state = strategy.FeedOneData(close_price, open_price, low_price, high_price, vol) if state is State.view: print(sys._getframe().f_code.co_name, sys._getframe().f_lineno, ':', code, '开始观察, state=', state, ' date=', trade_date) elif state is State.out_view: print(sys._getframe().f_code.co_name, sys._getframe().f_lineno, ':', code, ' , 退出观察 state=', state, ' date=', trade_date, end='\n\n') elif state is State.buy: #买入的价格 #myStati.set_buy_info((high_price + low_price)/2) myStati.set_buy_info(strategy.buy_price, trade_date) strLog = "%s%d,code=%s,买入 state=%s date=%s open=%.2f,close=%.2f" % ( sys._getframe().f_code.co_name, sys._getframe().f_lineno, code, state, trade_date, open_price, close_price) # 给日志加锁 log.getLogger().info(strLog) elif state is State.sell: #卖出的价格 #myStati.set_sell_info((high_price + low_price) / 2) myStati.set_sell_info(strategy.sell_price, trade_date) strLog = "%s%d,code=%s,卖出 state=%s date=%s open=%.2f,close=%.2f" % ( sys._getframe().f_code.co_name, sys._getframe().f_lineno, code, state, trade_date, open_price, close_price) #写多个日志文件 不需要加锁 log.getLogger().info(strLog) #统计卖出后的盈亏 myStati.statistics(code)