def on_bar_minx(self, bar): self.am.updateBar(bar) if not self.am.inited: return # 涨跌停时暂停交易 if bar.close == bar.UpperLimitPrice: self.pause = True to_log(self.vtSymbol + ' 已涨停,该品种dali策略暂停交易') if bar.close == bar.LowerLimitPrice: self.pause = True to_log(self.vtSymbol + ' 已跌停,该品种dali策略暂停交易') if self.paused == True and self.backtest == False: self.counter += 1 if self.counter == 3: filename = get_dss() + 'gateway_closed.csv' if os.path.exists(filename): pass else: send_email(get_dss(), self.vtSymbol + ' 挂单未成交!!!', '') return self.calculateIndicator() # 计算指标 self.generateSignal(bar) # 触发信号,产生交易指令
def __init__(self): """Constructor""" self.dss = get_dss() self.portfolio = None # 一对一 self.portfolioValue = 100E4 self.startDt = None self.endDt = None self.currentDt = None self.barDict = OrderedDict() self.min1barDict = OrderedDict() self.min5barDict = OrderedDict() self.min15barDict = OrderedDict() self.min30barDict = OrderedDict() self.result = None self.resultList = [] self.tradeDict = OrderedDict() # 加载配置 config = open(get_dss() + 'fut/cfg/config.json') setting = json.load(config) symbols = setting['symbols'] self.vtSymbol_list = symbols.split(',')
def smile_symbol(): """期权微笑曲线""" now = datetime.now() # today = now.strftime('%Y-%m-%d %H:%M:%S') today = now.strftime('%Y-%m-%d') # today = '2020-06-22' fn = get_dss() + 'opt/' + today[:7] + '_sigma.csv' df = pd.read_csv(fn) df = df[df.date == today] df = df.drop_duplicates(subset=['term'], keep='last') # print(df.head()) fn = get_dss() + 'fut/cfg/opt_mature.csv' df_IO = pd.read_csv(fn) df_IO = df_IO[df_IO.flag == df_IO.flag] # 筛选出不为空的记录 df = df[df.term.isin(list(df_IO.symbol))] print(df.term) for i, row in df.iterrows(): c_curve_dict = eval(row.c_curve) p_curve_dict = eval(row.p_curve) df1 = pd.DataFrame([c_curve_dict, p_curve_dict]) df1 = df1.T df1.columns = ['call', 'put'] df1.plot() plt.title(today + '_' + row.term) fn = 'static/smile_' + row.term + '.jpg' plt.savefig(fn)
def fresh_focus(): all = 0 now = datetime.now() today = now.strftime('%Y-%m-%d') dirname = get_dss() + 'fut/engine/focus/' listfile = os.listdir(dirname) for fn in listfile: if fn.startswith('focus'): fn = dirname + fn df = pd.read_csv(fn) if len(df) == 0: continue rec = df.iloc[-1,:] book_list = eval(rec.book_list) pnl = 0 for book in book_list: fn1 = get_dss() + 'fut/engine/book/book_' + book + '.csv' if os.path.exists(fn1) == False: continue df1 = pd.read_csv(fn1) row = df1.iloc[-1,:] pnl += row.pnl rec.datetime = today + ' 15:00:00' rec.pnl = pnl df = pd.DataFrame([rec]) df.to_csv(fn, index=False, header=None, mode='a') all += pnl df = pd.DataFrame([[today, all]], columns=['date','pnl']) fn = get_dss() + 'fut/engine/focus/all.csv' df.to_csv(fn, index=False, header=None, mode='a')
def warehouse_product(kind, p_list): dirname = get_dss() + 'info/warehouse/img/' fn = os.path.join(get_dss(), 'info/warehouse/warehouse_' + kind + '.csv') df_a = pd.read_csv(fn) date_list = sorted(set(df_a.date)) date_list = date_list[-60:] # print(date_list) for p in p_list: df = df_a[(df_a.date.isin(date_list)) & (df_a.symbol == p)] df = df.set_index('date') df = df.sort_index() # print(df.head()) df0 = df.copy() df0['value'] = df0['receipt'] df0.index.name = 'receipt' if kind == 'czce': df1 = df.copy() df1['value'] = df1['forecast'] df1.index.name = 'forecast' show_21(dirname, [df0], [df1], p, False, p) else: show_11(dirname, [df0], p, False, p) # 生成pdf listfile = os.listdir(dirname) img_list = [] for fn in listfile: img_list.append(fn) img2pdf(dirname, 'warehouse_' + kind + '.pdf', img_list)
def pcp(): r = 0.03 T = 15/365 S = 3912 result = [] dt = '2020-04-30' x_list = range(3150,4700,50) # print(*s_list) fn = get_dss() + 'backtest/IO/' + 'IO' + year + '.csv' df = pd.read_csv(fn) # print(df.head()) for x in x_list: symbol_c = 'IO2005-C-' + str(x) symbol_p = 'IO2005-P-' + str(x) # print(symbol_c) df_c = df[(df.date == dt) & (df.symbol == symbol_c)] df_p = df[(df.date == dt) & (df.symbol == symbol_p)] row_c = df_c.iloc[0,:] row_p = df_p.iloc[0,:] pSc = int( row_p.close + S -row_c.close ) pSc_final = int( pSc * (1 + r*T) ) result.append( [S, row_c.close, row_p.close, x, pSc, pSc_final, x-pSc_final] ) df = pd.DataFrame(result, columns=['S', 'call', 'put', 'X', 'pSc', 'pSc_final', 'diff']) fn = get_dss() + 'backtest/IO/p1.csv' df.to_csv(fn, index=False)
def extract_trade(): """从成交反馈文件中提取最新成交记录,追加到got_trade文件中""" # 提取当前日期下的成交记录 now = datetime.now() today = int(now.strftime('%Y%m%d')) # today = 20201217 fn = get_dss() + 'fut/engine/gateway_trade.csv' df = pd.read_csv(fn) df = df[df.TradingDay >= today] # 当日交易记录 df = df.drop_duplicates() # print(df) n1 = len(df) if n1 == 0: return # 对数据进行预处理,符合文件字段的要求 date_list = list(df.TradingDay.astype('str')) date_list = [x[:4] + '-' + x[4:6] + '-' + x[6:8] for x in date_list] df['TradingDay'] = date_list del df['SysID'] df['wipe'] = 'no' df['before'] = '' df['current'] = 'secure' df['got'] = 'no' df['seq'] = 0 # 在原文件的基础上合并新的成交记录,去重后重新写入文件 fn0 = get_dss() + 'fut/engine/book/got_trade.csv' df0 = pd.read_csv(fn0) df0 = pd.concat([df0, df], sort=False) df0 = df0.drop_duplicates(subset=['OrderID', 'TradeID', 'TradingDay'], keep='first') df0['seq'] = range(1, len(df0) + 1) df0.to_csv(fn0, index=False)
def calc_d_base(date): # 计算并保存d_base r = [] symbol_list = get_symbols_quote() for symbol in symbol_list: if symbol[:2] != 'IO': continue try: basic = symbol[:6] strike = get_contract(symbol).strike symbol_c = basic + '-C-' + str(strike) symbol_p = basic + '-P-' + str(strike) fn = get_dss() + 'fut/bar/day_' + symbol_c + '.csv' df_c = pd.read_csv(fn) fn = get_dss() + 'fut/bar/day_' + symbol_p + '.csv' df_p = pd.read_csv(fn) d_base = df_c.iat[-1, 5] + df_p.iat[-1, 5] r.append([date, basic, strike, d_base]) except Exception as e: # s = traceback.format_exc() # to_log(s) pass df = pd.DataFrame(r, columns=['date', 'basic', 'strike', 'd_base']) fn = get_dss() + 'opt/sdiffer_d_base.csv' if os.path.exists(fn): df.to_csv(fn, index=False, mode='a', header=False) else: df.to_csv(fn, index=False)
def smile(): """期权微笑曲线""" fn = get_dss() + 'fut/cfg/opt_mature.csv' df_opt = pd.read_csv(fn) # for pz in set(df_opt.pz): for pz in ['IO']: print(pz) df = df_opt[(df_opt.pz == pz) & (df_opt.flag == df_opt.flag)] # 筛选出不为空的记录 df = df.sort_values('symbol') symbol_list = list(df.symbol) cur_if = 'IF' + symbol_list[0][2:] # print(cur_if) fn = get_dss() + 'fut/bar/day_' + cur_if + '.csv' df = pd.read_csv(fn) row = df.iloc[-1, :] strike = row.open * 0.5 + row.close * 0.5 strike = int(round(strike / 1E4, 2) * 1E4) atm = strike # 获得平值 # print(atm) smile_pz(pz, symbol_list, atm, 'call') smile_pz(pz, symbol_list, atm, 'put') smile_pz(pz, symbol_list, atm, 'avg')
def price_signal(dss, day): df = get_daily(dss, day) # print(df.head(3)) # print(len(df)) df1 = df[df.p_change > 6.18] # print(len(df1)) # print(df1.head(3)) r = [] for i, row in df1.iterrows(): # print(row.code, row['name'], row.p_change) if be_bottom(row.code, day): df2 = pro.concept_detail(ts_code=get_ts_code(row.code)) concept = str(df2.concept_name.tolist()) r.append([day, row.code, row['name'], row.p_change, concept]) # 拷贝数据文件到下载目录,以备下载。windows下一定要用\\ ins = 'copy ' + get_dss( ) + 'hfq\\' + row.code + '.csv ' + 'C:\\Users\\Administrator\\Downloads\\' + row.code + '_' + row[ 'name'] + '.csv ' print(ins) os.system(ins) #break df = pd.DataFrame(r, columns=['date', 'code', 'name', 'change', 'concept']) fn = get_dss() + 'csv/price_signal.csv' df.to_csv(fn, index=False, mode='a') return r
def pandian_dali(today): # 读取value_dali文件 fn_value = get_dss() + 'fut/engine/star/value_dali.csv' if os.path.exists(fn_value): df_value = pd.read_csv(fn_value) else: return fn_param = get_dss() + 'fut/engine/dali/signal_dali_param.csv' df_param = pd.read_csv(fn_param) # df_param = df_param.set_index('pz') pz_list = list(df_param.pz) print(pz_list) r = [] for pz in pz_list: # 更新该品种的最新数据 fn_signal = get_dss() + 'fut/engine/dali/signal_dali_' +'multi'+ '_var_' + pz + '.csv' if os.path.exists(fn_signal): df = pd.read_csv(fn_signal) rec = df.iloc[-1,:] # print(rec) cur_value = int(rec.pnl_net) net_pos = rec.unit multi_duo_list = eval(rec.price_duo_list) multi_kong_list = eval(rec.price_kong_list) r.append( [today,pz,cur_value,net_pos,multi_duo_list,multi_kong_list] ) df = pd.DataFrame(r) df.to_csv(fn_value, index=False, mode='a', header=None)
def fut(): symbol = 'ag2006' filename = get_dss() + 'fut/put/min1_' + symbol + '.csv' df = pd.read_csv(filename, dtype='str') r_q = [list(df.columns)] for i, row in df.iterrows(): r_q.append(list(row)) filename = get_dss() + 'fut/put/rec/min5_' + symbol + '.csv' df = pd.read_csv(filename, dtype='str') r_t = [list(df.columns)] row = df.iloc[-1, :] r_t.append(list(row)) filename = get_dss() + 'fut/engine/engine_deal.csv' df = pd.read_csv(filename, dtype='str') r = [] for i, row in df.iterrows(): r.append(list(row)) r.append(list(df.columns)) r = list(reversed(r)) r = r[:12] return render_template("fut.html", title=symbol, rows_q=r_q, rows_t=r_t, rows=r)
def calc_die(): now = datetime.now() today = now.strftime('%Y-%m-%d') today = '2020-06-12' fn = get_dss() + 'opt/' + today[:7] + '.csv' df = pd.read_csv(fn) df = df[df.Localtime > today+' 11:00:00'] df = df[df.Localtime < today+' 12:00:00'] df = df.set_index('Instrument') r = [] if len(df) > 0: r += die_IO(df, today) r += die_m(df, today) r += die_RM(df, today) r += die_MA(df, today) r += die_CF(df, today) df2 = pd.DataFrame(r, columns=['date', 'term', 'c/p', 's1', 'c1', 's2', 'c2', 's3', 'c3', 'cost']) df2 = df2[(df2.cost < 0) & (df2.cost != float('-inf'))] fn = get_dss() + 'opt/die.csv' df2.to_csv(fn, index=False) return True else: return False
def calc_pcp(): now = datetime.now() today = now.strftime('%Y-%m-%d') # today = '2020-06-12' # fn = get_dss() + 'opt/' + today[:7] + '_greeks.csv' fn = get_dss() + 'opt/' + today[:7] + '.csv' df = pd.read_csv(fn) df = df[df.Localtime > today+' 11:00:00'] df = df[df.Localtime < today+' 12:00:00'] df = df.set_index('Instrument') r = [] if len(df) > 0: r += pcp_IO(df, today) r += pcp_m(df, today) r += pcp_RM(df, today) r += pcp_MA(df, today) r += pcp_CF(df, today) fn = get_dss() + 'opt/pcp.csv' df2 = pd.DataFrame(r, columns=['date', 'type', 'term', 'X', 'S', 'call', 'put', 'pSc', 'diff', 'rt']) df2 = df2.sort_values('type') df2.to_csv(fn, index=False) return True else: return False
def get_trade(): """从成交反馈文件中提取期权成交记录,追加到opt_trade文件中""" now = datetime.now() today = int(now.strftime('%Y%m%d')) fn = get_dss() + 'fut/engine/gateway_trade.csv' df = pd.read_csv(fn) df = df[df.TradingDay == today] # 当日交易记录 df = df[df.InstrumentID.str.len() >= 9] # 期权合约 # print(df) if len(df) > 0: df = df.drop_duplicates() del df['OrderID'] del df['SysID'] df['book'] = '' df['portfolio'] = '' df['margin'] = 0 date_list = list(df.TradingDay.astype('str')) date_list = [x[:4] + '-' + x[4:6] + '-' + x[6:8] for x in date_list] df['TradingDay'] = date_list # print(df) fn = get_dss() + 'fut/engine/opt/opt_trade.csv' if os.path.exists(fn): df.to_csv(fn, index=False, header=None, mode='a') else: df.to_csv(fn, index=False)
def compass(date): listfile = os.listdir(dirname) for fn in listfile: os.remove(dirname + fn) d2 = get_dss() + 'info/hold/img/' listfile = os.listdir(d2) for fn in listfile: os.remove(d2 + fn) fn = get_dss() + 'fut/cfg/opt_mature.csv' df2 = pd.read_csv(fn) df2 = df2[pd.notnull(df2.flag)] fn_list = [] for func in [CF, m, RM, al, IO]: # for func in [m]: try: fn_list += func(date, df2) except Exception as e: s = traceback.format_exc() to_log(s) # print(fn_list) send_email(get_dss(), 'compass', '', fn_list)
def proc_rec_mature(rec, fname): dss = get_dss() pos_dict = eval(rec.posDict) close_dict = eval(rec.closeDict) now = datetime.now() today = now.strftime('%Y-%m-%d') rec.date = today s_list = [] # 对于已到期的期权合约(IO除外),将持仓置为0,并清理;若有行权,添加行权记录到got_trade文件 for symbol in pos_dict: pos = pos_dict[symbol] c = get_contract(symbol) # 目前期货合约暂不处理(返回值为None);回头也返回一个到期日,过期作清零处理 if c.mature is not None: # 到期的,都要进行清零处理 if c.mature <= today: s_list.append(symbol) if c.be_opt and symbol[:2] != 'IO' and pos != 0: size = c.size if c.exchangeID == 'CZCE': # tick数据的特点,需对郑商所数据进行特殊处理 size = 1 settle = get_tick(c.basic)['AveragePrice'] / size fn = get_dss() + 'fut/engine/book/got_trade.csv' df = pd.read_csv(fn) exercise = [ '', c.exchangeID, c.basic, 'Open', int(time.time()), round(settle, 4), int(time.time()), '15:00:01', today, abs(pos), 'no', '', 'hedge' if 'hedge' in fname else 'secure', 'no', len(df) + 1 ] if (pos > 0 and c.opt_flag == 'C' and settle >= c.strike ) or \ (pos < 0 and c.opt_flag == 'P' and settle <= c.strike ): # 以strike作为价格买开仓basic exercise[0] = 'Buy' if (pos > 0 and c.opt_flag == 'P' and settle <= c.strike ) or \ (pos < 0 and c.opt_flag == 'C' and settle >= c.strike ): # 以strike作为价格卖开仓basic exercise[0] = 'Sell' if exercise[0] != '': df = pd.DataFrame([exercise]) df.to_csv(fn, index=False, header=None, mode='a') if s_list != []: # 清零处理 for s in s_list: if s in pos_dict: pos_dict.pop(s) if s in close_dict: close_dict.pop(s) rec.posDict = str(pos_dict) rec.closeDict = str(close_dict) df = pd.DataFrame([rec]) df.to_csv(fname, index=False, header=None, mode='a')
def save_sdiffer(date, date_pre, basic_m0, basic_m1, atm, stat): fn = get_dss() + 'fut/bar/min5_' + basic_m0 + '-C-' + str(atm) + '.csv' df_m0_c = pd.read_csv(fn) df_m0_c_pre = df_m0_c[df_m0_c.date == date_pre] df_m0_c = df_m0_c[df_m0_c.date == date] fn = get_dss() + 'fut/bar/min5_' + basic_m0 + '-P-' + str(atm) + '.csv' df_m0_p = pd.read_csv(fn) df_m0_p_pre = df_m0_p[df_m0_p.date == date_pre] df_m0_p = df_m0_p[df_m0_p.date == date] fn = get_dss() + 'fut/bar/min5_' + basic_m1 + '-C-' + str(atm) + '.csv' if os.path.exists(fn) == False: return df_m1_c = pd.read_csv(fn) df_m1_c_pre = df_m1_c[df_m1_c.date == date_pre] df_m1_c = df_m1_c[df_m1_c.date == date] if len(df_m1_c_pre) == 0: return fn = get_dss() + 'fut/bar/min5_' + basic_m1 + '-P-' + str(atm) + '.csv' df_m1_p = pd.read_csv(fn) df_m1_p_pre = df_m1_p[df_m1_p.date == date_pre] df_m1_p = df_m1_p[df_m1_p.date == date] # print(basic_m0, basic_m1) # print(atm) # print(df_m1_c_pre.head()) # print(df_m1_p_pre.head()) d_base_m1 = df_m1_c_pre.iat[-1, 5] + df_m1_p_pre.iat[-1, 5] d_base_m0 = df_m0_c_pre.iat[-1, 5] + df_m0_p_pre.iat[-1, 5] df_m1_c = df_m1_c.reset_index() df_m1_p = df_m1_p.reset_index() df_m0_c = df_m0_c.reset_index() df_m0_p = df_m0_p.reset_index() df_m1_c['diff_m1'] = df_m1_c.close + df_m1_p.close - d_base_m1 df_m0_c['diff_m0'] = df_m0_c.close + df_m0_p.close - d_base_m0 df_m1_c['differ'] = df_m1_c.diff_m1 - df_m0_c.diff_m0 df_m1_c['pz'] = 'IO' df_m1_c['basic_m0'] = basic_m0 df_m1_c['basic_m1'] = basic_m1 df_m1_c['atm'] = atm df_m1_c['diff_m0'] = df_m0_c['diff_m0'] df_m1_c['stat'] = stat df2 = df_m1_c[[ 'date', 'time', 'pz', 'basic_m0', 'basic_m1', 'atm', 'diff_m0', 'diff_m1', 'differ', 'stat' ]] # print(df2.head()) fn = get_dss() + 'opt/straddle_differ.csv' if os.path.exists(fn): df2.to_csv(fn, index=False, mode='a', header=False) else: df2.to_csv(fn, index=False)
def examine(): dss = get_dss() # setting_pz中的品种已在trade_time中维护 #读取交易时段文件 fn = dss + 'fut/cfg/trade_time.csv' df = pd.read_csv(fn) tm_pz_set = set(df.symbol) #print(tm_pz_set) fn = dss + 'fut/cfg/setting_pz.csv' df = pd.read_csv(fn) setting_pz_list = list(df.pz) #print(setting_pz_list) for pz in setting_pz_list: if pz not in tm_pz_set: to_log('examine: ' + pz + ' of setting_pz.csv not in trade_time.csv') # 加载配置,目前盯市的品种已在setting_pz中维护 symbols_quote_list = get_symbols_quote() for symbol in symbols_quote_list: c = get_contract(symbol) #print( c.pz ) if c is None: to_log('examine: ' + symbol + ' of config.json not in setting_pz.csv') # symbols_quote涵盖symbols_trade symbols_trade_list = get_symbols_trade() for symbol in symbols_trade_list: if symbol not in symbols_quote_list: to_log('examine: ' + symbol + ' of symbols_trade not in symbols_quote') # 行情接收器是否存在超时情况 config = open(dss + 'fut/cfg/config.json') setting = json.load(config) symbol = setting['symbols_quote_canary'] # symbols_quote_list = symbols.split(',') now = datetime.now() today = now.strftime('%Y-%m-%d') # today = '20200515' fn = get_dss() + 'fut/tick/tick_' + today + '_' + symbol + '.csv' if os.path.exists(fn): df = pd.read_csv(fn) df = df[(df.UpdateTime >= '14:50:59') & (df.UpdateTime <= '14:59:59')] df = df.sample(3) for i, row in df.iterrows(): u = datetime.strptime(row.UpdateDate + ' ' + row.UpdateTime, '%Y-%m-%d %H:%M:%S') u += timedelta(seconds=3) u = datetime.strftime(u, '%Y-%m-%d %H:%M:%S') # print(row.Localtime, u) if row.Localtime > u: to_log('examine: over time! ' + row.Localtime + ' > ' + u)
def iv_straddle_show(symbol, strike_list, startdate, kind): plt.figure(figsize=(12, 8)) for strike in strike_list: exchangeID = str(get_contract(symbol).exchangeID) if exchangeID in ['CFFEX', 'DCE']: s_a = symbol + '-C-' + strike s_b = symbol + '-P-' + strike else: s_a = symbol + 'C' + strike s_b = symbol + 'P' + strike if kind == 'daily': fn = get_dss() + 'fut/bar/min5_' + s_a + '.csv' else: fn = get_dss() + 'fut/put/rec/min5_' + s_a + '.csv' df_a = pd.read_csv(fn) if kind == 'daily': fn = get_dss() + 'fut/bar/min5_' + s_b + '.csv' else: fn = get_dss() + 'fut/put/rec/min5_' + s_b + '.csv' df_b = pd.read_csv(fn) df_a = df_a[df_a.date >= startdate] df_b = df_b[df_b.date >= startdate] assert len(df_a) == len(df_b) df_a['dt'] = df_a['date'] + ' ' + df_a['time'] df_a['value'] = df_a['close'] + df_b['close'] df_a = df_a.set_index('dt') # df_a['next'] = df_a['value'].shift(1) # df_a['value'] = np.log(df_a['value']) - np.log(df_a['next']) # df_a['value'] = df_a['value'].cumsum() # print(df_a.head()) plt.plot(df_a.value, label=strike) plt.xticks(rotation=90) plt.grid(True, axis='y') ax = plt.gca() for label in ax.get_xticklabels(): label.set_visible(False) for label in ax.get_xticklabels()[1::25]: label.set_visible(True) for label in ax.get_xticklabels()[-1:]: label.set_visible(True) plt.legend() fn = 'static/iv_straddle_show.jpg' plt.savefig(fn) plt.cla() r = '' fn = 'iv_straddle_show.jpg' now = str(int(time.time())) r = '<img src=\"static/' + fn + '?rand=' + now + '\" />' return r
def book_min5_now_show(startdate, dual_list): now = datetime.now() startdate = now.strftime('%Y-%m-%d') plt.figure(figsize=(12, 8)) for dual in dual_list: symbol_a = dual[0] num_a = int(dual[1]) symbol_b = dual[2] num_b = int(dual[3]) fn = get_dss() + 'fut/put/rec/min5_' + symbol_a + '.csv' if os.path.exists(fn) == False: continue df_a = pd.read_csv(fn) fn = get_dss() + 'fut/put/rec/min5_' + symbol_b + '.csv' if os.path.exists(fn) == False: continue df_b = pd.read_csv(fn) df_a = df_a[df_a.date >= startdate] df_b = df_b[df_b.date >= startdate] df_a = df_a.reset_index() df_b = df_b.reset_index() assert len(df_a) == len(df_b) # print(df_a.head()) # print(df_b.head()) df_a['dt'] = df_a['date'] + ' ' + df_a['time'] df_a['value'] = abs(num_a * df_a['close'] + num_b * df_b['close']) df_a = df_a.set_index('dt') # print(df_a.tail()) # print(df_b.tail()) plt.plot(df_a.value, label=symbol_a + ' ' + str(num_a) + ' ' + symbol_b + ' ' + str(num_b)) plt.xticks(rotation=90) plt.grid(True, axis='y') ax = plt.gca() for label in ax.get_xticklabels(): label.set_visible(False) for label in ax.get_xticklabels()[1::25]: label.set_visible(True) for label in ax.get_xticklabels()[-1:]: label.set_visible(True) plt.legend() fn = 'static/book_min5_now_show.jpg' plt.savefig(fn) plt.cla() r = '' fn = 'book_min5_now_show.jpg' now = str(int(time.time())) r = '<img src=\"static/' + fn + '?rand=' + now + '\" />' return r
def pandian_p(today): # 读取value_p文件 fn_value = get_dss() + 'fut/engine/value_p.csv' if os.path.exists(fn_value): df_value = pd.read_csv(fn_value) #print(df_value) else: return r = [] # p_list = [ 'dali', 'ic', 'opt', 'star', 'cui' ] p_list = ['dali', 'opt', 'star'] for p in p_list: df = df_value[df_value.p == p] # 获得该组合最近日期的一条记录 if len(df) > 0: rec = df.iloc[-1, :] capital = rec.capital date = rec.date if today[:4] == date[:4]: newyear_value = rec.newyear_value else: newyear_value = rec.cur_value # 换年 else: continue # 更新该品种的最新数据 fn = get_dss() + 'fut/engine/' + p + '/portfolio_' + p + '_var.csv' #fn = get_dss() + 'fut/engine/rsiboll/portfolio_rsiboll_var.csv' # print(fn) if os.path.exists(fn): df = pd.read_csv(fn) rec = df.iloc[-1, :] cur_value = rec.portfolioValue year_ratio = round(100 * (cur_value / newyear_value - 1), 2) pos_dict = eval(rec.posDict) close_dict = eval(rec.closeDict) margin = 0 for symbol in pos_dict: # print(symbol, pos_dict[symbol], close_dict[symbol]) c = get_contract(symbol) margin += abs(pos_dict[symbol]) * close_dict[symbol] * float( c.size) * float(c.margin) risk = round(100 * (margin / capital), 2) r.append([ today, p, capital, cur_value, newyear_value, year_ratio, margin, risk, str(pos_dict) ]) # print(r) df = pd.DataFrame(r) df.to_csv(fn_value, index=False, mode='a', header=None)
def draw_charts(): pz = 'CF' vtSymbol = 'CF' fn = get_dss() + 'backtest/fut/' + pz + '/day_' + vtSymbol + '.csv' df1 = pd.read_csv(fn) # df1 = df1[df1.date >= '2019-01-20'] #df1['datetime'] = df1['date'] + ' ' + df1['time'] df1['datetime'] = df1['date'] price_min = int(df1.close.min() * 0.99) price_max = df1.close.max() #fn = get_dss( )+ 'fut/engine/aberration_raw/signal_aberration_raw_duo_deal_CF.csv' #fn = get_dss( )+ 'fut/engine/aberration_raw/signal_aberration_raw_kong_deal_CF.csv' fn = get_dss( ) + 'fut/engine/aberration_enhance/signal_aberration_enhance_duo_deal_CF.csv' #fn = get_dss( )+ 'fut/engine/aberration_enhance/signal_aberration_enhance_kong_deal_CF.csv' df2 = pd.read_csv(fn) dt_list = df2['datetime'].tolist() dt_list = [dt[:10] for dt in dt_list] # print(dt_list) df2['datetime'] = dt_list line = gen_line(df1, vtSymbol, price_min, price_max) line_atr = gen_atr(df1, 10) line_boll = gen_boll(df1, 10, 2) line = line.overlap(line_boll) scatter_open = gen_poit_open(df2) scatter_close = gen_poit_close(df2) line = line.overlap(scatter_open) line = line.overlap(scatter_close) grid_chart = Grid(init_opts=opts.InitOpts( width="1300px", height="700px", animation_opts=opts.AnimationOpts(animation=False), )) grid_chart.add( line, grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", height="60%"), ) grid_chart.add( line_atr, grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", pos_top="76%", height="17%"), ) fn = get_dss() + 'backtest/render/k_deal_aberration_' + vtSymbol + '.html' grid_chart.render(fn)
def IO(date): year = date[:4] hv, obj_price = calc_hv(date) # print(hv, obj_price) fn = get_dss() + 'backtest/IO/' + 'IO' + year + '_greeks.csv' df = pd.read_csv(fn) df = df[(df.date == date) & (df.symbol.str.startswith('IO'))] # print(df.head()) # print( len(df) ) term_list = sorted(list(set([x[:6] for x in df.symbol]))) # print(term_list) r = [] for term in term_list: df1 = df[df.symbol.str.startswith(term)] strike_list = sorted(list(set([x[-4:] for x in df1.symbol]))) # print(term, strike_list) df1 = df1.set_index('symbol') i = 0 c_iv = 0 p_iv = 0 c_curve_dict = {} p_curve_dict = {} for strike in strike_list: symbol_c = term + '-C-' + strike symbol_p = term + '-P-' + strike c_curve_dict[strike] = df1.at[symbol_c, 'iv'] p_curve_dict[strike] = df1.at[symbol_p, 'iv'] if float(strike) >= obj_price - 100 and float( strike) <= obj_price + 100: i += 1 c_iv += df1.at[symbol_c, 'iv'] p_iv += df1.at[symbol_p, 'iv'] c_iv = c_iv / i p_iv = p_iv / i iv = c_iv * 0.5 + p_iv * 0.5 r.append([ date, term, iv, c_iv, p_iv, hv, obj_price, str(c_curve_dict), str(p_curve_dict) ]) fn = get_dss() + 'backtest/IO/' + 'IO' + year + '_sigma.csv' df = pd.DataFrame(r, columns=[ 'date', 'term', 'iv', 'c_iv', 'p_iv', 'hv', 'obj_price', 'c_curve', 'p_curve' ]) if os.path.exists(fn): df.to_csv(fn, index=False, mode='a', header=False) else: df.to_csv(fn, index=False)
def m15(): year = '2019' title = 'm15' symbol1 = 'm01' symbol2 = 'm05' start_dt = year + '-09-01' end_dt = year + '-12-31' fn1 = get_dss() + 'backtest/fut/m/day_' + symbol1 + '.csv' fn2 = get_dss() + 'backtest/fut/m/day_' + symbol2 + '.csv' sp(year + ' - ' + title, symbol1, symbol2, start_dt, end_dt, fn1, fn2)
def p91(): year = '2018' title = 'p91' symbol1 = 'p09' symbol2 = 'p01' start_dt = year + '-05-01' end_dt = year + '-08-31' fn1 = get_dss() + 'backtest/fut/p/day_' + symbol1 + '.csv' fn2 = get_dss() + 'backtest/fut/p/day_' + symbol2 + '.csv' sp(year + ' - ' + title, symbol1, symbol2, start_dt, end_dt, fn1, fn2)
def al23(): year = '2019' title = 'al23' symbol1 = 'al02' symbol2 = 'al03' start_dt = year + '-04-01' end_dt = year + '-12-31' fn1 = get_dss() + 'backtest/fut/al/day_' + symbol1 + '.csv' fn2 = get_dss() + 'backtest/fut/al/day_' + symbol2 + '.csv' sp(year + ' - ' + title, symbol1, symbol2, start_dt, end_dt, fn1, fn2)
def al67(): year = '2019' title = 'al67' symbol1 = 'al11' symbol2 = 'al12' start_dt = '2018' + '-01-01' end_dt = '2019' + '-12-31' fn1 = get_dss() + 'backtest/fut/al/day_' + symbol1 + '.csv' fn2 = get_dss() + 'backtest/fut/al/day_' + symbol2 + '.csv' sp(year + ' - ' + title, symbol1, symbol2, start_dt, end_dt, fn1, fn2)
def star(): pz_list = ['CF', 'SR', 'IO'] for pz in pz_list: # 读取品种每日盈亏情况,清洗数据为每日一个记录 fn = get_dss() + 'fut/engine/star/portfolio_star_' + pz + '_var.csv' df2 = pd.read_csv(fn) df2['date'] = df2.datetime.str.slice(0, 10) df2['time'] = df2.datetime.str.slice(11, 19) df2 = df2[df2.time.isin(['14:59:00', '15:00:00'])] df2 = df2.drop_duplicates(subset=['date'], keep='last') df2['star'] = df2['portfolioValue'] + df2['netPnl'] df2 = df2.loc[:, ['date', 'star']] df2 = df2.set_index('date') # print(df2.head(3)) fn = get_dss( ) + 'fut/engine/mutual/portfolio_mutual_' + pz + '_var.csv' df3 = pd.read_csv(fn) df3['date'] = df3.datetime.str.slice(0, 10) df3['time'] = df3.datetime.str.slice(11, 19) df3 = df3[df3.time.isin(['14:59:00', '15:00:00'])] df3 = df3.drop_duplicates(subset=['date'], keep='last') df3['mutual'] = df3['portfolioValue'] + df3['netPnl'] df3 = df3.loc[:, ['date', 'mutual']] df3 = df3.set_index('date') # print(df3.head(3)) df = df2.join(df3) df['total'] = df['star'] + df['mutual'] # print(df) plt.figure(figsize=(12, 7)) plt.title(pz) plt.plot(df.star) plt.plot(df.mutual) plt.plot(df.total) plt.xticks(rotation=45) plt.grid(True, axis='y') ax = plt.gca() for label in ax.get_xticklabels(): label.set_visible(False) for label in ax.get_xticklabels()[1::25]: label.set_visible(True) for label in ax.get_xticklabels()[-1:]: label.set_visible(True) plt.legend() fn = 'static/star_' + pz + '.jpg' plt.savefig(fn) plt.cla()
def hs300_spread_show(start_day): df_300 = get_inx('000300', start_day) df_300 = df_300.set_index('date') df_300 = df_300.sort_index() # print(df_300.tail()) fn = get_dss() + 'fut/cfg/opt_mature.csv' df_opt = pd.read_csv(fn) df = df_opt[(df_opt.pz == 'IO') & (df_opt.flag == df_opt.flag)] # 筛选出不为空的记录 df = df.sort_values('symbol') symbol_list = list(df.symbol) # print(symbol_list) plt.figure(figsize=(12, 8)) plt.title('hs300_spread') for symbol in symbol_list: code = 'IF' + symbol[2:] fn = get_dss() + 'fut/bar/day_' + code + '.csv' if os.path.exists(fn) == False: continue df = pd.read_csv(fn) df = df[df.date >= start_day] df = df.set_index('date') df = df.sort_index() df['value'] = df.close - df_300.close # print(df.tail()) plt.plot(df.value, label=code) plt.xticks(rotation=45) plt.grid(True, axis='y') ax = plt.gca() # for label in ax.get_xticklabels(): # label.set_visible(False) # for label in ax.get_xticklabels()[1::25]: # label.set_visible(True) # for label in ax.get_xticklabels()[-1:]: # label.set_visible(True) plt.legend() fn = 'static/hs300_spread_show.jpg' plt.savefig(fn) plt.cla() r = '' fn = 'hs300_spread_show.jpg' now = str(int(time.time())) r = '<img src=\"static/' + fn + '?rand=' + now + '\" />' return r