def recInstruments(recoveredlist, closeprice): log.WriteLog( 'LOG_TRADE', "[===adjust===] getlast 1: Time = %s InstrumentID = %s ClosePrice = %s " % (recoveredlist[-1]['Time'], recoveredlist[-1]['InstrumentID'], recoveredlist[-1]['ClosePrice'])) ndiff = closeprice - recoveredlist[-1]['ClosePrice'] log.WriteLog( 'LOG_TRADE', "[get adjusted value :] nClosePrice - lClosePrice = %s - %s = %s " % (closeprice, recoveredlist[-1]['ClosePrice'], ndiff)) for res in recoveredlist: res['HighestPrice'] = res['HighestPrice'] + ndiff res['OpenPrice'] = res['OpenPrice'] + ndiff res['LowestPrice'] = res['LowestPrice'] + ndiff res['ClosePrice'] = res['ClosePrice'] + ndiff log.WriteLog( 'LOG_TRADE', "[===adjusted===] getlast 1: Time = %s InstrumentID = %s ClosePrice = %s " % (recoveredlist[-1]['Time'], recoveredlist[-1]['InstrumentID'], recoveredlist[-1]['ClosePrice'])) return recoveredlist
def get_Inscontinue_data(instrument_info, mydb): for row in instrument_info: sourcelist = [] recoveredlist = [] ntmpExeclist = [] log.WriteLog( 'LOG_TRADE', "==incremental_update_begin instrument_info== %s" % (row['InstrumentsName'])) resetlist = reset_Instrument_details(mydb, row) print("reset:", resetlist) log.WriteLog('LOG_TRADE', "reset: %s" % resetlist) for i, d in enumerate(resetlist): tlist = d tlist['Exchange'] = row['Exchange'] if 'isRev' in d and 'nClosePrice' in d: recoveredlist = rec_closePrice(mydb, tlist, recoveredlist) if len(recoveredlist) > 0: recoveredlist = rec_closePrice(mydb, tlist, recoveredlist) if i < len(resetlist) - 1: endtime = resetlist[i + 1]['BeginTime'] else: endtime = time.strftime('%y%m%d', time.localtime()) tlist['Endtime'] = endtime reslist = get_hq_data(mydb, tlist) for v in reslist: sourcelist.append(v) recoveredlist.append(v.copy()) if i == len(ntmpExeclist) - 1: lpara = {} lpara['InstrumentsName'] = row['InstrumentsName'] con_gentodate(sourcelist, recoveredlist, lpara, mydb)
def insertDB_Instruments(InstrumentsName, sourcelist, recoveredlist, mydb): tmplist = [] if len(sourcelist) != len(recoveredlist): print("err sourcelen %s != reclen %s" % (len(sourcelist), len(recoveredlist))) return for i in range(len(sourcelist)): tmpValues = (InstrumentsName, sourcelist[i]['Time'], sourcelist[i]['OpenPrice'], sourcelist[i]['ClosePrice'], sourcelist[i]['HighestPrice'], sourcelist[i]['LowestPrice'], round(recoveredlist[i]['OpenPrice'], 3), round(recoveredlist[i]['ClosePrice'], 3), round(recoveredlist[i]['HighestPrice'], 3), round(recoveredlist[i]['LowestPrice'], 3), round(recoveredlist[i]['Volume'], 3)) tmplist.append(tmpValues) if sourcelist[i]['Time'] != recoveredlist[i]['Time']: print("Timeerror", sourcelist[i]['Time'], recoveredlist[i]['Time']) return try: log.WriteLog('LOG_TRADE', "BEGIN INSERT SQL :...") _sql = "INSERT INTO `future_redetails`(`InstrumentsName`,`Time`,`OpenPrice`,`ClosePrice`,`HighestPrice`,`LowestPrice`,`rOpenPrice`,"\ "`rClosePrice`,`rHighestPrice`,`rLowestPrice`,`Volume` )VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);" mydb.Executemany(_sql, tmplist) log.WriteLog('LOG_TRADE', "====FINISH %s==" % InstrumentsName) except Exception as err: print(err) finally: pass
def main(): make_profit_dir() trade_rec_file = "1.txt" if len(sys.argv) > 1: trade_rec_file = sys.argv[1] # 读取交易记录 records = read_records(trade_rec_file) if len(records) == 0: raise Exception('cant read trading record!') return pos_manager = positions.PositionsManager() # 分析交易收益 infos = parse_records(pos_manager, records) # 计算每日净值 calc_day_profit(infos["contracts"].keys(), infos["day_profit"], infos["day_contracts"], infos["day_accountID"], infos["day_times"]) print "----------------" log.WriteLog("result", "合约\t平仓收益\t手续费") for c,v in infos["contracts"].items(): log.WriteLog("result" ,"%s\t%.2f\t%.2f"%(c, v[0], v[1]))
def calc_day_profit(sys_config, db_profit, trading_days, contracts, day_contract_profits, day_margins): contracts.sort() # 保存打印 title = "日期" for c in contracts: title += "\t" + c log.WriteLog("net_profit", title) for day in trading_days: line = "%s" % day for c in contracts: if day_contract_profits.has_key( day) and day_contract_profits[day].has_key(c): line += "\t%.2f" % day_contract_profits[day][c] else: line += "\t0" log.WriteLog("net_profit", line) # 合并合约 day_symbol_profits = {} for day in trading_days: if not day_symbol_profits.has_key(day): day_symbol_profits[day] = {} for c in contracts: if day_contract_profits.has_key( day) and day_contract_profits[day].has_key(c): symbol = contract_info.get_contract_symbol(c) if not day_symbol_profits[day].has_key(symbol): day_symbol_profits[day][symbol] = 0 day_symbol_profits[day][symbol] += day_contract_profits[day][c] profit_sqls = [] margin_sqls = [] for day in trading_days: for symbol, profit in day_symbol_profits[day].items(): sql = "REPLACE INTO fd_report_profit(accountID,time,instrumentID,profit) VALUES('%s', %s, '%s', %d);" % ( sys_config.fund_account, day, symbol, profit) profit_sqls.append(sql) if day_margins.has_key(day): sql = "REPLACE INTO fd_report_margin(accountID,time,margin) VALUES('%s', %s, %d);" % ( sys_config.fund_account, day, day_margins[day]) margin_sqls.append(sql) for sql in profit_sqls: log.WriteLog("net_profit_sql", sql) for sql in margin_sqls: log.WriteLog("net_margin_sql", sql) if sys_config.send_profit2db: for sql in sqls: db_profit.Execute(sql) db_profit.Commit()
def get_insdetails(instrumentsname, mydb): log.WriteLog('LOG_TRADE', "==gen_all_begin instrument_info== %s" % instrumentsname) sql = "select InstrumentsName,Period,BeginTime from Instrument_details where InstrumentsName = '%s' " % ( instrumentsname) log.WriteLog('LOG_TRADE', "SELECT SQL_Instrument_details :%s" % (sql)) mydb.Execute(sql) instrument_details = mydb.FetchAll() if instrument_details == None: log.WriteLog('LOG_ERROR', "==instrument_details none== ") return instrument_details
def check_complete_only(contract, k_datas, exg_cfg, tick): ma = re.findall("([^\d]+)[\d]+", contract) symbol = ma[0] is_pre_date = False cur_daymin_index = 0 daymin_set = [] for index, row in k_datas.iterrows(): time = row[1] if not tick: time = int(row[0]) date = 20000000 + time / 10000 trading_day = get_trading_day(time) if not is_pre_date: if date != trading_day: daymin_set = exg_cfg.get_contract_daymin(symbol, trading_day) is_pre_date = True else: if date == trading_day: daymin_set = exg_cfg.get_contract_daymin(symbol, date) is_pre_date = False cur_daymin_index = 0 if cur_daymin_index >= len(daymin_set): continue daymin = daymin_set[cur_daymin_index] if is_pre_date: while daymin < 2100 and cur_daymin_index + 1 < len(daymin_set): cur_daymin_index = cur_daymin_index + 1 daymin = daymin_set[cur_daymin_index] cur_time = date % 1000000 * 10000 + daymin while cur_time < time: log_name = "%s_%u" % (contract, 20000000 + cur_time / 10000) if tick: log.WriteLog(log_name, "tick时间戳[%u]数据缺失" % (cur_time)) else: log.WriteLog(log_name, "min时间戳[%u]数据缺失" % (cur_time)) cur_daymin_index = cur_daymin_index + 1 if cur_daymin_index >= len(daymin_set): break daymin = daymin_set[cur_daymin_index] cur_time = date % 1000000 * 10000 + daymin if cur_time == time: cur_daymin_index = cur_daymin_index + 1
def print_log(msg): log.set_log_mode('a') now_datetime = time.localtime() content = u"%u-%02u-%02u %02u:%02u:%02u %s"%(now_datetime.tm_year, now_datetime.tm_mon, now_datetime.tm_mday, now_datetime.tm_hour, now_datetime.tm_min, now_datetime.tm_sec, msg) log.WriteLog("sys", content)
def main(): parameters = sys.argv[1:] parameters_clean = check_argv(parameters) if not parameters_clean: print sys.exit(1) argv_jiraid, argv_projects, argv_act, options, argv_tag = parameters_clean os.system('clear') log_update = log.WriteLog(options) log_update.TimeMarker() log_update.DebugLog('check argv OK!') log_update.DebugLog('get issues ..') tzjira = workflow.tzJira() env, jiraid, ops_tag, update_tag, rollback_tag, projects, debugs, errors = tzjira.Get_issues(argv_jiraid, argv_projects, argv_act, options, argv_tag) if debugs != []: for debug in debugs: log_update.DebugLog(debug) if errors != []: for error in errors: log_update.ErrorLog(error) log_update.CloseLog() sys.exit(1) log_update.DebugLog('get issues OK!') log_update.DebugLog('init issues .. ') tasks, debugs, errors = init.main(env, projects) if debugs != []: for debug in debugs: log_update.DebugLog(debug) if errors != []: for error in errors: log_update.ErrorLog(error) log_update.CloseLog() sys.exit(1) log_update.DebugLog('init issues OK!') if not '--check' in options and 'update' == argv_act and not '--force=beta' in options and not '--force=prod' in options and env == 'prod' and not '--noupdatejira' in options: log_update.DebugLog('update jira ..') tzjira.Update_issues(jiraid, 'prodstart') log_update.DebugLog('update jira OK!') log_update.DebugLog('tasks begin ..') task_type = tasks.pop('type') if task_type == 'static' and argv_act == 'update': boolean = proto_b2b.syncing(jiraid, ops_tag, update_tag, tasks, options).main() elif task_type == 'application': boolean = app_b2b.main(env, jiraid, ops_tag, update_tag, rollback_tag, tasks, argv_act, options) log_update.DebugLog('tasks finish OK!') if not '--check' in options and 'update' == argv_act and not '--force=beta' in options and not '--force=prod' in options and boolean and not '--p-only' in options and not '--noupdatejira' in options: log_update.DebugLog('update jira ..') if env == 'beta': tzjira.Update_issues(jiraid, 'betaend') elif env == 'prod': tzjira.Update_issues(jiraid, 'prodend') log_update.DebugLog('update jira OK!') log_update.CloseLog()
def continue_generate(): mydb = func.create_database() # 查询合约-交易所信息表 mydb.Execute("select InstrumentsName,Exchange from instrument_info") instrument_info = mydb.FetchAll() get_Inscontinue_data(instrument_info, mydb) log.WriteLog('LOG_TRADE', "finish")
def process(this, datestamp, next_datestamp, all_positions): this.up_buy = this.mu + this.buy_param * this.sigma this.down_buy = this.mu - this.buy_param * this.sigma belong_slice_time = this.data_mgr.get_belong_slice_time( this.select_id, datestamp) if belong_slice_time is None: return None, None point = this.data_mgr.get_point(this.select_id, datestamp) if datestamp == belong_slice_time[0] and (point >= this.up_boundary or point <= this.down_boundary): this.up_boundary = point + 1.25 * this.sigma this.down_boundary = point - 1.25 * this.sigma return [(this.select_id, 0)], [] if next_datestamp is None or belong_slice_time != this.data_mgr.get_belong_slice_time( this.select_id, next_datestamp): this.buy_param = 0.75 this.up_boundary = this.mu + 2 * this.sigma this.down_boundary = this.mu - 2 * this.sigma if all_positions.has_key(this.select_id): #需换期 open_datestamp = all_positions[this.select_id].datetime this.position_mgr.wait_change_period(this.select_id, open_datestamp) log.WriteLog('wait_change_period', '%s\t%s' % (this.select_id, str(open_datestamp))) return [], [this.select_id] #当期结束时卖出 if all_positions.has_key(this.select_id): if point >= this.up_boundary or point <= this.down_boundary: #止损卖出 this.buy_param = 0.8 this.up_boundary = this.mu + 2 * this.sigma this.down_boundary = this.mu - 2 * this.sigma return [], [this.select_id] position_point = this.position_mgr.get_position_price_by_id( this.select_id) if (position_point < this.mu and point >= this.mu) or (position_point > this.mu and point <= this.mu): #盈利卖出 this.up_boundary = this.mu + 2 * this.sigma this.down_boundary = this.mu - 2 * this.sigma this.buy_param = 0.75 return [], [this.select_id] else: if not this.data_mgr.is_delivery_day(this.select_id, datestamp): if (this.buy_param == 0.75 and (point >= this.up_buy and point < this.up_boundary)) or ( this.buy_param == 0.8 and point <= this.up_buy and point > this.mu): return [(this.select_id, 0)], [] elif (this.buy_param == 0.75 and (point <= this.down_buy and point > this.down_boundary) ) or (this.buy_param == 0.8 and point >= this.down_buy and point < this.mu): return [(this.select_id, 1)], [] return None, None
def main(): make_log_dir() make_profit_dir() trade_rec_file = "trade_records.db" if len(sys.argv) > 1: trade_rec_file = sys.argv[1] db_data = create_database("db_data") db_profit = create_database("db_profit") exg_config = contract_info.create_exg_config() pos_manager = positions.PositionsManager() sys_config = Sys_config("system.ini", "fund_info") # 加载合约信息(乘数) contract_info.load_contract_mul() # 加载合约信息(保证金率) contract_info.load_contract_mar() # 获取该品种最后记录的净值 # rec = query_last_profit(db_profit, "virtual_tm_3", "mesh_ru") # 读取交易记录 records = read_records(trade_rec_file) if len(records) == 0: raise Exception, 'cant read trading record!' return # 获取起始交易日,查询所有交易日 trading_day = records[0][1] trading_days = get_trading_day(db_profit, trading_day) # 分析交易收益 infos = parse_records(pos_manager, trading_days, records, db_data) # 计算每日净值 calc_day_profit(sys_config, db_profit, trading_days, infos["contracts"].keys(), infos["day_profit"], infos["day_margin"]) log.WriteLog("result", "----------------") log.WriteLog("result", "result:\t合约\t平仓收益\t手续费") for c, v in infos["contracts"].items(): log.WriteLog("result", "result:\t%s\t%.2f\t%.2f" % (c, v[0], v[1])) log.WriteLog("result", "----------------") log.WriteLog("result", "position profit") K = infos["day_pos_profit"].keys() K.sort() total = 0 for c, v in infos["day_pos_profit"][K[-1]].items(): log.WriteLog("result", "%s:\t%s" % (c, v)) total += v log.WriteLog("result", "total:\t%s" % total) pos_manager.print_position()
def compare_data(k_datas, exg_cfg): groupy = k_datas.groupby(0) keys = groupy.groups.keys() sys_info = SystemConfig("system.ini") for contract in keys: min_file = "min/%s.txt" % (contract) if not os.path.exists(min_file): continue ma = re.findall("([^\d]+)[\d]+", contract) symbol = ma[0] # 检查分钟线数据完整性 min_datas = read_min_file(min_file) check_min_complete(contract, min_datas, exg_cfg) #遍历分钟数据进行比较 contract_datas = groupy.get_group(contract) cur_contract_data_index = 0 for index, row in min_datas.iterrows(): if cur_contract_data_index >= len(contract_datas.index): break cur_contract_data = contract_datas.iloc[cur_contract_data_index] cur_time = int(cur_contract_data[1]) min_time = int(row[0]) if min_time < cur_time: continue while cur_time < min_time: cur_contract_data_index = cur_contract_data_index + 1 if cur_contract_data_index >= len(contract_datas.index): break cur_contract_data = contract_datas.iloc[ cur_contract_data_index] cur_time = int(cur_contract_data[1]) if cur_time == min_time: if not exg_cfg.at_end_min(symbol, 20000000 + cur_time / 10000, cur_time % 10000): open_diff = float(row[1]) - float(cur_contract_data[2]) high_diff = float(row[2]) - float(cur_contract_data[3]) low_diff = float(row[3]) - float(cur_contract_data[4]) close_diff = float(row[4]) - float(cur_contract_data[5]) if abs(open_diff) > sys_info.limit_values[contract] or abs(high_diff) > sys_info.limit_values[contract] \ or abs(low_diff) > sys_info.limit_values[contract] or abs(close_diff) > sys_info.limit_values[contract]: log_name = "%s_diff" % (contract) log.WriteLog( log_name, "时间戳[%u] 开盘差[%.4f] 最高差[%.4f] 最低差[%.4f] 收盘差[%.4f]" % (cur_time, open_diff, high_diff, low_diff, close_diff))
def __init__(self, jiraid, ops_tag, update_tag, tasks, options): self.git = '%s %s' % (tasks['git'], update_tag) self.rsync = tasks['rsync'] self.jiraid = jiraid self.ops_tag = ops_tag self.update_tag = update_tag self.options = options self.log_proto = log.WriteLog(log_options=self.options, log_label='proto-b2b') self.log_proto.TimeMarker()
def add_record(this, datestamp, dict): log.WriteLog('add_record', '%s %s' % (str(datestamp), dict)) if dict is None: return for key in dict.keys(): if key not in this.df_records.columns: raise 'add_record error: key error' if not dict.has_key('id'): raise 'add_record error: has no key:id' row = pd.DataFrame(dict, index=[datestamp]) this.df_records = this.df_records.append(row)
def get_hq_data(mydb, tlist): reslist = [] # 构造sql查询参数 begintime = str(tlist['BeginTime']) + config.CON_BEGINTIME endtime = tlist['Endtime'] + config.CON_ENDTIME InstrumentID = tlist['InstrumentsName'] + tlist['Period'] sql = "select `id`,`InstrumentID`,`Time`,`OpenPrice`,`ClosePrice`,`HighestPrice`,`LowestPrice`,`Volume` from %s where InstrumentID = \"%s\" AND %s.Time+0 BETWEEN %s AND %s ORDER BY %s.Time ASC" % ( tlist['Exchange'], InstrumentID, tlist['Exchange'], begintime, endtime, tlist['Exchange']) log.WriteLog('LOG_TRADE', "SELECT SQL :%s" % (sql)) mydb.Execute(sql) # 查找到源数据 res = mydb.FetchAll() for r in res: reslist.append(r) log.WriteLog('LOG_TRADE', "[SQL GET] SQL_Cnt :%s" % (len(res))) return reslist
def calc_average_back(time_idx_arr, price_arr): #计算时间索引序列(X轴)平均数 ave_time_idx = calc_average(time_idx_arr) #计算价格序列(Y轴)平均数 ave_close_price = calc_average(price_arr) #计算时间索引和对应价格乘积平均数 ave_x_y_pro = calc_pro_average(time_idx_arr, price_arr) #计算时间索引平方平均数 ave_x_power = calc_pro_average(time_idx_arr, time_idx_arr) log.WriteLog("points", "time_idx:%u\tprice:%f" % (time_idx_arr[0], price_arr[0])) log.WriteLog( "points", "time_idx:%f\tprice:%f\txy:%f\tx_power:%f" % (ave_time_idx, ave_close_price, ave_x_y_pro, ave_x_power)) log.WriteLog("points", "\n") #计算斜率 a = (ave_time_idx * ave_close_price - ave_x_y_pro) / (ave_time_idx * ave_time_idx - ave_x_power) #计算截距 b = ave_close_price - a * ave_time_idx return [a, b]
def get_data_head(ini_info, head): head_ini_info = ini_info["header"] critical_ini_info = ini_info["critical"] head_index = {} head_info = head.split('\t') head_size = len(head_info) for i in xrange(0, head_size): if head_ini_info.has_key(head_info[i]): key = head_ini_info[head_info[i]] head_index[key] = i else: log.WriteLog( "error", "文件[head_type.ini][header]中关键字[%s]未被配置" % head_info[i]) for critical_info in critical_ini_info.keys(): if not head_index.has_key(critical_info): log.WriteLog( "error", "文件[head_type.ini][header]中critical关键字[%s]未被配置" % critical_info) return head_index
def rec_closePrice(mydb, tlist, recoveredlist): if len(recoveredlist) > 0: endtime = recoveredlist[-1]['Time'] else: endtime = tlist['BeginTime'] + config.CON_ENDTIME InstrumentID = tlist['InstrumentsName'] + tlist['Period'] log.WriteLog('LOG_TRADE', "[get_current_period before adjust %s]" % (InstrumentID)) sql = """select ClosePrice from %s where InstrumentID = \"%s\" AND %s.Time <= %s ORDER BY %s.Time DESC limit 1""" % ( tlist['Exchange'], InstrumentID, tlist['Exchange'], endtime, tlist['Exchange']) log.WriteLog('LOG_TRADE', "SELECT SQL :%s" % (sql)) mydb.Execute(sql) lcloseprice = mydb.FetchOne() if lcloseprice == None: log.WriteLog('LOG_ERROR', "getlastClosePrice = None") return rncloseprice = lcloseprice['ClosePrice'] log.WriteLog('LOG_TRADE', "nClosePrice = %s" % (rncloseprice)) if len(recoveredlist) <= 0: # 得到当期收盘价 ndiff = rncloseprice - tlist['nClosePrice'] log.WriteLog( 'LOG_TRADE', "[get adjusted value :] closeprice - rncloseprice = %s - %s = %s " % (rncloseprice, tlist['nClosePrice'], ndiff)) else: closeprice = recoveredlist[-1]['ClosePrice'] ndiff = rncloseprice - closeprice log.WriteLog( 'LOG_TRADE', "[get adjusted value :] closeprice - rncloseprice = %s - %s = %s " % (rncloseprice, recoveredlist[-1]['ClosePrice'], ndiff)) for r in recoveredlist: r['HighestPrice'] = r['HighestPrice'] + ndiff r['OpenPrice'] = r['OpenPrice'] + ndiff r['LowestPrice'] = r['LowestPrice'] + ndiff r['ClosePrice'] = r['ClosePrice'] + ndiff # 对历史数据update sql = "update future_redetails set rClosePrice = rClosePrice + %s,rHighestPrice = rHighestPrice + %s,rLowestPrice = rLowestPrice + %s,rOpenPrice = rOpenPrice + %s where InstrumentsName = '%s'" % ( ndiff, ndiff, ndiff, ndiff, tlist['InstrumentsName']) log.WriteLog('LOG_TRADE', "update SQL :%s" % (sql)) mydb.Execute(sql) return recoveredlist
def print_position(this): log.WriteLog("result", "--------------------") log.WriteLog("result", "print position") for contract, dir_positions in this.contracts.items(): if len(dir_positions[DIR_BUY]) > 0 or len( dir_positions[DIR_SELL]) > 0: buy_position_price = 0 sel_position_price = 0 buy_number = 0 sel_number = 0 for pos in dir_positions[DIR_BUY]: buy_position_price += pos.price * pos.number buy_number += pos.number log.WriteLog( "result", "hold\t%s\t[buy]\t%d-%06d\t%.2f\t%d" % (contract, pos.datetime / 1000000, pos.datetime % 1000000, pos.price, pos.number)) for pos in dir_positions[DIR_SELL]: sel_position_price += pos.price * pos.number sel_number += pos.number log.WriteLog( "result", "hold\t%s\t[sell]\t%d-%06d\t%.2f\t%d" % (contract, pos.datetime / 1000000, pos.datetime % 1000000, pos.price, pos.number)) log.WriteLog("result", "------------------static") if buy_number > 0: buy_position_price /= buy_number log.WriteLog( "result", "%s\t[buy]\t%d\t%.2f" % (contract, buy_number, buy_position_price)) if sel_number > 0: sel_position_price /= sel_number log.WriteLog( "result", "%s\t[sell]\t%d\t%.2f" % (contract, sel_number, sel_position_price)) log.WriteLog("result", "\n")
def send_record2db(sys_config, db_profit, records): for rec in records: contract = rec[0] date = rec[1] time = rec[2] trade_dir = rec[3] trade_type = rec[4] price = rec[5] trade_num = rec[6] commission = rec[7] sys_id = rec[8] sql = "REPLACE INTO fd_report_trade_record(tradeName, contract, sysID, type, tradeDate, tradeTime, dir, price, commission, number) VALUES('%s', '%s', %s, '%s', %d, %d, '%s', %.2f, %.2f, %d);" \ % (sys_config.fund_account, contract, sys_id, trade_type, date, time, trade_dir, price, commission, trade_num) log.WriteLog("record_sql", sql) db_profit.Execute(sql.encode('utf8')) db_profit.Commit()
def process(this): #this.plot() dates = this.data_mgr.get_trade_datestamps_by_id(this.select_id) mu = this.data_mgr.get_mu() sigma = this.data_mgr.get_sigma() ##中值、上买入线、上止损线、下买入线、下止损先 up_boundary = mu + 2 * sigma down_boundary = mu - 2 * sigma up_buy = mu + 0.75 * sigma down_buy = mu - 0.75 * sigma index = 0 for datestamp in dates: all_positions = this.position_mgr.get_all_positions() next_datestamp = None if index != len(dates) - 1: next_datestamp = dates[index + 1] buy_ids, sell_ids = this.policy_select.process( datestamp, next_datestamp, all_positions) this.policy_trade.process(datestamp, buy_ids, sell_ids) index += 1 if index == len(dates): for id in all_positions.keys(): datestamp = all_positions[id].datetime this.position_mgr.wait_change_period(id, datestamp) log.WriteLog('wait_change_period', '%s\t%s' % (id, str(datestamp))) this.report_mgr.write_record_to_csv() this.report_mgr.add_up(this.select_id) this.plot_mgr.plot_bar(this.report_mgr.get_records()) this.plot_mgr.plot_cumsum_profit_curve( this.report_mgr.get_records()) wait_change_period = this.position_mgr.get_wait_change_period( this.select_id) this.plot_mgr.plot_trade_point( this.report_mgr.get_records(), wait_change_period, [up_boundary, up_buy, mu, down_buy, down_boundary]) this.plot_mgr.show()
def main(env, jiraid, ops_tag, update_tag, rollback_tag, tasks, act, options): log_app = log.WriteLog(log_options=options, log_label='app_b2b.py') log_app.TimeMarker() # if not '--check' in options and env == 'prod' and (act == 'update' or act == 'rollback' or act == 'stop'): # log_app.DebugLog('update zabbix ..') # zabbixHost = 'http://192.168.241.1/zabbix/api_jsonrpc.php' # zabbix = tzZabbix.Zabbix(zabbixHost) # auth = zabbix.login(username='******', password='******') # log_app.DebugLog("zabbix create maintenance: %s" % auth) # maintenanceid = zabbix.maintenanceCreate(ops_tag, auth) # log_app.DebugLog("maintenanceid: %s" % maintenanceid) # if zabbix.logout(auth): # log_app.DebugLog("logout successfully, bye") # log_app.DebugLog('update zabbix OK!') # else: # log_app.ErrorLog("zabbix logout error!") boolean = True lock = multiprocessing.Lock() threads = [] if '--check' in options or act == 'update': log_app.DebugLog('check packages ..') boolean = check_package(tasks, update_tag, options, log_app, env) if boolean: log_app.DebugLog('check packages OK!') if '--check' in options or (boolean and not '--p-only' in options): try: for project in tasks.keys(): start_exec = remoteExec(project, env, jiraid, ops_tag, update_tag, rollback_tag, tasks, act, options, lock) start_exec.start() threads.append(start_exec) for t in threads: t.join() except Exception, e: log_app.ErrorLog(e)
def traverse(high_low_set): results = [] flag_info = [] #上一旗形线性回归斜率和截距(无需区分高低) last_back_info = [] #旗形高点线性回归斜率和截距 high_back_info = [] #旗形低点线性回归斜率和截距 low_back_info = [] #旗形高点的时间索引、时间戳、价格 high_points = [[], [], []] #临时旗形高点的时间索引、时间戳、价格 temp_high_points = [[], [], []] #旗形低点的时间索引、时间戳、价格 low_points = [[], [], []] #临时旗形低点的时间索引、时间戳、价格 temp_low_points = [[], [], []] for high_low in high_low_set: high_len = len(high_points[0]) low_len = len(low_points[0]) change_turning = False if high_low[0] == "high": #在高点达到2个以前,不进行是否加入线性回归方程的判断 if high_len < 2: high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) #在高点达到2个的时候,计算初始线性回归方程信息 if high_len == 1: for i in xrange(0, len(high_points[0])): log.WriteLog( "points", "high_point_idx:%u\thigh_point_time:%u\thigh_point_price:%f" % (high_points[0][i], high_points[1][i], high_points[2][i])) high_back_info = calc_average_back(high_points[0], high_points[2]) log.WriteLog( "function", "point:%f\tslope:%f\tintercept:%f" % (high_low[2], high_back_info[0], high_back_info[1])) #在高点达到2个后,从第3个点开始,进行是否加入方程的判断 else: a = high_back_info[0] b = high_back_info[1] #以线性回归方程做计算,算出时间索引对应下的目标价格 tar_price = a * high_low[1] + b #若实际价格偏离目标价格2.5%以上,则判断该点不属于当前方程 if (high_low[3] - tar_price) > tar_price * 0.03: #若斜率向上,则方程斜率变高,需去除旧的高点,用最后一个旧高点与最新一点做线性回归运算 if a > 0: #在斜率本就大于0的情况下增大,不可能出现斜率转向 temp_high_points = [[], [], []] temp_high_points[0].append(high_points[0][high_len - 1]) temp_high_points[1].append(high_points[1][high_len - 1]) temp_high_points[2].append(high_points[2][high_len - 1]) temp_high_points[0].append(high_low[1]) temp_high_points[1].append(high_low[2]) temp_high_points[2].append(high_low[3]) high_back_info = calc_average_back( temp_high_points[0], temp_high_points[2]) high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) #若斜率向下,则考虑低点斜率的情况 else: #若低点斜率向下则为突破成功 if low_back_info[0] < 0: if a >= -9: #记录突破信息 if len(last_back_info) > 0: log.WriteLog( "break", "break high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], tar_price, last_back_info[0], last_back_info[1])) else: log.WriteLog( "break", "break high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f" % (high_low[1], high_low[2], high_low[3], tar_price)) #记录突破时高低点坐标,斜率与截距 log.WriteLog( "function", "high:time:%u\tslope:%f\tintercept:%f\tlow:time:%u\tslope:%f\tintercept:%f" % (high_points[1][0], high_back_info[0], high_back_info[1], low_points[1][0], low_back_info[0], low_back_info[1])) #以突破点横坐标为横坐标找出延长点坐标 high_extend_price = high_back_info[ 0] * high_low[1] + high_back_info[1] low_extend_price = low_back_info[0] * high_low[ 1] + low_back_info[1] #先记录此次旗形信息 flag_info.append( high_points[0][0]) #阻力线起始点时间戳索引 flag_info.append(0) #阻力线起始点类型0为最高值 1为最低值 flag_info.append(high_points[1][0]) #阻力线起始点时间戳 flag_info.append(high_points[2][0]) #阻力线起始点价格 if len(temp_high_points[0]) > 0: flag_info.append(temp_high_points[1][0]) flag_info.append(temp_high_points[2][0]) else: flag_info.append(high_points[1][0]) flag_info.append(high_points[2][0]) flag_info.append(high_low[2]) #阻力线结束点时间戳 flag_info.append(high_extend_price) #阻力线结束点价格 flag_info.append(low_points[1][0]) #阻力线起始点时间戳 flag_info.append(low_points[2][0]) #阻力线起始点价格 if len(temp_low_points[0]) > 0: flag_info.append(temp_low_points[1][0]) flag_info.append(temp_low_points[2][0]) else: flag_info.append(low_points[1][0]) flag_info.append(low_points[2][0]) flag_info.append(high_low[2]) #阻力线结束点时间戳 flag_info.append(low_extend_price) #阻力线结束点价格 flag_info.append(high_back_info[0]) #阻力线斜率 flag_info.append(high_back_info[1]) #阻力线截距 #若上一次旗形为空,即此次旗形并非突破而来,则从阻力起始点开始计算经过距离 if len(last_back_info) == 0: flag_info.append( low_points[0][low_len - 1] - high_points[0][0]) #阻力线经过时间戳数量 flag_info.append( high_points[2][0] - low_points[2][low_len - 1]) #阻力线经过价格 #否则以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 else: break_point_price = last_back_info[ 0] * low_points[0][0] + last_back_info[ 1] flag_info.append(low_points[0][low_len - 1] - low_points[0][0]) flag_info.append(break_point_price - low_points[2][low_len - 1]) result_len = len(results) if result_len > 0: results[result_len - 1][18] = low_points[0][ low_len - 1] - low_points[0][0] results[result_len - 1][ 19] = break_point_price - low_points[ 2][low_len - 1] flag_info.append(0) #阻力线突破后经过时间戳数量(暂空) flag_info.append(0) #阻力线突破后经过价格(暂空) #将此次阻力线信息存入集合 results.append(flag_info) #清空阻力线信息以便下次接收 flag_info = [] #记录此次线性回归方程斜率和截距 last_back_info = [] last_back_info.extend(high_back_info) else: #上一次旗形非空,以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 if len(last_back_info) > 0: break_point_price = last_back_info[ 0] * low_points[0][0] + last_back_info[ 1] result_len = len(results) if result_len > 0: results[result_len - 1][18] = low_points[0][ low_len - 1] - low_points[0][0] results[result_len - 1][ 19] = break_point_price - low_points[ 2][low_len - 1] last_back_info = [] #此次旗形已结束,更新高低点集合,最新点为第一高点,原旗形最后一点为第一低点 high_points = [[], [], []] high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) low_points[0] = low_points[0][low_len - 1:] low_points[1] = low_points[1][low_len - 1:] low_points[2] = low_points[2][low_len - 1:] #突破发生则清空临时低点集合 temp_high_points = [[], [], []] temp_low_points = [[], [], []] #清空此次方程斜率和截距 high_back_info = [] low_back_info = [] #若低点斜率向上则重新统计斜率 else: log.WriteLog("break", "cross:%u" % high_low[2]) temp_high_points = [[], [], []] temp_high_points[0].append( high_points[0][high_len - 1]) temp_high_points[1].append( high_points[1][high_len - 1]) temp_high_points[2].append( high_points[2][high_len - 1]) temp_high_points[0].append(high_low[1]) temp_high_points[1].append(high_low[2]) temp_high_points[2].append(high_low[3]) high_back_info = calc_average_back( temp_high_points[0], temp_high_points[2]) if high_back_info[0] > 0: change_turning = True high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) #若实际价格偏离目标价格2.5%以上,则判断该点不属于当前方程 elif (high_low[3] - tar_price) < -tar_price * 0.03: #无论斜率方向如何,方程斜率变低,需去除旧的高点,用最后一个旧高点与最新一点做线性回归运算 temp_high_points = [[], [], []] temp_high_points[0].append(high_points[0][high_len - 1]) temp_high_points[1].append(high_points[1][high_len - 1]) temp_high_points[2].append(high_points[2][high_len - 1]) temp_high_points[0].append(high_low[1]) temp_high_points[1].append(high_low[2]) temp_high_points[2].append(high_low[3]) high_back_info = calc_average_back(temp_high_points[0], temp_high_points[2]) #在斜率本就小于0的情况下变小,不可能出现斜率转向 #只有在斜率大于0的情况下变小,才可能出现斜率转向 if a > 0 and high_back_info[0] < 0: change_turning = True high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) #若实际价格未偏离目标的2.5%以上,则加入线性回归方程计算 else: high_points[0].append(high_low[1]) high_points[1].append(high_low[2]) high_points[2].append(high_low[3]) #若经过斜率偏离的重新计算,则以高点临时集合进行计算 if len(temp_high_points[0]) > 0: temp_high_points[0].append(high_low[1]) temp_high_points[1].append(high_low[2]) temp_high_points[2].append(high_low[3]) high_back_info = calc_average_back( temp_high_points[0], temp_high_points[2]) #否则在原先的高点集合中计算 else: high_back_info = calc_average_back( high_points[0], high_points[2]) #此种情况下可以出现任意斜率转向的情况 if a > 0 and high_back_info[ 0] < 0 or a < 0 and high_back_info[0] > 0: change_turning = True elif high_low[0] == "low": #在低点达到2个以前,不进行是否加入线性回归方程的判断 if low_len < 2: low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) #在低点达到2个的时候,计算初始线性回归方程信息 if low_len == 1: for i in xrange(0, len(low_points[0])): log.WriteLog( "points", "low_point_idx:%u\tlow_point_time:%u\tlow_point_price:%f" % (low_points[0][i], low_points[1][i], low_points[2][i])) low_back_info = calc_average_back(low_points[0], low_points[2]) log.WriteLog( "function", "point:%f\tslope:%f\tintercept:%f" % (high_low[2], low_back_info[0], low_back_info[1])) #在低点达到2个后,从第3个点开始,进行是否加入方程的判断 else: a = low_back_info[0] b = low_back_info[1] #以线性回归方程做计算,算出时间索引对应下的目标价格 tar_price = a * high_low[1] + b #若实际价格偏离目标价格2.5%以上,则判断该点不属于当前方程 if (high_low[3] - tar_price) < -tar_price * 0.03: #若斜率向下,则方程斜率变低,需去除旧的低点,用最后一个旧低点与最新一点做线性回归运算 if a < 0: #在斜率本就小于0的情况下减小,不可能出现斜率转向 temp_low_points = [[], [], []] temp_low_points[0].append(low_points[0][low_len - 1]) temp_low_points[1].append(low_points[1][low_len - 1]) temp_low_points[2].append(low_points[2][low_len - 1]) temp_low_points[0].append(high_low[1]) temp_low_points[1].append(high_low[2]) temp_low_points[2].append(high_low[3]) low_back_info = calc_average_back( temp_low_points[0], temp_low_points[2]) low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) #若斜率向上,则考虑高点斜率的情况 else: #若高点斜率向上则为突破成功 if high_back_info[0] > 0: if a <= 9: #记录突破信息 if len(last_back_info) > 0: log.WriteLog( "break", "break low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], tar_price, last_back_info[0], last_back_info[1])) else: log.WriteLog( "break", "break low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f" % (high_low[1], high_low[2], high_low[3], tar_price)) #记录突破时高低点坐标,斜率与截距 log.WriteLog( "function", "high:time:%u\tslope:%f\tintercept:%f\tlow:time:%u\tslope:%f\tintercept:%f" % (high_points[1][0], high_back_info[0], high_back_info[1], low_points[1][0], low_back_info[0], low_back_info[1])) #以突破点横坐标为横坐标找出延长点坐标 high_extend_price = high_back_info[ 0] * high_low[1] + high_back_info[1] low_extend_price = low_back_info[0] * high_low[ 1] + low_back_info[1] #先记录此次旗形信息 flag_info.append( low_points[0][0]) #阻力线起始点时间戳索引 flag_info.append(1) #阻力线起始点类型0为最高值 1为最低值 flag_info.append(low_points[1][0]) #阻力线起始点时间戳 flag_info.append(low_points[2][0]) #阻力线起始点价格 if len(temp_low_points[0]) > 0: flag_info.append(temp_low_points[1][0]) flag_info.append(temp_low_points[2][0]) else: flag_info.append(low_points[1][0]) flag_info.append(low_points[2][0]) flag_info.append(high_low[2]) #阻力线结束点时间戳 flag_info.append(low_extend_price) #阻力线结束点价格 flag_info.append(high_points[1][0]) #阻力线起始点时间戳 flag_info.append(high_points[2][0]) #阻力线起始点价格 if len(temp_high_points[0]) > 0: flag_info.append(temp_high_points[1][0]) flag_info.append(temp_high_points[2][0]) else: flag_info.append(high_points[1][0]) flag_info.append(high_points[2][0]) flag_info.append(high_low[2]) #阻力线结束点时间戳 flag_info.append(high_extend_price) #阻力线结束点价格 flag_info.append(low_back_info[0]) #阻力线斜率 flag_info.append(low_back_info[1]) #阻力线截距 #若上一次旗形为空,即此次旗形并非突破而来,则从阻力起始点开始计算经过距离 if len(last_back_info) == 0: flag_info.append( high_points[0][high_len - 1] - low_points[0][0]) #阻力线经过时间戳数量 flag_info.append( high_points[2][high_len - 1] - low_points[2][0]) #阻力线经过价格 #否则以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 else: break_point_price = last_back_info[ 0] * high_points[0][ 0] + last_back_info[1] flag_info.append(high_points[0][high_len - 1] - low_points[0][0]) flag_info.append(high_points[2][high_len - 1] - break_point_price) result_len = len(results) if result_len > 0: results[result_len - 1][18] = high_points[0][ high_len - 1] - low_points[0][0] results[result_len - 1][19] = high_points[2][ high_len - 1] - break_point_price flag_info.append(0) #阻力线突破后经过时间戳数量(暂空) flag_info.append(0) #阻力线突破后经过价格(暂空) #将此次阻力线信息存入集合 results.append(flag_info) #清空阻力线信息以便下次接收 flag_info = [] #记录此次线性回归方程斜率和截距 last_back_info = [] last_back_info.extend(low_back_info) else: #上一次旗形非空,以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 if len(last_back_info) > 0: break_point_price = last_back_info[ 0] * high_points[0][ 0] + last_back_info[1] result_len = len(results) if result_len > 0: results[result_len - 1][18] = high_points[0][ high_len - 1] - low_points[0][0] results[result_len - 1][19] = high_points[2][ high_len - 1] - break_point_price last_back_info = [] #此次旗形已结束,更新高低点集合,最新点为第一低点,原旗形最后一点为第一高点 low_points = [[], [], []] low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) high_points[0] = high_points[0][high_len - 1:] high_points[1] = high_points[1][high_len - 1:] high_points[2] = high_points[2][high_len - 1:] #突破发生则清空临时低点集合 temp_high_points = [[], [], []] temp_low_points = [[], [], []] #清空此次方程斜率和截距 high_back_info = [] low_back_info = [] #若高点斜率向下则重新统计斜率 else: log.WriteLog("break", "cross:%u" % high_low[2]) temp_low_points = [[], [], []] temp_low_points[0].append(low_points[0][low_len - 1]) temp_low_points[1].append(low_points[1][low_len - 1]) temp_low_points[2].append(low_points[2][low_len - 1]) temp_low_points[0].append(high_low[1]) temp_low_points[1].append(high_low[2]) temp_low_points[2].append(high_low[3]) low_back_info = calc_average_back( temp_low_points[0], temp_low_points[2]) if low_back_info[0] < 0: change_turning = True low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) #若实际价格偏离目标价格2.5%以上,则判断该点不属于当前方程 elif (high_low[3] - tar_price) > tar_price * 0.03: #无论斜率方向如何,方程斜率变高,需去除旧的低点,用最后一个旧低点与最新一点做线性回归运算 temp_low_points = [[], [], []] temp_low_points[0].append(low_points[0][low_len - 1]) temp_low_points[1].append(low_points[1][low_len - 1]) temp_low_points[2].append(low_points[2][low_len - 1]) temp_low_points[0].append(high_low[1]) temp_low_points[1].append(high_low[2]) temp_low_points[2].append(high_low[3]) low_back_info = calc_average_back(temp_low_points[0], temp_low_points[2]) #在斜率本就大于0的情况下变大,不可能出现斜率转向 #只有在斜率小于0的情况下变大,才可能出现斜率转向 if a < 0 and low_back_info[0] > 0: change_turning = True low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) #若实际价格未偏离目标的2.5%以上,则加入线性回归方程计算 else: low_points[0].append(high_low[1]) low_points[1].append(high_low[2]) low_points[2].append(high_low[3]) #若经过斜率偏离的重新计算,则以高点临时集合进行计算 if len(temp_low_points[0]) > 0: temp_low_points[0].append(high_low[1]) temp_low_points[1].append(high_low[2]) temp_low_points[2].append(high_low[3]) low_back_info = calc_average_back( temp_low_points[0], temp_low_points[2]) #否则在原先的高点集合中计算 else: low_back_info = calc_average_back( low_points[0], low_points[2]) #此种情况下可以出现任意斜率转向的情况 if a > 0 and low_back_info[ 0] < 0 or a < 0 and low_back_info[0] > 0: change_turning = True #考虑线性回归方程斜率的各种情况,筛选出突破后未能形成旗形的情况 #1.高点方程尚未成型而低点方程已成型 if len(high_back_info) == 0 and len(low_back_info) > 0: #若上一旗形线性回归方程与低点方程方向一致(在此情况下理论上只会出现上一旗形方程与低点方程 #斜率都小于0的情况),说明突破失败 if len(last_back_info) > 0: if last_back_info[0] < 0 and low_back_info[0] < 0: #填充上一旗形突破距离 high_len = len(high_points[0]) break_point_price = last_back_info[0] * high_points[0][ high_len - 1] + last_back_info[1] #记录未转折就失败信息 log.WriteLog( "break", "unsucc_point high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) result_len = len(results) results[result_len - 1][18] = 0 results[result_len - 1][19] = high_points[2][high_len - 1] - break_point_price #清空清空方程斜率和截距 last_back_info = [] #2.低点方程未成型而高点方程已成型 elif len(low_back_info) == 0 and len(high_back_info) > 0: #若上一旗形线性回归方程与高点方程方向一致(在此情况下理论上只会出现上一旗形方程与低点方程 #斜率都大于0的情况),说明突破失败 if len(last_back_info) > 0: if last_back_info[0] > 0 and high_back_info[0] > 0: #填充上一旗形突破距离 low_len = len(low_points[0]) break_point_price = last_back_info[0] * low_points[0][ low_len - 1] + last_back_info[1] #记录未转折就失败信息 log.WriteLog( "break", "unsucc_point low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) result_len = len(results) results[result_len - 1][18] = 0 results[ result_len - 1][19] = break_point_price - low_points[2][low_len - 1] #清空清空方程斜率和截距 last_back_info = [] #3.高低点方程都已成型,但方向不一致 elif len(low_back_info) > 0 and len(high_back_info) > 0: if low_back_info[0] > 0 and high_back_info[0] < 0 or low_back_info[ 0] < 0 and high_back_info[0] > 0: #区分没有出现中途转向的情况和出现中途转向的情况 #中途转向(说明突破后旗形发展了一段时间) if change_turning: if len(last_back_info) > 0: #上一旗形向上的情况下 if last_back_info[0] > 0: #填充上一旗形突破距离 low_len = len(low_points[0]) break_point_price = last_back_info[0] * low_points[ 0][0] + last_back_info[1] #记录旗形成型转折失败信息 log.WriteLog( "break", "unsucc_change low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) #既然转向,则导致转向的那个点必然高于倒数第二点 result_len = len(results) results[result_len - 1][18] = low_points[0][ low_len - 2] - low_points[0][0] results[result_len - 1][19] = break_point_price - low_points[2][ low_len - 2] temp_high_points = [[], [], []] temp_low_points = [[], [], []] high_len = len(high_points[0]) high_points[0] = high_points[0][high_len - 1:] high_points[1] = high_points[1][high_len - 1:] high_points[2] = high_points[2][high_len - 1:] low_points[0] = low_points[0][low_len - 2:] low_points[1] = low_points[1][low_len - 2:] low_points[2] = low_points[2][low_len - 2:] low_back_info = calc_average_back( low_points[0], low_points[2]) high_back_info = [] #上一旗形向下的情况下 elif last_back_info[0] < 0: #填充上一旗形突破距离 high_len = len(high_points[0]) break_point_price = last_back_info[ 0] * high_points[0][0] + last_back_info[1] #记录旗形成型转折失败信息 log.WriteLog( "break", "unsucc_change high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) #既然转向,则导致转向的那个点必然低于倒数第二点 result_len = len(results) results[result_len - 1][18] = high_points[0][ high_len - 2] - high_points[0][0] results[result_len - 1][19] = high_points[2][ high_len - 2] - break_point_price temp_high_points = [[], [], []] temp_low_points = [[], [], []] low_len = len(low_points[0]) low_points[0] = low_points[0][low_len - 1:] low_points[1] = low_points[1][low_len - 1:] low_points[2] = low_points[2][low_len - 1:] high_points[0] = high_points[0][high_len - 2:] high_points[1] = high_points[1][high_len - 2:] high_points[2] = high_points[2][high_len - 2:] high_back_info = calc_average_back( high_points[0], high_points[2]) low_back_info = [] #清空清空方程斜率和截距 last_back_info = [] #并非中途转向(说明旗形一开始就未成型) else: if len(last_back_info) > 0: #上一旗形向上的情况下 if last_back_info[0] > 0: #填充上一旗形突破距离 low_len = len(low_points[0]) break_point_price = last_back_info[0] * low_points[ 0][0] + last_back_info[1] #记录旗形未成型转折失败信息 log.WriteLog( "break", "unsucc_line low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) result_len = len(results) results[result_len - 1][18] = 0 results[ result_len - 1][19] = break_point_price - low_points[2][0] high_len = len(high_points[0]) high_points[0] = high_points[0][high_len - 1:] high_points[1] = high_points[1][high_len - 1:] high_points[2] = high_points[2][high_len - 1:] high_back_info = [] #上一旗形向下的情况下 elif last_back_info[0] < 0: #填充上一旗形突破距离 high_len = len(high_points[0]) break_point_price = last_back_info[ 0] * high_points[0][0] + last_back_info[1] #记录旗形未成型转折失败信息 log.WriteLog( "break", "unsucc_line high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, last_back_info[0], last_back_info[1])) result_len = len(results) results[result_len - 1][18] = 0 results[ result_len - 1][19] = high_points[2][0] - break_point_price low_len = len(low_points[0]) low_points[0] = low_points[0][low_len - 1:] low_points[1] = low_points[1][low_len - 1:] low_points[2] = low_points[2][low_len - 1:] low_back_info = [] #清空清空方程斜率和截距 last_back_info = [] else: #只需考虑转向后高低线性回归方程斜率变一致的情况 if change_turning: if high_low[0] == "high": high_len = len(high_points[0]) high_points[0] = high_points[0][high_len - 2:] high_points[1] = high_points[1][high_len - 2:] high_points[2] = high_points[2][high_len - 2:] high_back_info = calc_average_back( high_points[0], high_points[2]) low_len = len(low_points[0]) low_points[0] = low_points[0][low_len - 2:] low_points[1] = low_points[1][low_len - 2:] low_points[2] = low_points[2][low_len - 2:] low_back_info = calc_average_back( low_points[0], low_points[2]) #记录旗形重新成型信息 log.WriteLog( "break", "resucc high\ttime:%u\ttime:%u\treal:%f\tslope:%f\tintercept:%f" % (high_low[1], high_low[2], high_low[3], low_back_info[0], low_back_info[1])) elif high_low[0] == "low": low_len = len(low_points[0]) low_points[0] = low_points[0][low_len - 2:] low_points[1] = low_points[1][low_len - 2:] low_points[2] = low_points[2][low_len - 2:] low_back_info = calc_average_back( low_points[0], low_points[2]) high_len = len(high_points[0]) high_points[0] = high_points[0][high_len - 2:] high_points[1] = high_points[1][high_len - 2:] high_points[2] = high_points[2][high_len - 2:] high_back_info = calc_average_back( high_points[0], high_points[2]) #记录旗形重新成型信息 log.WriteLog( "break", "resucc low\ttime:%u\ttime:%u\treal:%f\tslope:%f\tintercept:%f" % (high_low[1], high_low[2], high_low[3], high_back_info[0], high_back_info[1])) temp_high_points = [[], [], []] temp_low_points = [[], [], []] last_back_info = [] return results
def run(self): for self.ip in self.tasks[self.project]['servers'].keys(): self.debug_file = self.tasks[self.project]['other'][ self.ip]['debug_log'] self.error_file = self.tasks[self.project]['other'][ self.ip]['error_log'] self.ssh_port = self.tasks[self.project]['other'][ self.ip]['ssh_port'] if self.ssh_port == '': self.ssh_port = 22 self.ssh_method = self.tasks[self.project]['other'][ self.ip]['ssh_method'] if self.ssh_method == '': self.ssh_method = 'pkey' self.ssh_user = self.tasks[self.project]['other'][ self.ip]['ssh_user'] if self.ssh_user == '': self.ssh_user = '******' self.ssh_passwd = self.tasks[self.project]['other'][ self.ip]['ssh_passwd'] if self.ssh_passwd == '': self.ssh_passwd = None self.ssh_key = self.tasks[self.project]['other'][ self.ip]['ssh_key'] if self.ssh_key == '': self.ssh_key = None self.remote_log = log.WriteLog(self.options, self.debug_file, self.error_file, '%s %s' % (self.project, self.ip)) self.remote_log.TimeMarker() while True: try: self.remote_log.DebugLog("connect to %s .." % self.ip) self.ssh, self.scp = tzSsh.sshClient(self.ip).connect() except Exception as e: time.sleep(10) self.remote_log.ErrorLog( "%s\nconnect to %s Failed\n\tAfter trying for 10 seconds" % (e, self.ip)) continue else: self.remote_log.DebugLog("%s: connect to %s OK!" % (self.project, self.ip)) break for self.java_path in self.tasks[self.project]['servers'][ self.ip]['java_path'].split(','): self.java_port = self.tasks[self.project]['servers'][ self.ip]['java_port'] % self.java_path self.java_stop = self.tasks[self.project]['servers'][ self.ip]['java_stop'] % self.java_path self.java_start = self.tasks[self.project]['servers'][ self.ip]['java_start'] % self.java_path self.package_target = self.tasks[self.project]['servers'][ self.ip]['package_target'] % self.java_path self.clean_cache = self.tasks[self.project]['servers'][ self.ip]['clean_cache'].split(',') self.update_package = self.tasks[self.project]['servers'][ self.ip]['update_package'] self.rollback_package = self.tasks[self.project]['servers'][ self.ip]['rollback_package'] self.package_source = self.tasks[self.project]['servers'][ self.ip]['package_source'] self.offline = self.tasks[self.project]['offline'] self.package_name = os.path.basename(self.package_target) self.thrift_server = self.tasks[self.project]['thrift_server'] if 'rollback' == self.act: self.act_rollback() elif 'update' == self.act: self.act_update() elif 'restart' == self.act: self.act_restart() elif 'stop' == self.act: self.act_stop() elif 'start' == self.act: self.act_start() elif 'test' == self.act: self.act_test() else: pass if 'offline' == self.act: self.act_offline() elif 'online' == self.act: self.act_online() # if not '--check' in self.options: # retval = tzOps.ops().log(self.ops_tag, self.ip, self.project, self.jiraid) # if retval == '1': # self.remote_log.DebugLog('tzOps: log to ops %s %s %s successfully' % (self.ops_tag, self.ip, self.project)) # else: # self.remote_log.WarnLog('tzOps: log to ops %s %s %s failed' % (self.ops_tag, self.ip, self.project)) # self.remote_log.WarnLog("tzOps: retval -> %s" % retval) self.ssh.close() self.remote_log.CloseLog()
def calc_day_profit(contracts, day_contract_profits, day_contracts, day_accountID, day_times): contracts.sort() # 保存打印 title = "日期" for c in contracts: title += "\t" + c log.WriteLog("net_profit", title) days = day_contract_profits.keys() days.sort() for day in days: line = "%s" % day for c in contracts: if day_contract_profits[day].has_key(c): line += "\t%.2f" % day_contract_profits[day][c] else: line += "\t0" log.WriteLog("net_profit", line) title = "日期\t合约" log.WriteLog("day_contracts", title) days = day_contracts.keys() days.sort() for day in days: line = "%s" % day set_contracts = day_contracts[day] size = len(set_contracts) for i in xrange(0,size): if 0 == i: line += "\t%s" % set_contracts[i] else: line += ",%s" % set_contracts[i] log.WriteLog("day_contracts", line) title = "日期\t账号ID" log.WriteLog("day_accountID", title) days = day_accountID.keys() days.sort() for day in days: line = "%s" % day set_accountID = day_accountID[day] size = len(set_accountID) for i in xrange(0,size): if 0 == i: line += "\t%s" % set_accountID[i] else: line += ",%s" % set_accountID[i] log.WriteLog("day_accountID", line) title = "日期\t交易次数" log.WriteLog("day_times", title) days = day_times.keys() days.sort() for day in days: line = "%s\t%d" % (day, day_times[day]) log.WriteLog("day_times", line)
def get_Instrument_data(instrument_info, mydb): for row in instrument_info: sourcelist = [] recoveredlist = [] instrument_details = get_insdetails(row['InstrumentsName'], mydb) if instrument_details == None: return cnt = len(instrument_details) for i in range(0, cnt): # 结束时间以下条记录开始时间为准,如果是最后一条,以当前时间为结束时间 endtime = time.strftime('%y%m%d', time.localtime()) if i < cnt - 1: endtime = instrument_details[i + 1]['BeginTime'] # ===测试数据=== if row['InstrumentsName'] == 'TF': endtime = '140612' else: endtime = '140616' # ===测试数据=== # 构造sql查询参数 begintime = instrument_details[i][ 'BeginTime'] + config.CON_BEGINTIME endtime = endtime + config.CON_ENDTIME log.WriteLog( 'LOG_TRADE', "[Ready GET] InstrumentsName : %s,Period : %s,BeginTime : %s,EndTime : %s" % (instrument_details[i]['InstrumentsName'], instrument_details[i]['Period'], instrument_details[i]['BeginTime'], endtime)) instrumentID = instrument_details[i][ 'InstrumentsName'] + instrument_details[i]['Period'] sql = """select `id`,`InstrumentID`,`Time`,`OpenPrice`,`ClosePrice`,`HighestPrice`,`LowestPrice`,`Volume` from %s where InstrumentID = \"%s\" AND %s.Time+0 BETWEEN %s AND %s ORDER BY %s.Time ASC""" % ( row['Exchange'], instrumentID, row['Exchange'], begintime, endtime, row['Exchange']) log.WriteLog('LOG_TRADE', "SELECT SQL :%s" % (sql)) mydb.Execute(sql) # 查找到源数据 res = mydb.FetchAll() if res == None: log.WriteLog( 'LOG_ERROR', "instrumentID: %s isNone,begintime:%s, endtime:%s" % (instrumentID, begintime, endtime)) return log.WriteLog('LOG_TRADE', "[SQL GET] SQL_Cnt :%s" % (len(res))) if len(recoveredlist) > 0: # 复权时间 lasttime = recoveredlist[-1]['Time'] log.WriteLog( 'LOG_TRADE', "[get_current_period before adjust %s]" % (instrumentID)) sql = "select ClosePrice from %s where InstrumentID = '%s' AND %s.Time <= %s ORDER BY %s.Time DESC limit 1" % ( row['Exchange'], instrumentID, row['Exchange'], lasttime, row['Exchange']) mydb.Execute(sql) log.WriteLog('LOG_TRADE', "SELECT SQL :%s" % (sql)) nClosePrice = mydb.FetchOne()['ClosePrice'] log.WriteLog('LOG_TRADE', "nClosePrice = %s" % (nClosePrice)) recoveredlist = recInstruments(recoveredlist, nClosePrice) for r in res: sourcelist.append(r) recoveredlist.append(r.copy()) nmode = 1 # nmode 0:增量 1:全 fileIO.filewrite(row['InstrumentsName'], "source", sourcelist, nmode) fileIO.filewrite(row['InstrumentsName'], "recover", recoveredlist, nmode) func.insertDB_Instruments(instrument_details[i]['InstrumentsName'], sourcelist, recoveredlist, mydb)
def process_point(self, high_low): self.temp_points = {"high": [[], [], []], "low": [[], [], []]} self.change_turning = False point_type = high_low[0] other_point_type = "low" if point_type == "high" else "high" other_points_len = len(self.points[other_point_type][0]) points_len = len(self.points[point_type][0]) #在极值点达到2个以前,不进行是否加入线性回归方程的判断 if points_len < 2: self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) #在极值达到2个的时候,计算初始线性回归方程信息 if points_len == 1: for i in xrange(0, len(self.points[point_type][0])): log.WriteLog( "points", "type:%s\tpoint_idx:%u\tpoint_time:%u\tpoint_price:%f" % (point_type, self.points[point_type][0][i], self.points[point_type][1][i], self.points[point_type][2][i])) self.back_info[point_type] = calc_average_back( self.points[point_type][0], self.points[point_type][2]) log.WriteLog( "function", "point:%f\tslope:%f\tintercept:%f" % (high_low[2], self.back_info[point_type][0], self.back_info[point_type][1])) #在极值点达到2个后,从第3个点开始,进行是否加入方程的判断 else: a = self.back_info[point_type][0] b = self.back_info[point_type][1] #以线性回归方程做计算,算出时间索引对应下的目标价格 tar_price = a * high_low[1] + b #若实际价格偏离目标价格2.5%以上,则判断该点不属于当前方程 if (point_type == "high" and (high_low[3] - tar_price) > tar_price * OFFSET_RATE) \ or (point_type == "low" and (tar_price - high_low[3]) > tar_price * OFFSET_RATE): #若斜率与偏离方向同向,则方程斜率绝对值变大,需去除旧的高点,用最后一个旧高点与最新一点做线性回归运算 if point_type == "high" and a > 0 or point_type == "low" and a < 0: #在斜率往原本方向同一方向偏离时,不可能出现斜率转向 self.temp_points[point_type] = [[], [], []] self.temp_points[point_type][0].append( self.points[point_type][0][points_len - 1]) self.temp_points[point_type][1].append( self.points[point_type][1][points_len - 1]) self.temp_points[point_type][2].append( self.points[point_type][2][points_len - 1]) self.temp_points[point_type][0].append(high_low[1]) self.temp_points[point_type][1].append(high_low[2]) self.temp_points[point_type][2].append(high_low[3]) self.back_info[point_type] = calc_average_back( self.temp_points[point_type][0], self.temp_points[point_type][2]) self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) #若斜率与偏离方向不同向,则考虑另一线性回归线的情况 else: #若另一线性回归线与原线性回归线同向则突破成功 if (other_point_type == "low" and len(self.back_info[other_point_type]) and self.back_info[other_point_type][0]) < 0 \ or (other_point_type == "high" and len(self.back_info[other_point_type]) and self.back_info[other_point_type][0] > 0): if abs(a) <= MAX_SLOPE: #记录突破信息 if len(self.last_back_info) > 0: log.WriteLog( "break", "break %s\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (point_type, high_low[1], high_low[2], high_low[3], tar_price, self.last_back_info[0], self.last_back_info[1])) else: log.WriteLog( "break", "break %s\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f" % (point_type, high_low[1], high_low[2], high_low[3], tar_price)) #记录突破时高低点坐标,斜率与截距 log.WriteLog( "function", "%s:time:%u\tslope:%f\tintercept:%f\t%s:time:%u\tslope:%f\tintercept:%f" % (point_type, self.points[point_type][1][0], self.back_info[point_type][0], self.back_info[point_type][1], other_point_type, self.points[other_point_type][1][0], self.back_info[other_point_type][0], self.back_info[other_point_type][1])) #以突破点横坐标为横坐标找出延长点坐标 extend_price = self.back_info[point_type][ 0] * high_low[1] + self.back_info[point_type][1] other_extend_price = self.back_info[ other_point_type][0] * high_low[ 1] + self.back_info[other_point_type][1] #先记录此次旗形信息 self.flag_info.append( self.points[point_type][0][0]) #阻力线起始点时间戳索引 self.flag_info.append(0 if point_type == "high" else 1) #阻力线起始点类型0为最高值 1为最低值 self.flag_info.append( self.points[point_type][1][0]) #阻力线起始点时间戳 self.flag_info.append( self.points[point_type][2][0]) #阻力线起始点价格 if len(self.temp_points[point_type][0]) > 0: self.flag_info.append( self.temp_points[point_type][1][0]) self.flag_info.append( self.temp_points[point_type][2][0]) else: self.flag_info.append( self.points[point_type][1][0]) self.flag_info.append( self.points[point_type][2][0]) self.flag_info.append(high_low[2]) #阻力线结束点时间戳 self.flag_info.append(extend_price) #阻力线结束点价格 self.flag_info.append(self.points[other_point_type] [1][0]) #阻力线起始点时间戳 self.flag_info.append( self.points[other_point_type][2][0]) #阻力线起始点价格 if len(self.temp_points[other_point_type][0]) > 0: self.flag_info.append( self.temp_points[other_point_type][1][0]) self.flag_info.append( self.temp_points[other_point_type][2][0]) else: self.flag_info.append( self.points[other_point_type][1][0]) self.flag_info.append( self.points[other_point_type][2][0]) self.flag_info.append(high_low[2]) #阻力线结束点时间戳 self.flag_info.append( other_extend_price) #阻力线结束点价格 self.flag_info.append( self.back_info[point_type][0]) #阻力线斜率 self.flag_info.append( self.back_info[point_type][1]) #阻力线截距 #若上一次旗形为空,即此次旗形并非突破而来,则从阻力起始点开始计算经过距离 if len(self.last_back_info) == 0: self.flag_info.append( self.points[other_point_type][0][ other_points_len - 1] - self.points[point_type][0][0]) #阻力线经过时间戳数量 self.flag_info.append( self.points[point_type][2][0] - self.points[other_point_type][2][ other_points_len - 1]) #阻力线经过价格 #否则以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 else: break_point_price = self.last_back_info[ 0] * self.points[other_point_type][0][ 0] + self.last_back_info[1] self.flag_info.append( self.points[other_point_type][0][ other_points_len - 1] - self.points[other_point_type][0][0]) self.flag_info.append( break_point_price - self.points[other_point_type][2][ other_points_len - 1]) result_len = len(self.results) if result_len > 0: self.results[result_len - 1][ 18] = self.points[other_point_type][0][ other_points_len - 1] - self.points[other_point_type][ 0][0] self.results[result_len - 1][ 19] = break_point_price - self.points[ other_point_type][2][ other_points_len - 1] self.flag_info.append(0) #阻力线突破后经过时间戳数量(暂空) self.flag_info.append(0) #阻力线突破后经过价格(暂空) #将此次阻力线信息存入集合 self.results.append(self.flag_info) #清空阻力线信息以便下次接收 self.flag_info = [] #记录此次线性回归方程斜率和截距 self.last_back_info = [] self.last_back_info.extend( self.back_info[point_type]) else: #上一次旗形非空,以上一阻力线突破点计算经过距离,突破点由第一低点横坐标代入上一旗形方程计算所得 if len(self.last_back_info) > 0: break_point_price = self.last_back_info[ 0] * self.points[other_point_type][0][ 0] + self.last_back_info[1] result_len = len(self.results) if result_len > 0: self.results[result_len - 1][ 18] = self.points[other_point_type][0][ other_points_len - 1] - self.points[other_point_type][ 0][0] self.results[result_len - 1][ 19] = break_point_price - self.points[ other_point_type][2][ other_points_len - 1] self.last_back_info = [] #此次旗形已结束,更新高低点集合,最新点为第一高点,原旗形最后一点为第一低点 self.points[point_type] = [[], [], []] self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) self.points[other_point_type][0] = self.points[ other_point_type][0][other_points_len - 1:] self.points[other_point_type][1] = self.points[ other_point_type][1][other_points_len - 1:] self.points[other_point_type][2] = self.points[ other_point_type][2][other_points_len - 1:] #突破发生则清空临时极值点集合 self.temp_points[point_type] = [[], [], []] self.temp_points[other_point_type] = [[], [], []] #清空此次方程斜率和截距 self.back_info[point_type] = [] self.back_info[other_point_type] = [] #若低点斜率向上则重新统计斜率 else: log.WriteLog("break", "cross:%u" % high_low[2]) self.temp_points[point_type] = [[], [], []] self.temp_points[point_type][0].append( self.points[point_type][0][points_len - 1]) self.temp_points[point_type][1].append( self.points[point_type][1][points_len - 1]) self.temp_points[point_type][2].append( self.points[point_type][2][points_len - 1]) self.temp_points[point_type][0].append(high_low[1]) self.temp_points[point_type][1].append(high_low[2]) self.temp_points[point_type][2].append(high_low[3]) self.back_info[point_type] = calc_average_back( self.temp_points[point_type][0], self.temp_points[point_type][2]) if self.back_info[point_type][0] > 0: self.change_turning = True self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) #若实际价格偏离目标价格3%以上,则判断该点不属于当前方程 elif point_type == "high" and (tar_price - high_low[3]) > tar_price * OFFSET_RATE \ or point_type == "low" and (high_low[3] - tar_price) > tar_price * OFFSET_RATE: #无论斜率方向如何,方程斜率变低,需去除旧的高点,用最后一个旧高点与最新一点做线性回归运算 self.temp_points[point_type] = [[], [], []] self.temp_points[point_type][0].append( self.points[point_type][0][points_len - 1]) self.temp_points[point_type][1].append( self.points[point_type][1][points_len - 1]) self.temp_points[point_type][2].append( self.points[point_type][2][points_len - 1]) self.temp_points[point_type][0].append(high_low[1]) self.temp_points[point_type][1].append(high_low[2]) self.temp_points[point_type][2].append(high_low[3]) self.back_info[point_type] = calc_average_back( self.temp_points[point_type][0], self.temp_points[point_type][2]) #只有在斜率大于0的情况下变小或斜率小于0的情况下变大,才可能出现斜率转向 if point_type == "high" and a > 0 and self.back_info[point_type][0] < 0 \ or point_type == "low" and a < 0 and self.back_info[point_type][0] > 0: self.change_turning = True self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) #若实际价格未偏离目标的3%以上,则加入线性回归方程计算 else: self.points[point_type][0].append(high_low[1]) self.points[point_type][1].append(high_low[2]) self.points[point_type][2].append(high_low[3]) #若经过斜率偏离的重新计算,则以高点临时集合进行计算 if len(self.temp_points[point_type][0]) > 0: self.temp_points[point_type][0].append(high_low[1]) self.temp_points[point_type][1].append(high_low[2]) self.temp_points[point_type][2].append(high_low[3]) self.back_info[point_type] = calc_average_back( self.temp_points[point_type][0], self.temp_points[point_type][2]) #否则在原先的高点集合中计算 else: self.back_info[point_type] = calc_average_back( self.points[point_type][0], self.points[point_type][2]) #此种情况下可以出现任意斜率转向的情况 if a > 0 and self.back_info[point_type][ 0] < 0 or a < 0 and self.back_info[point_type][0] > 0: self.change_turning = True
def ProcessAfterProcess(self, high_low): #考虑线性回归方程斜率的各种情况,筛选出突破后未能形成旗形的情况 #1.高点方程尚未成型而低点方程已成型 if len(self.back_info["high"]) == 0 and len(self.back_info["low"]) > 0: #若上一旗形线性回归方程与低点方程方向一致(在此情况下理论上只会出现上一旗形方程与低点方程 #斜率都小于0的情况),说明突破失败 if len(self.last_back_info) > 0: if self.last_back_info[0] < 0 and self.back_info["low"][0] < 0: #填充上一旗形突破距离 high_len = len(self.points["high"][0]) break_point_price = self.last_back_info[0] * self.points[ "high"][0][high_len - 1] + self.last_back_info[1] #记录未转折就失败信息 log.WriteLog( "break", "unsucc_point high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) result_len = len(self.results) self.results[result_len - 1][18] = 0 self.results[result_len - 1][19] = self.points["high"][2][ high_len - 1] - break_point_price #清空清空方程斜率和截距 self.last_back_info = [] #2.低点方程未成型而高点方程已成型 elif len(self.back_info["low"]) == 0 and len( self.back_info["high"]) > 0: #若上一旗形线性回归方程与高点方程方向一致(在此情况下理论上只会出现上一旗形方程与低点方程 #斜率都大于0的情况),说明突破失败 if len(self.last_back_info) > 0: if self.last_back_info[0] > 0 and self.back_info["high"][0] > 0: #填充上一旗形突破距离 low_len = len(self.points["low"][0]) break_point_price = self.last_back_info[0] * self.points[ "low"][0][low_len - 1] + self.last_back_info[1] #记录未转折就失败信息 log.WriteLog( "break", "unsucc_point low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) result_len = len(self.results) self.results[result_len - 1][18] = 0 self.results[result_len - 1][19] = break_point_price - self.points[ "low"][2][low_len - 1] #清空清空方程斜率和截距 self.last_back_info = [] #3.高低点方程都已成型,但方向不一致 elif len(self.back_info["low"]) > 0 and len( self.back_info["high"]) > 0: if self.back_info["low"][0] > 0 and self.back_info["high"][ 0] < 0 or self.back_info["low"][0] < 0 and self.back_info[ "high"][0] > 0: #区分没有出现中途转向的情况和出现中途转向的情况 #中途转向(说明突破后旗形发展了一段时间) if self.change_turning: if len(self.last_back_info) > 0: #上一旗形向上的情况下 if self.last_back_info[0] > 0: #填充上一旗形突破距离 low_len = len(self.points["low"][0]) break_point_price = self.last_back_info[ 0] * self.points["low"][0][ 0] + self.last_back_info[1] #记录旗形成型转折失败信息 log.WriteLog( "break", "unsucc_change low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) #既然转向,则导致转向的那个点必然高于倒数第二点 result_len = len(self.results) self.results[result_len - 1][18] = self.points["low"][0][ low_len - 2] - self.points["low"][0][0] self.results[ result_len - 1][19] = break_point_price - self.points[ "low"][2][low_len - 2] self.temp_points["high"] = [[], [], []] self.temp_points["low"] = [[], [], []] high_len = len(self.points["high"][0]) self.points["high"][0] = self.points["high"][0][ high_len - 1:] self.points["high"][1] = self.points["high"][1][ high_len - 1:] self.points["high"][2] = self.points["high"][2][ high_len - 1:] self.points["low"][0] = self.points["low"][0][ low_len - 2:] self.points["low"][1] = self.points["low"][1][ low_len - 2:] self.points["low"][2] = self.points["low"][2][ low_len - 2:] self.back_info["low"] = calc_average_back( self.points["low"][0], self.points["low"][2]) self.back_info["high"] = [] #上一旗形向下的情况下 elif self.last_back_info[0] < 0: #填充上一旗形突破距离 high_len = len(self.points["high"][0]) break_point_price = self.last_back_info[ 0] * self.points["high"][0][ 0] + self.last_back_info[1] #记录旗形成型转折失败信息 log.WriteLog( "break", "unsucc_change high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) #既然转向,则导致转向的那个点必然低于倒数第二点 result_len = len(self.results) self.results[result_len - 1][18] = self.points["high"][0][ high_len - 2] - self.points["high"][0][0] self.results[result_len - 1][19] = self.points["high"][2][ high_len - 2] - break_point_price self.temp_points["high"] = [[], [], []] self.temp_points["low"] = [[], [], []] low_len = len(self.points["low"][0]) self.points["low"][0] = self.points["low"][0][ low_len - 1:] self.points["low"][1] = self.points["low"][1][ low_len - 1:] self.points["low"][2] = self.points["low"][2][ low_len - 1:] self.points["high"][0] = self.points["high"][0][ high_len - 2:] self.points["high"][1] = self.points["high"][1][ high_len - 2:] self.points["high"][2] = self.points["high"][2][ high_len - 2:] self.back_info["high"] = calc_average_back( self.points["high"][0], self.points["high"][2]) self.back_info["low"] = [] #清空清空方程斜率和截距 self.last_back_info = [] #并非中途转向(说明旗形一开始就未成型) else: if len(self.last_back_info) > 0: #上一旗形向上的情况下 if self.last_back_info[0] > 0: #填充上一旗形突破距离 low_len = len(self.points["low"][0]) break_point_price = self.last_back_info[ 0] * self.points["low"][0][ 0] + self.last_back_info[1] #记录旗形未成型转折失败信息 log.WriteLog( "break", "unsucc_line low\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) result_len = len(self.results) self.results[result_len - 1][18] = 0 self.results[ result_len - 1][19] = break_point_price - self.points[ "low"][2][0] high_len = len(self.points["high"][0]) self.points["high"][0] = self.points["high"][0][ high_len - 1:] self.points["high"][1] = self.points["high"][1][ high_len - 1:] self.points["high"][2] = self.points["high"][2][ high_len - 1:] self.back_info["high"] = [] #上一旗形向下的情况下 elif self.last_back_info[0] < 0: #填充上一旗形突破距离 high_len = len(self.points["high"][0]) break_point_price = self.last_back_info[ 0] * self.points["high"][0][ 0] + self.last_back_info[1] #记录旗形未成型转折失败信息 log.WriteLog( "break", "unsucc_line high\ttime_idx:%u\ttime:%u\treal:%f\ttar:%f\tlast_slope:%f\tlast_intercept:%f" % (high_low[1], high_low[2], high_low[3], break_point_price, self.last_back_info[0], self.last_back_info[1])) result_len = len(self.results) self.results[result_len - 1][18] = 0 self.results[result_len - 1][19] = self.points[ "high"][2][0] - break_point_price low_len = len(self.points["low"][0]) self.points["low"][0] = self.points["low"][0][ low_len - 1:] self.points["low"][1] = self.points["low"][1][ low_len - 1:] self.points["low"][2] = self.points["low"][2][ low_len - 1:] self.back_info["low"] = [] #清空清空方程斜率和截距 self.last_back_info = [] else: #只需考虑转向后高低线性回归方程斜率变一致的情况 if self.change_turning: if high_low[0] == "high": high_len = len(self.points["high"][0]) self.points["high"][0] = self.points["high"][0][ high_len - 2:] self.points["high"][1] = self.points["high"][1][ high_len - 2:] self.points["high"][2] = self.points["high"][2][ high_len - 2:] self.back_info["high"] = calc_average_back( self.points["high"][0], self.points["high"][2]) low_len = len(self.points["low"][0]) self.points["low"][0] = self.points["low"][0][low_len - 2:] self.points["low"][1] = self.points["low"][1][low_len - 2:] self.points["low"][2] = self.points["low"][2][low_len - 2:] self.back_info["low"] = calc_average_back( self.points["low"][0], self.points["low"][2]) #记录旗形重新成型信息 log.WriteLog( "break", "resucc high\ttime:%u\ttime:%u\treal:%f\tslope:%f\tintercept:%f" % (high_low[1], high_low[2], high_low[3], self.back_info["low"][0], self.back_info["low"][1])) elif high_low[0] == "low": low_len = len(self.points["low"][0]) self.points["low"][0] = self.points["low"][0][low_len - 2:] self.points["low"][1] = self.points["low"][1][low_len - 2:] self.points["low"][2] = self.points["low"][2][low_len - 2:] self.back_info["low"] = calc_average_back( self.points["low"][0], self.points["low"][2]) high_len = len(self.points["high"][0]) self.points["high"][0] = self.points["high"][0][ high_len - 2:] self.points["high"][1] = self.points["high"][1][ high_len - 2:] self.points["high"][2] = self.points["high"][2][ high_len - 2:] self.back_info["high"] = calc_average_back( self.points["high"][0], self.points["high"][2]) #记录旗形重新成型信息 log.WriteLog( "break", "resucc low\ttime:%u\ttime:%u\treal:%f\tslope:%f\tintercept:%f" % (high_low[1], high_low[2], high_low[3], self.back_info["high"][0], self.back_info["high"][1])) self.temp_points["high"] = [[], [], []] self.temp_points["low"] = [[], [], []] self.last_back_info = []
def add_up(this, id, is_testCase=False): open_time = this.get_open_times(id) win_time, win_point = this.get_win_situation(id) loss_time, loss_point = this.get_loss_situation(id) win_ratio = win_time / float(win_time + loss_time) #胜率 win_loss_ratio = 0 #盈亏比 if abs(loss_point) * win_time != 0: win_loss_ratio = (win_point * loss_time) / float( abs(loss_point) * win_time) max_win_point = this.get_max_win_point(id) max_loss_point = this.get_max_loss_point(id) df_index = this.df_records['profit'].notnull() if id is not None: df_index = df_index & (this.df_records['id'] == id) df_datas = this.df_records[df_index] total_profit = 0 max_total_profit = -99999 max_retrace = 0 max_constant_win_time = 0 #最大连胜次数 max_constant_win_point = 0 #最大连胜点数 max_constant_loss_time = 0 #最大连亏次数 max_constant_loss_point = 0 #最大连亏点数 constant_win_time = 0 constant_win_point = 0 constant_loss_time = 0 constant_loss_point = 0 for profit in df_datas['profit']: if profit > 0: constant_win_time += 1 max_constant_win_time = max(max_constant_win_time, constant_win_time) constant_loss_time = 0 constant_win_point += profit max_constant_win_point = max(max_constant_win_point, constant_win_point) constant_loss_point = 0 elif profit < 0: constant_loss_time += 1 max_constant_loss_time = max(max_constant_loss_time, constant_loss_time) constant_win_time = 0 constant_loss_point += profit max_constant_loss_point = min(max_constant_loss_point, constant_loss_point) constant_win_point = 0 else: constant_win_time = 0 constant_win_point = 0 constant_loss_time = 0 constant_loss_point = 0 total_profit += profit max_total_profit = max(max_total_profit, total_profit) retrace = 0 if max_total_profit != 0: retrace = (max_total_profit - total_profit) / float( abs(max_total_profit)) max_retrace = max(max_retrace, retrace) log.WriteLog(LOG_SUMMARY_REPORT, u"盈利点数:%d" % total_profit) log.WriteLog(LOG_SUMMARY_REPORT, u"开仓次数:%d" % open_time) log.WriteLog(LOG_SUMMARY_REPORT, u"盈利次数:%d 盈利点数%.2f" % (win_time, win_point)) log.WriteLog(LOG_SUMMARY_REPORT, u"亏损次数:%d 亏损点数%.2f" % (loss_time, loss_point)) log.WriteLog(LOG_SUMMARY_REPORT, u"胜率:%.2f 盈亏比%.2f" % (win_ratio, win_loss_ratio)) log.WriteLog( LOG_SUMMARY_REPORT, u"单笔最大收益点数:%.2f 单笔最大亏损点数%.2f" % (max_win_point, max_loss_point)) log.WriteLog( LOG_SUMMARY_REPORT, u"最大连胜次数:%d 最大连胜点数%.2f" % (max_constant_win_time, max_constant_win_point)) log.WriteLog( LOG_SUMMARY_REPORT, u"最大连亏次数:%.2f 最大连亏点数%.2f" % (max_constant_loss_time, max_constant_loss_point)) log.WriteLog(LOG_SUMMARY_REPORT, u"最大回撤:%.2f%%" % (max_retrace * 100)) if not is_testCase: return if total_profit != 102 or open_time !=3 or win_time !=2 or win_point != 152 or loss_time != 1 or loss_point != -50 \ or round(win_ratio,2) !=0.67 or win_loss_ratio !=1.52 or max_win_point != 126 or max_loss_point !=-50 \ or max_constant_win_time != 2 or max_constant_win_point != 152 or max_constant_loss_time != 1 or max_constant_loss_point !=-50\ or max_retrace !=0: raise ValueError('error function add_up')