import os import time import numpy as np import pandas as pd import datetime from bokeh.io import output_file, show from bokeh.layouts import column, grid from bokeh.plotting import figure from bokeh.models import LinearAxis, Range1d from scipy.stats import linregress from chinese_calendar import is_workday, is_holiday from utils import * from chinese_calendar import is_workday from cftc import * from lme import * from option import * cur_dir = os.path.dirname(__file__) data_dir = os.path.join(cur_dir, 'data') start_time = '2005-1-1' end_time = '2028-12-31' # 20ç§é¢è² many_colors = ['crimson','orange','blue','darkgreen','khaki','purple','deeppink', 'cyan','darkgray','tomato','turquoise','yellow','yellowgreen','gold','black', 'teal','midnightblue','red','gold'] def test1(): path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) shfe_cu = np.array(df['æè´§æ¶çä»·(主å):é´æé'], dtype=float) t00, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) t01, shfe_cu = get_period_data(t, shfe_cu, start_time, end_time, remove_nan=True) cftc_plot_disaggregated(t00, lme_cu_3m, 'æè´§æ¶çä»·:LME3个æé', t01, shfe_cu, 'æè´§æ¶çä»·(主å):沪é', code='085692', inst_name='CME:é') def test11(): path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) shfe_cu = np.array(df['æè´§æ¶çä»·(主å):é´æé'], dtype=float) t00, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) t01, shfe_cu = get_period_data(t, shfe_cu, start_time, end_time, remove_nan=True) lme_plot_position(t00, lme_cu_3m, 'æè´§æ¶çä»·:LME3个æé', t01, shfe_cu, 'æè´§æ¶çä»·(主å):沪é', code='Copper', inst_name='LME:é') # éåºå def test2(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) shfe_cu = np.array(df1['æè´§æ¶çä»·(主å):é´æé'], dtype=float) lme_cu_03 = np.array(df1['LMEéåè´´æ°´(0-3)'], dtype=float) # sh_bonded_area_stock1 = np.array(df1['åºåæè´§ï¼éï¼ä¿ç¨ååæ»è®¡'], dtype=float) # # sh_bonded_area_stock2 = np.array(df1['åºå:é:ä¸æµ·ä¿ç¨åº'], dtype=float) lme_stock = np.array(df1['LMEæ»åºå:é'], dtype=float) lme_stock_cancel = np.array(df1['LME注éä»å:é'], dtype=float) lme_stock_register = np.array(df1['LME注åä»å:é'], dtype=float) sh_stock = np.array(df1['åºå:é´æé'], dtype=float) # premium1 = np.array(df1['1#çµè§£éåè´´æ°´:æ大å¼'], dtype=float) comex_stock = np.array(df1['COMEXåºå:é'], dtype=float) # idx = np.logical_not(np.isnan(sh_bonded_area_stock1)) # t11, sh_bonded_area_stock1 = get_period_data(t1[idx], sh_bonded_area_stock1[idx], start_time, end_time) idx = np.logical_not(np.isnan(lme_stock)) t13, lme_stock = get_period_data(t1[idx], lme_stock[idx], start_time, end_time) idx = np.logical_not(np.isnan(sh_stock)) t14, sh_stock = get_period_data(t1[idx], sh_stock[idx], start_time, end_time) t15, comex_stock = get_period_data(t1, comex_stock, start_time, end_time, remove_nan=True) idx = np.logical_not(np.isnan(shfe_cu)) t01 = t1[idx] shfe_cu = shfe_cu[idx] fig0 = figure(frame_width=1400, frame_height=200, tools=TOOLS, x_axis_type = "datetime") fig0.line(t01, shfe_cu, line_width=2, line_color='black', legend_label='沪é') fig0.xaxis[0].ticker.desired_num_ticks = 20 fig1 = figure(frame_width=1400, frame_height=250, tools=TOOLS, x_range=fig0.x_range, x_axis_type = "datetime") # fig1.line(t11, sh_bonded_area_stock1, color='orange', legend_label='åºå:é:ä¿ç¨') fig1.line(t13, lme_stock, line_width=2, color='blue', legend_label='æ»åºå:LMEé') fig1.line(t14, sh_stock, line_width=2, color='deeppink', legend_label='åºåæè´§:é´æé') fig1.line(t15, comex_stock, line_width=2, color='darkgreen', legend_label='COMEXåºå:é') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig1.yaxis[0].axis_label = 'åºå(å¨)' # åºåãä»åãæä» idx = np.logical_not(np.isnan(lme_stock_register)) t16 = t1[idx] lme_stock_register = lme_stock_register[idx] idx = np.logical_not(np.isnan(lme_stock_cancel)) t17 = t1[idx] lme_stock_cancel = lme_stock_cancel[idx] fig2 = figure(frame_width=1400, frame_height=250, tools=TOOLS, x_range=fig0.x_range, x_axis_type = "datetime") # fig2.line(tp_short, lme_position_short, line_width=2, line_color='red', legend_label='LME æä»é') fig2.line(t13, lme_stock, line_width=1, line_color='blue', legend_label='LME åºå') fig2.line(t16, lme_stock_register, line_width=1, line_color='orange', legend_label='LME 注åä»å') fig2.line(t17, lme_stock_cancel, line_width=1, line_color='darkgreen', legend_label='LME 注éä»å') fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" # # åºåãä»å/æä» # fig3 = figure(frame_width=1400, frame_height=250, x_range=fig0.x_range, x_axis_type = "datetime") # fig3.line(tr1, ratio1, line_width=2, line_color='orange', legend_label='LME åºå/æä»é') # fig3.line(tr2, ratio2, line_width=2, line_color='blue', legend_label='LME 注åä»å/æä»é') # fig3.line(tr3, ratio3, line_width=2, line_color='darkgreen', legend_label='LME 注éä»å/æä»é') # fig3.y_range = Range1d(0, 1.1) # idx = np.logical_not(np.isnan(lme_cu_03)) # t21 = t1[idx] # lme_cu_03 = lme_cu_03[idx] # y_column2_name = 'y22' # fig3.extra_y_ranges = { # y_column2_name: Range1d( # start=np.min(lme_cu_03) - abs(np.min(lme_cu_03))*0.1, # end=200, # ), # } # fig3.line(t21, lme_cu_03, color='black', y_range_name=y_column2_name, legend_label='LME 0-3') # fig3.add_layout(LinearAxis(y_range_name=y_column2_name), 'right') # fig3.xaxis[0].ticker.desired_num_ticks = 20 # fig3.legend.click_policy="hide" show(column(fig0,fig1,fig2)) # é PMI def test3(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df1['æè´§æ¶çä»·:LME3个æé'], dtype=float) shfe_cu = np.array(df1['æè´§æ¶çä»·(主å):é´æé'], dtype=float) path2 = os.path.join(data_dir, 'ä¸ç¾PMI'+'.csv') df2 = pd.read_csv(path2) t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m')) pmi = np.array(df2['PMI'], dtype=float) pmi1 = np.array(df2['PMI:ç产'], dtype=float) pmi2 = np.array(df2['PMI:æ°è®¢å'], dtype=float) pmi3 = np.array(df2['PMI:æ°åºå£è®¢å'], dtype=float) # pmi4 = np.array(df2['PMI:å¨æ订å'], dtype=float) pmi5 = np.array(df2['PMI:产æååºå'], dtype=float) pmi6 = np.array(df2['PMI:éè´é'], dtype=float) pmi7 = np.array(df2['PMI:è¿å£'], dtype=float) pmi8 = np.array(df2['PMI:åºåä»·æ ¼'], dtype=float) pmi9 = np.array(df2['PMI:è´è¿ä»·æ ¼'], dtype=float) pmi10 = np.array(df2['PMI:åææåºå'], dtype=float) # pmi11 = np.array(df2['PMI:ä¾è´§åé éæ¶é´'], dtype=float) # pmi12 = np.array(df2['PMI:ç产ç»è¥æ´»å¨é¢æ'], dtype=float) print(correlation(t1, shfe_cu, t2, pmi)) print(correlation(t1, shfe_cu, t2, pmi1)) print(correlation(t1, shfe_cu, t2, pmi2)) print(correlation(t1, shfe_cu, t2, pmi3)) # print(correlation(t1, shfe_cu, t2, pmi4)) print(correlation(t1, shfe_cu, t2, pmi5)) print(correlation(t1, shfe_cu, t2, pmi6)) print(correlation(t1, shfe_cu, t2, pmi7)) print(correlation(t1, shfe_cu, t2, pmi8)) print(correlation(t1, shfe_cu, t2, pmi9)) print(correlation(t1, shfe_cu, t2, pmi10)) # return t3 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m')) us_pmi = np.array(df2['ç¾å½:ä¾åºç®¡çåä¼(ISM):PMI:å£è°'], dtype=float) us_pmi1 = np.array(df2['ç¾å½:ISM:PMI:æ°è®¢å:å£è°'], dtype=float) us_pmi2 = np.array(df2['ç¾å½:ISM:PMI:产åº:å£è°'], dtype=float) us_pmi3 = np.array(df2['ç¾å½:ISM:PMI:å°±ä¸:å£è°'], dtype=float) us_pmi4 = np.array(df2['ç¾å½:ISM:PMI:ä¾åºå交ä»:å£è°'], dtype=float) us_pmi5 = np.array(df2['ç¾å½:ISM:PMI:èªæåºå:å£è°'], dtype=float) us_pmi6 = np.array(df2['ç¾å½:ISM:PMI:客æ·åºå:å£è°'], dtype=float) us_pmi7 = np.array(df2['ç¾å½:ISM:PMI:ç©ä»·:å£è°'], dtype=float) us_pmi8 = np.array(df2['ç¾å½:ISM:PMI:订ååºå:å£è°'], dtype=float) us_pmi9 = np.array(df2['ç¾å½:ISM:PMI:æ°åºå£è®¢å:å£è°'], dtype=float) us_pmi10 = np.array(df2['ç¾å½:ISM:PMI:è¿å£:å£è°'], dtype=float) us_pmi11 = np.array(df2['ç¾å½:ä¾åºç®¡çåä¼(ISM):æå¡ä¸PMI:å£è°'], dtype=float) us_pmi12 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:åä¸æ´»å¨:å£è°'], dtype=float) us_pmi13 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:æ°è®¢å:å£è°'], dtype=float) us_pmi14 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:å°±ä¸:å£è°'], dtype=float) us_pmi15 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:ä¾åºå交ä»:å£è°'], dtype=float) us_pmi16 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:åºå:å£è°'], dtype=float) us_pmi17 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:ç©ä»·:å£è°'], dtype=float) us_pmi18 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:订ååºå:å£è°'], dtype=float) us_pmi19 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:æ°åºå£è®¢å:å£è°'], dtype=float) us_pmi20 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:è¿å£:å£è°'], dtype=float) us_pmi21 = np.array(df2['ç¾å½:ISM:æå¡ä¸PMI:åºåæ¯æ°:å£è°'], dtype=float) us_pmi22 = np.array(df2['ç¾å½:Markitå¶é ä¸PMI:åå¼'], dtype=float) us_pmi23 = np.array(df2['ç¾å½:Markit PMI:æå¡ä¸:åå¼'], dtype=float) us_pmi24 = np.array(df2['ç¾å½:Markit PMI:综å产åº:åå¼'], dtype=float) print('US PMI') print(correlation(t1, shfe_cu, t3, us_pmi)) print(correlation(t1, shfe_cu, t3, us_pmi1)) print(correlation(t1, shfe_cu, t3, us_pmi2)) print(correlation(t1, shfe_cu, t3, us_pmi3)) print(correlation(t1, shfe_cu, t3, us_pmi4)) print(correlation(t1, shfe_cu, t3, us_pmi5)) print(correlation(t1, shfe_cu, t3, us_pmi6)) print(correlation(t1, shfe_cu, t3, us_pmi7)) print(correlation(t1, shfe_cu, t3, us_pmi8)) print(correlation(t1, shfe_cu, t3, us_pmi9)) print(correlation(t1, shfe_cu, t3, us_pmi10)) print(correlation(t1, shfe_cu, t3, us_pmi11)) print(correlation(t1, shfe_cu, t3, us_pmi12)) print(correlation(t1, shfe_cu, t3, us_pmi13)) print(correlation(t1, shfe_cu, t3, us_pmi14)) print(correlation(t1, shfe_cu, t3, us_pmi15)) print(correlation(t1, shfe_cu, t3, us_pmi16)) print(correlation(t1, shfe_cu, t3, us_pmi17)) print(correlation(t1, shfe_cu, t3, us_pmi18)) print(correlation(t1, shfe_cu, t3, us_pmi19)) print(correlation(t1, shfe_cu, t3, us_pmi20)) print(correlation(t1, shfe_cu, t3, us_pmi21)) print(correlation(t1, shfe_cu, t3, us_pmi22)) print(correlation(t1, shfe_cu, t3, us_pmi23)) print(correlation(t1, shfe_cu, t3, us_pmi24)) idx = np.logical_not(np.isnan(shfe_cu)) t11, shfe_cu = get_period_data(t1[idx], shfe_cu[idx], start_time, end_time) idx = np.logical_not(np.isnan(pmi)) t20, pmi = get_period_data(t2[idx], pmi[idx], start_time, end_time) idx = np.logical_not(np.isnan(pmi9)) t21, pmi9 = get_period_data(t2[idx], pmi9[idx], start_time, end_time) idx = np.logical_not(np.isnan(pmi2)) t22, pmi2 = get_period_data(t2[idx], pmi2[idx], start_time, end_time) idx = np.logical_not(np.isnan(pmi1)) t23, pmi1 = get_period_data(t2[idx], pmi1[idx], start_time, end_time) idx = np.logical_not(np.isnan(pmi3)) t24, pmi3 = get_period_data(t2[idx], pmi3[idx], start_time, end_time) fig1 = figure(frame_width=725, frame_height=170, x_axis_type = "datetime") fig1.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig1.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig1.extra_y_ranges = { y_column2_name: Range1d( start=45, end=54, ), } fig1.line(t20, pmi, color='blue', y_range_name=y_column2_name, legend_label='PMI') fig1.line(t20, 50, color='black', y_range_name=y_column2_name) fig1.add_layout(LinearAxis(y_range_name="y12"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 10 fig1.legend.click_policy="hide" fig1.legend.location='top_left' fig11 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig11.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig11.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y112' fig11.extra_y_ranges = { y_column2_name: Range1d( start=np.min(pmi9)*0.9, end=np.max(pmi9)*1.1, ), } fig11.line(t21, pmi9, color='blue', y_range_name=y_column2_name, legend_label='PMI:è´è¿ä»·æ ¼') fig11.line(t21, 50, color='black', y_range_name=y_column2_name) fig11.add_layout(LinearAxis(y_range_name="y112"), 'right') fig11.xaxis[0].ticker.desired_num_ticks = 10 fig11.legend.click_policy="hide" fig11.legend.location='top_left' fig12 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig12.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig12.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y122' fig12.extra_y_ranges = { y_column2_name: Range1d( start=44, end=55, ), } fig12.line(t23, pmi1, color='blue', y_range_name=y_column2_name, legend_label='PMI:ç产') fig12.line(t23, 50, color='black', y_range_name=y_column2_name) fig12.add_layout(LinearAxis(y_range_name="y122"), 'right') fig12.xaxis[0].ticker.desired_num_ticks = 10 fig12.legend.click_policy="hide" fig12.legend.location='top_left' fig13 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig13.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig13.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y132' fig13.extra_y_ranges = { y_column2_name: Range1d( start=44, end=55, ), } fig13.line(t24, pmi3, color='blue', y_range_name=y_column2_name, legend_label='PMI:æ°åºå£è®¢å') fig13.line(t24, 50, color='black', y_range_name=y_column2_name) fig13.add_layout(LinearAxis(y_range_name="y132"), 'right') fig13.xaxis[0].ticker.desired_num_ticks = 10 fig13.legend.click_policy="hide" fig13.legend.location='top_left' fig14 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig14.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig14.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y22' fig14.extra_y_ranges = { y_column2_name: Range1d( start=40, end=np.max(pmi2)*1.1, ), } fig14.line(t22, pmi2, color='blue', y_range_name=y_column2_name, legend_label='PMI:æ°è®¢å') fig14.line(t22, 50, color='black', y_range_name=y_column2_name) fig14.add_layout(LinearAxis(y_range_name="y22"), 'right') fig14.xaxis[0].ticker.desired_num_ticks = 10 fig14.legend.click_policy="hide" fig14.legend.location='top_left' idx = np.logical_not(np.isnan(us_pmi5)) t31, us_pmi5 = get_period_data(t3[idx], us_pmi5[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi6)) t31, us_pmi6 = get_period_data(t3[idx], us_pmi6[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi17)) t31, us_pmi17 = get_period_data(t3[idx], us_pmi17[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi)) t32, us_pmi = get_period_data(t3[idx], us_pmi[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi21)) t33, us_pmi21 = get_period_data(t3[idx], us_pmi21[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi22)) t34, us_pmi22 = get_period_data(t3[idx], us_pmi22[idx], start_time, end_time) idx = np.logical_not(np.isnan(us_pmi24)) t35, us_pmi24 = get_period_data(t3[idx], us_pmi24[idx], start_time, end_time) fig3 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig3.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig3.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y32' fig3.extra_y_ranges = { y_column2_name: Range1d( # start=np.min(pmi)*0.9, # end=np.max(pmi)*1.1, start=46, end=65, ), } fig3.line(t32, us_pmi, color='blue', y_range_name=y_column2_name, legend_label='ç¾å½:ä¾åºç®¡çåä¼(ISM):å¶é ä¸PMI') fig3.add_layout(LinearAxis(y_range_name="y32"), 'right') fig3.xaxis[0].ticker.desired_num_ticks = 10 fig3.legend.click_policy="hide" fig3.legend.location='top_left' fig31 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig31.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig31.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y312' fig31.extra_y_ranges = { y_column2_name: Range1d( # start=np.min(pmi)*0.9, # end=np.max(pmi)*1.1, start=45, end=90, ), } fig31.line(t31, us_pmi17, color='blue', y_range_name=y_column2_name, legend_label='ç¾å½:ISM:éå¶é ä¸PMI:ç©ä»·') fig31.add_layout(LinearAxis(y_range_name="y312"), 'right') fig31.xaxis[0].ticker.desired_num_ticks = 10 fig31.legend.click_policy="hide" fig31.legend.location='top_left' fig32 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig32.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig32.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y312' fig32.extra_y_ranges = { y_column2_name: Range1d( # start=np.min(pmi)*0.9, # end=np.max(pmi)*1.1, start=35, end=70, ), } fig32.line(t33, us_pmi21, color='blue', y_range_name=y_column2_name, legend_label='ç¾å½:ISM:éå¶é ä¸PMI:åºåæ¯æ°') fig32.add_layout(LinearAxis(y_range_name="y312"), 'right') fig32.xaxis[0].ticker.desired_num_ticks = 10 fig32.legend.click_policy="hide" fig32.legend.location='top_left' fig33 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig33.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig33.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y332' fig33.extra_y_ranges = { y_column2_name: Range1d( # start=np.min(pmi)*0.9, # end=np.max(pmi)*1.1, start=35, end=70, ), } fig33.line(t34, us_pmi22, color='blue', y_range_name=y_column2_name, legend_label='ç¾å½:Markitå¶é ä¸PMI:åå¼') fig33.add_layout(LinearAxis(y_range_name="y332"), 'right') fig33.xaxis[0].ticker.desired_num_ticks = 10 fig33.legend.click_policy="hide" fig33.legend.location='top_left' fig34 = figure(frame_width=725, frame_height=170, x_range=fig1.x_range, x_axis_type = "datetime") fig34.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig34.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y342' fig34.extra_y_ranges = { y_column2_name: Range1d( # start=np.min(pmi)*0.9, # end=np.max(pmi)*1.1, start=35, end=70, ), } fig34.line(t35, us_pmi24, color='blue', y_range_name=y_column2_name, legend_label='ç¾å½:Markit PMI:综å产åº:åå¼') fig34.add_layout(LinearAxis(y_range_name="y342"), 'right') fig34.xaxis[0].ticker.desired_num_ticks = 10 fig34.legend.click_policy="hide" fig34.legend.location='top_left' layout = grid([[fig1, fig3], [fig11, fig31], [fig12, fig32], [fig13, fig14], [fig33, fig34]]) show(layout) # é MSCI def test33(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df1['æè´§å®æ¹ä»·:LME3个æé'], dtype=float) lme_cu_03 = np.array(df1['LMEéåè´´æ°´(0-3)'], dtype=float) shfe_cu = np.array(df1['æè´§æ¶çä»·(æ´»è·å约):é´æé'], dtype=float) msci_em = np.array(df1['MSCIæ°å ´å¸åº'], dtype=float) msci_dm = np.array(df1['MSCIåè¾¾å¸åº'], dtype=float) msci_us = np.array(df1['MSCIç¾å½'], dtype=float) msci_cn = np.array(df1['MSCIä¸å½(ç¾å )'], dtype=float) msci_eu = np.array(df1['MSCI欧洲'], dtype=float) idx = np.logical_not(np.isnan(shfe_cu)) t11, shfe_cu = get_period_data(t1[idx], shfe_cu[idx], start_time, end_time) idx = np.logical_not(np.isnan(msci_em)) t1_em, msci_em = get_period_data(t1[idx], msci_em[idx], start_time, end_time) idx = np.logical_not(np.isnan(msci_dm)) t1_dm, msci_dm = get_period_data(t1[idx], msci_dm[idx], start_time, end_time) idx = np.logical_not(np.isnan(msci_us)) t1_us, msci_us = get_period_data(t1[idx], msci_us[idx], start_time, end_time) idx = np.logical_not(np.isnan(msci_cn)) t1_cn, msci_cn = get_period_data(t1[idx], msci_cn[idx], start_time, end_time) idx = np.logical_not(np.isnan(msci_eu)) t1_eu, msci_eu = get_period_data(t1[idx], msci_eu[idx], start_time, end_time) # path2 = os.path.join(data_dir, 'æ±ç'+'.csv') # df2 = pd.read_csv(path2) # t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) # usdcny = np.array(df2['å³ææ±ç:ç¾å å 人æ°å¸'], dtype=float) fig1 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig1.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig1.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(msci_em)*0.9, end=np.max(msci_em)*1.1, ), } fig1.line(t1_em, msci_em, color='black', y_range_name=y_column2_name, legend_label='MSCI æ°å ´å¸åº') fig1.add_layout(LinearAxis(y_range_name="y12"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 10 fig1.legend.click_policy="hide" fig1.legend.location='top_left' fig2 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig2.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig2.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig2.extra_y_ranges = { y_column2_name: Range1d( start=np.min(msci_dm)*0.9, end=np.max(msci_dm)*1.1, ), } fig2.line(t1_dm, msci_dm, color='black', y_range_name=y_column2_name, legend_label='MSCI åè¾¾å¸åº') fig2.add_layout(LinearAxis(y_range_name="y12"), 'right') fig2.xaxis[0].ticker.desired_num_ticks = 10 fig2.legend.click_policy="hide" fig2.legend.location='top_left' fig3 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig3.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig3.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig3.extra_y_ranges = { y_column2_name: Range1d( start=np.min(msci_us)*0.9, end=np.max(msci_us)*1.1, ), } fig3.line(t1_us, msci_us, color='black', y_range_name=y_column2_name, legend_label='MSCI ç¾å½') fig3.add_layout(LinearAxis(y_range_name="y12"), 'right') fig3.xaxis[0].ticker.desired_num_ticks = 10 fig3.legend.click_policy="hide" fig3.legend.location='top_left' fig4 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig4.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig4.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig4.extra_y_ranges = { y_column2_name: Range1d( start=np.min(msci_cn)*0.9, end=np.max(msci_cn)*1.1, ), } fig4.line(t1_cn, msci_cn, color='black', y_range_name=y_column2_name, legend_label='MSCI ä¸å½') fig4.add_layout(LinearAxis(y_range_name="y12"), 'right') fig4.xaxis[0].ticker.desired_num_ticks = 10 fig4.legend.click_policy="hide" fig4.legend.location='top_left' fig5 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig5.line(t11, shfe_cu, line_width=2, line_color='orange', legend_label='沪é') fig5.y_range = Range1d(np.min(shfe_cu)*0.9, np.max(shfe_cu)*1.1) y_column2_name = 'y12' fig5.extra_y_ranges = { y_column2_name: Range1d( start=np.min(msci_eu)*0.9, end=np.max(msci_eu)*1.1, ), } fig5.line(t1_eu, msci_eu, color='black', y_range_name=y_column2_name, legend_label='MSCI 欧洲') fig5.add_layout(LinearAxis(y_range_name="y12"), 'right') fig5.xaxis[0].ticker.desired_num_ticks = 10 fig5.legend.click_policy="hide" fig5.legend.location='top_left' layout = grid([[fig1, fig3], [fig2, fig4], [fig5, None]]) show(layout) # ç²¾ç¼é ä¿ç¨åºåºå å è¿å£ def test4(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) stock_notax = np.array(df1['åºå:é:ä¸æµ·ä¿ç¨åº'], dtype=float) idx = np.logical_not(np.isnan(stock_notax)) t10 = t1[idx] stock_notax = stock_notax[idx] * 10000 # ä¸å¨ --> å¨ import1 = np.array(df1['è¿å£æ°é:æªé»è½§éå«é>99.9935%çç²¾ç¼éé´æ(74031111):å½æå¼'], dtype=float) import2 = np.array(df1['è¿å£æ°é:æªé»è½§å ¶ä»ç²¾ç¼éé´æ(74031119):å½æå¼'], dtype=float) import3 = np.array(df1['è¿å£æ°é:æªé»è½§ç²¾ç¼éé´æåæ(74031190):å½æå¼'], dtype=float) import4 = np.array(df1['åºå£æ°é:æªé»è½§çç²¾ç¼é线é(74031200):å½æå¼'], dtype=float) import5 = np.array(df1['è¿å£æ°é:æªé»è½§çç²¾ç¼éå¯æ®µ(74031300):å½æå¼'], dtype=float) import6 = np.array(df1['è¿å£æ°é:å ¶ä»æªé»è½§çç²¾ç¼é(74031900):å½æå¼'], dtype=float) export1 = np.array(df1['åºå£æ°é:æªé»è½§éå«é>99.9935%çç²¾ç¼éé´æ(74031111):å½æå¼'], dtype=float) export2 = np.array(df1['åºå£æ°é:æªé»è½§å ¶ä»ç²¾ç¼éé´æ(74031119):å½æå¼'], dtype=float) export4 = np.array(df1['åºå£æ°é:æªé»è½§çç²¾ç¼é线é(74031200):å½æå¼'], dtype=float) export5 = np.array(df1['åºå£æ°é:æªé»è½§çç²¾ç¼éå¯æ®µ(74031300):å½æå¼'], dtype=float) export6 = np.array(df1['åºå£æ°é:å ¶ä»æªé»è½§çç²¾ç¼é(74031900):å½æå¼'], dtype=float) t11, import_sum = data_add(t1, import1, t1, import2, replace=0) t11, import_sum = data_add(t11, import_sum, t1, import3, replace=0) t11, import_sum = data_add(t11, import_sum, t1, import4, replace=0) t11, import_sum = data_add(t11, import_sum, t1, import5, replace=0) t11, import_sum = data_add(t11, import_sum, t1, import6, replace=0) t12, export_sum = data_add(t1, export1, t1, export2, replace=0) t12, export_sum = data_add(t12, export_sum, t1, export4, replace=0) t12, export_sum = data_add(t12, export_sum, t1, export5, replace=0) t12, export_sum = data_add(t12, export_sum, t1, export6, replace=0) # åè¿å£ t13, net_import = data_sub(t11, import_sum, t12, export_sum, replace=0) net_import /= 1000 # åå --> å¨ idx = np.where(net_import > 0)[0] t13 = t13[idx] net_import = net_import[idx] import_sum2 = np.array(df1['è¿å£æ°é:ç²¾ç¼é:å½æå¼'], dtype=float) idx = np.logical_not(np.isnan(import_sum2)) t14 = t1[idx] import_sum2 = import_sum2[idx] import_3 = np.array(df1['è¿å£æ°é:éåºæåç¢æ(74040000):å½æå¼'], dtype=float) idx = np.logical_not(np.isnan(import_3)) t15 = t1[idx] import_3 = import_3[idx]/1000 fig1 = figure(frame_width=1400, frame_height=250, x_axis_type = "datetime") fig1.line(t10, stock_notax, line_width=2, line_color='orange', legend_label='åºå:é:ä¸æµ·ä¿ç¨åº') fig1.line(t14, import_sum2, line_width=2, line_color='purple', legend_label='è¿å£æ°é:ç²¾ç¼é:å½æå¼') fig1.line(t15, import_3, line_width=2, line_color='darkgreen', legend_label='è¿å£æ°é:åºé:å½æå¼') fig1.line(t13, net_import, color='blue', legend_label='ç²¾ç¼é åè¿å£') # fig1.y_range = Range1d(np.min(stock_notax)*0.9, np.max(stock_notax)*1.1) # y_column2_name = 'y2' # fig1.extra_y_ranges = { # y_column2_name: Range1d( # start=np.min(net_import)*0.9, # end=np.max(net_import)*1.1, # ), # } # fig1.line(t13, net_import, color='blue', y_range_name=y_column2_name, legend_label='ç²¾ç¼é åè¿å£') # fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" cu_cny_spot = np.array(df1['é¿æ±æè²å¸åº:å¹³åä»·:é:1#'], dtype=float) idx = np.logical_not(np.isnan(cu_cny_spot)) t17 = t1[idx] cu_cny_spot = cu_cny_spot[idx] lme3 = np.array(df1['æè´§å®æ¹ä»·:LME3个æé'], dtype=float) lme03 = np.array(df1['LMEéåè´´æ°´(0-3)'], dtype=float) lme_spot = np.array(df1['ç°è´§ç»ç®ä»·:LMEé'], dtype=float) path2 = os.path.join(data_dir, 'æ±ç'+'.csv') df2 = pd.read_csv(path2) t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) usdcny = np.array(df2['å³ææ±ç:ç¾å å 人æ°å¸'], dtype=float) idx = np.logical_not(np.isnan(lme_spot)) t15 = t1[idx] lme_spot = lme_spot[idx] t16, lme0 = data_add(t1, lme3, t1, lme03) tmp = lme_spot + 100 t21, cu_cny_import = data_mul(t15, tmp, t2, usdcny) cu_cny_import *= 1.17 cu_cny_import += 200 t22, import_profit = data_sub(t17, cu_cny_spot, t21, cu_cny_import) fig2 = figure(frame_width=1400, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") # fig2.line(t21, cu_cny_import, line_width=2, line_color='orange', legend_label='é è¿å£ä»· 估计') # fig2.line(t17, cu_cny_spot, line_width=2, line_color='blue', legend_label='é å½å ç°è´§ä»·') fig2.line(t22, import_profit, line_width=2, line_color='orange', legend_label='è¿å£å©æ¶¦(估计)') show(column(fig1,fig2)) # _, diff = data_sub(t15, lme_spot, t16, lme0) # print(lme03[-50:]) # print(diff[-50:]) # ç°è´§åè´´æ°´ def test5(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df1['æè´§æ¶çä»·:LME3个æé'], dtype=float) shfe_cu = np.array(df1['æè´§æ¶çä»·(主å):é´æé'], dtype=float) path2 = os.path.join(data_dir, 'æ±ç'+'.csv') df2 = pd.read_csv(path2) t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) usdcny = np.array(df2['å³ææ±ç:ç¾å å 人æ°å¸'], dtype=float) lme03 = np.array(df1['LMEéåè´´æ°´(0-3)'], dtype=float) idx = np.logical_not(np.isnan(lme03)) t10 = t1[idx] lme03 = lme03[idx] t10, lme03_cny = data_mul(t10, lme03, t2, usdcny) premium31 = np.array(df1['ä¸æµ·æè²å¸åº:æ´å±±é溢价:å¹³åä»·'], dtype=float) idx = np.logical_not(np.isnan(premium31)) t13 = t1[idx] premium31 = premium31[idx] t13, premium31 = data_mul(t13, premium31, t2, usdcny) idx = np.logical_not(np.isnan(shfe_cu)) fig1 = figure(frame_width=1400, frame_height=320, x_axis_type = "datetime") fig1.y_range = Range1d(np.min(shfe_cu[idx])*0.9, np.max(shfe_cu[idx])*1.1) fig1.line(t1[idx], shfe_cu[idx], line_width=2, line_color='orange', legend_label='æè´§æ¶çä»·(主å):é´æé') fig1.xaxis[0].ticker.desired_num_ticks = 20 idx = np.logical_not(np.isnan(lme_cu_3m)) y_column2_name = 'y2' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(lme_cu_3m[idx])*0.9, end=np.max(lme_cu_3m[idx])*1.1, ), } fig1.line(t1[idx], lme_cu_3m[idx], color='blue', y_range_name=y_column2_name, legend_label='LME 3个æé ç¾å å³è½´') fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.legend.location='top_left' fig2 = figure(frame_width=1400, frame_height=300, tools=TOOLS, x_range=fig1.x_range, x_axis_type = "datetime") fig2.line(t10, lme03_cny, line_width=2, line_color='black', legend_label='LMEéåè´´æ°´(0-3) 人æ°å¸') fig2.line(t13, premium31, line_width=2, line_color='chartreuse', legend_label='ä¸æµ·æè²å¸åº:æ´å±±é溢价:å¹³åä»·') fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" fig2.legend.location='top_left' plot_seasonality(t13, premium31, title='æ´å±±é溢价') # cu_c1 = np.array(df1['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) # cu_c3 = np.array(df1['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) # t14, cu_diff = data_sub(t1, cu_c1, t1, cu_c3) # fig2 = figure(frame_width=1400, frame_height=300, x_range=fig1.x_range, x_axis_type = "datetime") # fig2.line(t14, cu_diff, line_width=2, line_color='black', legend_label='cu c1-c3 (å·¦)') # fig2.y_range = Range1d(np.min(cu_diff)*0.9, np.max(cu_diff)*1.1) # idx = np.logical_not(np.isnan(cu_c1)) # t15 = t1[idx] # cu_c1 = cu_c1[idx] # idx = np.logical_not(np.isnan(cu_c3)) # t16 = t1[idx] # cu_c3 = cu_c3[idx] # y_column2_name = 'y2' # fig2.extra_y_ranges = { # y_column2_name: Range1d( # start=np.min(cu_c1)*0.9, # end=np.max(cu_c1)*1.1, # ), # } # fig2.line(t15, cu_c1, color='orange', y_range_name=y_column2_name, legend_label='cu c1 (å³)') # fig2.line(t16, cu_c3, color='blue', y_range_name=y_column2_name, legend_label='cu c3 (å³)') # fig2.add_layout(LinearAxis(y_range_name="y2"), 'right') # fig2.xaxis[0].ticker.desired_num_ticks = 20 # fig2.legend.click_policy="hide" # fig2.legend.location='top_left' show(column(fig1,fig2)) # TC/RC def test6(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) tcrc = np.array(df1['é¿æ±æè²å¸åº:éç²¾ç¿TC/RC:å¹³åä»·'], dtype=float) idx = np.logical_not(np.isnan(tcrc)) t10 = t1[idx] tcrc = tcrc[idx] cu_c1 = np.array(df1['æè´§æ¶çä»·(主å):é´æé'], dtype=float) idx = np.logical_not(np.isnan(cu_c1)) t11 = t1[idx] cu_c1 = cu_c1[idx] fig1 = figure(frame_width=1400, frame_height=300, x_axis_type = "datetime") fig1.line(t10, tcrc, line_width=2, line_color='orange', legend_label='ç°è´§:ä¸å½éå¶ç¼å:TCRC (å·¦)') fig1.y_range = Range1d(np.min(tcrc)*0.9, np.max(tcrc)*1.1) y_column2_name = 'y2' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(cu_c1)*0.9, end=np.max(cu_c1)*1.1, ), } fig1.line(t11, cu_c1, line_width=2, color='black', y_range_name=y_column2_name, legend_label='cu c1 (å³)') fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig1.legend.location='top_left' show(fig1) def test66(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) production_air = np.array(df1['产é:空è°:å½æå¼'], dtype=float) production_elec = np.array(df1['产é:åçµè®¾å¤:å½æå¼'], dtype=float) production_wire = np.array(df1['产é:å ç¼:å½æå¼'], dtype=float) production_frig = np.array(df1['产é:家ç¨çµå°ç®±:å½æå¼'], dtype=float) production_li = np.array(df1['产é:é离åçµæ± :å½æå¼'], dtype=float) production_car = np.array(df1['产é:æ°è½æºæ±½è½¦:å½æå¼'], dtype=float) production_house = np.array(df1['æ¿å±æ°å¼å·¥é¢ç§¯:累计å¼'], dtype=float) t11, production_air = get_period_data(t1, production_air, start_time, end_time, remove_nan=True) t12, production_elec = get_period_data(t1, production_elec, start_time, end_time, remove_nan=True) t13, production_wire = get_period_data(t1, production_wire, start_time, end_time, remove_nan=True) t14, production_frig = get_period_data(t1, production_frig, start_time, end_time, remove_nan=True) t15, production_li = get_period_data(t1, production_li, start_time, end_time, remove_nan=True) t16, production_car = get_period_data(t1, production_car, start_time, end_time, remove_nan=True) fig1 = figure(frame_width=725, frame_height=250, x_axis_type = "datetime") fig1.line(t11, production_air, line_width=2, line_color='orange', legend_label='产é:空è°:å½æå¼') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig1.legend.location='top_left' fig2 = figure(frame_width=725, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") fig2.line(t12, production_elec, line_width=2, line_color='orange', legend_label='产é:åçµè®¾å¤:å½æå¼') fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" fig2.legend.location='top_left' fig3 = figure(frame_width=725, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") fig3.line(t13, production_wire, line_width=2, line_color='orange', legend_label='产é:å ç¼:å½æå¼') fig3.xaxis[0].ticker.desired_num_ticks = 20 fig3.legend.click_policy="hide" fig3.legend.location='top_left' fig4 = figure(frame_width=725, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") fig4.line(t14, production_frig, line_width=2, line_color='orange', legend_label='产é:家ç¨çµå°ç®±:å½æå¼') fig4.xaxis[0].ticker.desired_num_ticks = 20 fig4.legend.click_policy="hide" fig4.legend.location='top_left' fig5 = figure(frame_width=725, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") fig5.line(t15, production_li, line_width=2, line_color='orange', legend_label='产é:é离åçµæ± :å½æå¼') fig5.xaxis[0].ticker.desired_num_ticks = 20 fig5.legend.click_policy="hide" fig5.legend.location='top_left' fig6 = figure(frame_width=725, frame_height=250, x_range=fig1.x_range, x_axis_type = "datetime") fig6.line(t16, production_car, line_width=2, line_color='orange', legend_label='产é:æ°è½æºæ±½è½¦:å½æå¼') fig6.xaxis[0].ticker.desired_num_ticks = 20 fig6.legend.click_policy="hide" fig6.legend.location='top_left' layout = grid([[fig1, fig3], [fig2, fig4], [fig5, fig6]]) show(layout) # éæè´§ä»·æ ¼ä¸ç»æµæ°æ® def test7(): path1 = os.path.join(data_dir, 'é'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) # car = np.array(df1['产é:æ°è½æºæ±½è½¦:å½æå¼'], dtype=float) p1 = np.array(df1['ä»·æ ¼:åºé:1#å 亮é线:ä¸æµ·'], dtype=float) p2 = np.array(df1['é¿æ±æè²å¸åº:å¹³åä»·:é:1#'], dtype=float) # ç²¾åºéä»·å·® t11, diff1 = data_sub(t1, p2, t1, p1) cu_c1 = np.array(df1['æè´§æ¶çä»·(主å):é´æé'], dtype=float) idx = np.logical_not(np.isnan(cu_c1)) t12 = t1[idx] cu_c1 = cu_c1[idx] path2 = os.path.join(data_dir, 'éæè´§ä»·æ ¼'+'.csv') df2 = pd.read_csv(path2).dropna() t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) cu01_open = np.array(df2['æè´§å¼çä»·:é:è¿ä¸'], dtype=float) cu02_open = np.array(df2['æè´§å¼çä»·:é:è¿äº'], dtype=float) cu03_open = np.array(df2['æè´§å¼çä»·:é:è¿ä¸'], dtype=float) cu04_open = np.array(df2['æè´§å¼çä»·:é:è¿å'], dtype=float) cu05_open = np.array(df2['æè´§å¼çä»·:é:è¿äº'], dtype=float) cu06_open = np.array(df2['æè´§å¼çä»·:é:è¿å '], dtype=float) cu01_close = np.array(df2['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) cu02_close = np.array(df2['æè´§æ¶çä»·:é:è¿äº'], dtype=float) cu03_close = np.array(df2['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) cu04_close = np.array(df2['æè´§æ¶çä»·:é:è¿å'], dtype=float) cu05_close = np.array(df2['æè´§æ¶çä»·:é:è¿äº'], dtype=float) cu06_close = np.array(df2['æè´§æ¶çä»·:é:è¿å '], dtype=float) diff2 = cu01_close - cu03_close fig1 = figure(frame_width=1400, frame_height=300, x_axis_type = "datetime") fig1.line(t11, diff1, line_width=2, line_color='purple', legend_label='ç²¾åºéä»·å·®') fig1.y_range = Range1d(np.min(diff1)*1.1, np.max(diff1)*1.1) y_column2_name = 'y2' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(diff2)*0.9, end=np.max(diff2)*1.1, ), } fig1.line(t2, diff2, color='blue', y_range_name=y_column2_name, legend_label='cu c1-c3 (å³)') fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig1.legend.location='top_left' show(fig1) # 交å²æ¥ååçæéç»æ def test8(): path1 = os.path.join(data_dir, 'éæè´§ä»·æ ¼'+'.csv') df1 = pd.read_csv(path1).dropna() t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) # cu01_close = np.array(df1['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) # cu02_close = np.array(df1['æè´§æ¶çä»·:é:è¿äº'], dtype=float) # cu03_close = np.array(df1['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) # cu04_close = np.array(df1['æè´§æ¶çä»·:é:è¿å'], dtype=float) # cu05_close = np.array(df1['æè´§æ¶çä»·:é:è¿äº'], dtype=float) # cu06_close = np.array(df1['æè´§æ¶çä»·:é:è¿å '], dtype=float) # cu07_close = np.array(df1['æè´§æ¶çä»·:é:è¿ä¸'], dtype=float) # cu08_close = np.array(df1['æè´§æ¶çä»·:é:è¿å «'], dtype=float) # cu09_close = np.array(df1['æè´§æ¶çä»·:é:è¿ä¹'], dtype=float) # cu10_close = np.array(df1['æè´§æ¶çä»·:é:è¿å'], dtype=float) # cu11_close = np.array(df1['æè´§æ¶çä»·:é:è¿åä¸'], dtype=float) # cu12_close = np.array(df1['æè´§æ¶çä»·:é:è¿åäº'], dtype=float) # plot_term_structure(t1, # [cu01_close,cu02_close,cu03_close,cu04_close,cu05_close,cu06_close, # cu07_close,cu08_close,cu09_close,cu10_close,cu11_close,cu12_close], # ['2022-10-13','2022-10-14','2022-10-17','2022-10-18','2022-10-19','2022-10-20','2022-10-21','2022-10-24'], # '2023-5-31') cu_close = np.array(df1[['æè´§æ¶çä»·:é:1æ份å约','æè´§æ¶çä»·:é:2æ份å约','æè´§æ¶çä»·:é:3æ份å约','æè´§æ¶çä»·:é:4æ份å约', 'æè´§æ¶çä»·:é:5æ份å约','æè´§æ¶çä»·:é:6æ份å约','æè´§æ¶çä»·:é:7æ份å约','æè´§æ¶çä»·:é:8æ份å约', 'æè´§æ¶çä»·:é:9æ份å约','æè´§æ¶çä»·:é:10æ份å约','æè´§æ¶çä»·:é:11æ份å约','æè´§æ¶çä»·:é:12æ份å约']], dtype=float) L = len(t1) # æå交ææ¥åååå 天 days_around = 5 # åä¹åå 个æçåçº¦ä»·æ ¼ months_after = 6 today_15 = datetime.datetime.strptime('2002-02-15', '%Y-%m-%d') time_df = pd.DataFrame() new_data = np.empty([0, months_after],dtype=float) while (1): days_num = calendar.monthrange(today_15.year, today_15.month)[1] today_15 = today_15 + pd.Timedelta(days = days_num) today_tmp = today_15 if (today_15 >= datetime.datetime.strptime('2022-11-01', '%Y-%m-%d')): break # èåæ¥é¡ºå»¶ï¼æ¾15å·ä¹å第ä¸ä¸ªææ°æ®çä¸å¤© while (1): where = np.where(t1 == today_tmp)[0] # 15å·ææ°æ® if (len(where) == 0): today_tmp += pd.Timedelta(days = 1) else: break idx = np.where(t1 == today_tmp)[0][0] time_df = pd.concat([time_df, df1['time'][idx-days_around:idx+days_around+1]], axis=0) new_data = np.vstack([new_data, cu_close[idx-days_around:idx+days_around+1, [x%12 for x in range(today_15.month,today_15.month+months_after)]]]) time_df.columns=['time'] new_time = pd.DatetimeIndex(pd.to_datetime(time_df['time'], format='%Y-%m-%d')) print(new_time) print(new_data) avg_time = new_time[days_around::(days_around*2+1)] n = int(len(new_data)/(2*days_around+1)) avg_before = np.empty((n), dtype=float) avg_after = np.empty((n), dtype=float) avg_diff = np.empty((n), dtype=float) for i in range(n): # ä»·å·® avg_before[i] = np.average(new_data[days_around+i*(days_around*2+1)-2:days_around+i*(days_around*2+1)+1, 1]-new_data[days_around+i*(days_around*2+1)-2:days_around+i*(days_around*2+1)+1, 4]) avg_after[i] = np.average(new_data[days_around+i*(days_around*2+1)+1:days_around+i*(days_around*2+1)+3, 1]-new_data[days_around+i*(days_around*2+1)+1:days_around+i*(days_around*2+1)+3, 4]) # ç»å¯¹ä»·æ ¼ # avg_before[i] = np.average(new_data[days_around+i*(days_around*2+1)-1:days_around+i*(days_around*2+1)+1, 1]) # avg_after[i] = np.average(new_data[days_around+i*(days_around*2+1)+2:days_around+i*(days_around*2+1)+5, 1]) avg_diff = avg_after - avg_before path2 = os.path.join(data_dir, 'é'+'.csv') df2 = pd.read_csv(path2) t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) stock_notax = np.array(df2['åºå:é:ä¸æµ·ä¿ç¨åº'], dtype=float) lme_stock = np.array(df2['æ»åºå:LMEé'], dtype=float) sh_stock = np.array(df2['åºåæè´§:é´æé'], dtype=float) stock = sh_stock t2_diff = np.empty((n), dtype=pd.Timestamp) stock_diff = np.zeros((n), dtype=float) for i in range(n): where = np.where(t2 == avg_time[i])[0] if (len(where) == 1): idx = where[0] if (not np.isnan(stock[idx])): t2_diff[i] = t2[idx] stock_diff[i] = (stock[idx] + stock[idx-1] - stock[idx-2] - stock[idx-3])/stock[idx-3] print(stock_diff) idx = np.logical_not(np.isnan(stock_diff)) t2_diff = t2_diff[idx] stock_diff = stock_diff[idx] avg_diff /= np.max(avg_diff) stock_diff /= np.max(stock_diff) fig1 = figure(frame_width=1400, frame_height=600, x_axis_type = "datetime") fig1.vbar(x=t2_diff-pd.Timedelta(days = 4), top=stock_diff, line_width=2, color='orange', legend_label='åºåå·®') fig1.vbar(x=avg_time, top=avg_diff, line_width=2, color='purple', legend_label='ä»·å·®') # fig1.y_range = Range1d(np.min(avg_diff)*1.1, np.max(avg_diff)*1.1) # y_column2_name = 'y2' # fig1.extra_y_ranges = { # y_column2_name: Range1d( # start=np.min(sh_stock_diff)*1.1, # end=np.max(sh_stock_diff)*1.1, # ), # } # fig1.vbar(x=t2_diff+pd.Timedelta(days = 4), top=sh_stock_diff, y_range_name=y_column2_name, line_width=2, color='orange', legend_label='åºåå·®') # fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig1.legend.location='top_left' show(fig1) # cu01-02 å bc01-02 çå ³ç³» def test9(): path1 = os.path.join(data_dir, '沪éè¿ä¸'+'.csv') df1 = pd.read_csv(path1).dropna() t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) cu01_close = np.array(df1['æ¶çä»·(å )'], dtype=float) t1, cu01_close = get_period_data(t1, cu01_close, '2022-07-20 09:45:00', '2022-10-27 10:35:00', format='%Y-%m-%d %H:%M:%S') path2 = os.path.join(data_dir, '沪éè¿äº'+'.csv') df2 = pd.read_csv(path2).dropna() t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) cu02_close = np.array(df2['æ¶çä»·(å )'], dtype=float) t2, cu02_close = get_period_data(t2, cu02_close, '2022-07-20 09:45:00', '2022-10-27 10:35:00', format='%Y-%m-%d %H:%M:%S') path3 = os.path.join(data_dir, 'å½é éè¿ä¸'+'.csv') df3 = pd.read_csv(path3).dropna() t3 = pd.DatetimeIndex(pd.to_datetime(df3['time'], format='%Y-%m-%d')) bc01_close = np.array(df3['æ¶çä»·(å )'], dtype=float) t3, bc01_close = get_period_data(t3, bc01_close, '2022-07-20 09:45:00', '2022-10-27 10:35:00', format='%Y-%m-%d %H:%M:%S') path4 = os.path.join(data_dir, 'å½é éè¿äº'+'.csv') df4 = pd.read_csv(path4).dropna() t4 = pd.DatetimeIndex(pd.to_datetime(df4['time'], format='%Y-%m-%d')) bc02_close = np.array(df4['æ¶çä»·(å )'], dtype=float) t4, bc02_close = get_period_data(t4, bc02_close, '2022-07-20 09:45:00', '2022-10-27 10:35:00', format='%Y-%m-%d %H:%M:%S') # æ¶é´å¯¹é½ idx1 = np.isin(t1, t2) idx2 = np.isin(t2, t1) t1 = t1[idx1] # t2 = t2[idx2] cu01_close = cu01_close[idx1] cu02_close = cu02_close[idx2] idx1 = np.isin(t1, t3) idx2 = np.isin(t3, t1) t1 = t1[idx1] # t3 = t3[idx2] cu01_close = cu01_close[idx1] cu02_close = cu02_close[idx1] bc01_close = bc01_close[idx2] idx1 = np.isin(t1, t4) idx2 = np.isin(t4, t1) t1 = t1[idx1] # t3 = t3[idx2] cu01_close = cu01_close[idx1] cu02_close = cu02_close[idx1] bc01_close = bc01_close[idx1] bc02_close = bc02_close[idx2] print(len(cu01_close),len(cu02_close),len(bc01_close),len(bc02_close),len(t1)) cu_diff = cu01_close - cu02_close bc_diff = bc01_close - bc02_close diff = cu_diff - bc_diff fig1 = figure(frame_width=1500, frame_height=400, x_axis_type = "datetime") # fig1.line(t1, diff, line_width=2, line_color='orange', legend_label='diff') # fig1.circle(x=cu_diff, y=bc_diff, color="blue") slope, intercept, _, _, _ = linregress(cu_diff, bc_diff) yy = cu_diff * slope + intercept # fig1.line(x=cu_diff, y=yy, color="black") fig1.line(t1, cu_diff, line_width=2, color='orange', legend_label='cu01 - cu02') fig1.line(t1, bc_diff, line_width=2, color='blue', legend_label='bc01 - bc02') # fig1.vbar(x=t1, top=cu_diff, line_width=2, color='orange', legend_label='cu01 - cu02') # fig1.vbar(x=t1, top=bc_diff, line_width=2, color='blue', legend_label='bc01 - bc02') fig2 = figure(frame_width=1500, frame_height=500, x_range=fig1.x_range, x_axis_type = "datetime") fig2.line(t1, diff, line_width=2, line_color='black', legend_label='(cu01 - cu02) - (bc01 - bc02)') show(column(fig1,fig2)) print(linregress(cu_diff, bc_diff)) pass # cuä»·å·® å bcä»·å·® çä»·å·® def test10(): cu_list = ['cu2109','cu2110','cu2111','cu2112','cu2201','cu2202','cu2203','cu2204','cu2205','cu2206','cu2207','cu2208','cu2209','cu2210','cu2211','cu2212','cu2301','cu2302','cu2303'] bc_list = ['bc2109','bc2110','bc2111','bc2112','bc2201','bc2202','bc2203','bc2204','bc2205','bc2206','bc2207','bc2208','bc2209','bc2210','bc2211','bc2212','bc2301','bc2302','bc2303'] list_len = len(cu_list) cu_df_list = [] bc_df_list = [] for i in range(list_len): # 读cuæ°æ® path = os.path.join(data_dir, cu_list[i]+'_1min'+'.csv') cu_df_list.append(pd.read_csv(path).dropna()) # 读bcæ°æ® path = os.path.join(data_dir, bc_list[i]+'_1min'+'.csv') bc_df_list.append(pd.read_csv(path).dropna()) t = np.empty([0],dtype=np.datetime64) cu_price_c1 = np.empty([0],dtype=float) cu_price_c2 = np.empty([0],dtype=float) bc_price_c1 = np.empty([0],dtype=float) bc_price_c2 = np.empty([0],dtype=float) cu_n1 = 0 cu_n2 = 1 bc_n1 = 0 bc_n2 = 1 fig1 = figure(frame_width=1400, frame_height=400, x_axis_type = "datetime", title='xæ5æ¥ è³ x+1æ25æ¥, x+2,x+3å约') fig2 = figure(frame_width=1400, frame_height=400, x_axis_type = "datetime", x_range=fig1.x_range) for i in range(3,list_len-2): start_time = datetime.datetime.strptime('20'+cu_list[i+cu_n1][2:], '%Y%m') # x-3æ1æ¥ ~ x-1æ1æ¥ # end_time = datetime.datetime(start_time.year, start_time.month, 1) - pd.Timedelta(days=3) # end_time = datetime.datetime(end_time.year, end_time.month, 1) # start_time = datetime.datetime(start_time.year, start_time.month, 1) - pd.Timedelta(days=63) # start_time = datetime.datetime(start_time.year, start_time.month, 1) # x-2æ1æ¥ ~ x-1æ20æ¥ end_time = datetime.datetime(start_time.year, start_time.month, 1) - pd.Timedelta(days=3) end_time = datetime.datetime(end_time.year, end_time.month, 20) start_time = datetime.datetime(start_time.year, start_time.month, 1) - pd.Timedelta(days=33) start_time = datetime.datetime(start_time.year, start_time.month, 1) while (1): if (is_workday(start_time)): break start_time = start_time + pd.Timedelta(days=1) start_time = start_time + pd.Timedelta(days=1) print(start_time, '--', end_time, 'å约:', cu_list[i+cu_n1][2:], cu_list[i+cu_n2][2:]) # 读CUå¨start_timeåend_timeä¹é´çæ°æ® cu_t1 = pd.DatetimeIndex(pd.to_datetime(cu_df_list[i+cu_n1]['time'], format='%Y-%m-%d')) cu_t2 = pd.DatetimeIndex(pd.to_datetime(cu_df_list[i+cu_n2]['time'], format='%Y-%m-%d')) cu_price1 = np.array(cu_df_list[i+cu_n1]['æ¶çä»·(å )'], dtype=float) cu_price2 = np.array(cu_df_list[i+cu_n2]['æ¶çä»·(å )'], dtype=float) idx1 = np.where((start_time <= cu_t1) & (cu_t1 <= end_time)) cu_t1 = cu_t1[idx1] cu_price1 = cu_price1[idx1] idx2 = np.where((start_time <= cu_t2) & (cu_t2 <= end_time)) cu_t2 = cu_t2[idx2] cu_price2 = cu_price2[idx2] idx1 = np.isin(cu_t1, cu_t2) idx2 = np.isin(cu_t2, cu_t1) cu_t1 = cu_t1[idx1] cu_price1 = cu_price1[idx1] cu_price2 = cu_price2[idx2] print(len(cu_price1), len(cu_price2)) # 读BCå¨start_timeåend_timeä¹é´çæ°æ® bc_df_list[i+bc_n1].drop_duplicates('time', keep='last', inplace=True) bc_df_list[i+bc_n2].drop_duplicates('time', keep='last', inplace=True) bc_t1 = pd.DatetimeIndex(pd.to_datetime(bc_df_list[i+bc_n1]['time'], format='%Y-%m-%d')) bc_t2 = pd.DatetimeIndex(pd.to_datetime(bc_df_list[i+bc_n2]['time'], format='%Y-%m-%d')) bc_price1 = np.array(bc_df_list[i+bc_n1]['æ¶çä»·(å )'], dtype=float) bc_price2 = np.array(bc_df_list[i+bc_n2]['æ¶çä»·(å )'], dtype=float) idx1 = np.where((start_time <= bc_t1) & (bc_t1 <= end_time)) bc_t1 = bc_t1[idx1] bc_price1 = bc_price1[idx1] idx2 = np.where((start_time <= bc_t2) & (bc_t2 <= end_time)) bc_t2 = bc_t2[idx2] bc_price2 = bc_price2[idx2] idx1 = np.isin(bc_t1, bc_t2) idx2 = np.isin(bc_t2, bc_t1) bc_t1 = bc_t1[idx1] bc_price1 = bc_price1[idx1] bc_price2 = bc_price2[idx2] print(len(bc_price1), len(bc_price2)) cu_idx = np.isin(cu_t1, bc_t1) bc_idx = np.isin(bc_t1, cu_t1) cu_t1 = cu_t1[cu_idx] cu_price1 = cu_price1[cu_idx] cu_price2 = cu_price2[cu_idx] bc_price1 = bc_price1[bc_idx] bc_price2 = bc_price2[bc_idx] idx = np.empty((len(cu_t1)), dtype=bool) for k in range(len(cu_t1)): ts = pd.Timestamp(cu_t1[k]) hour = ts.hour minute = ts.minute if ((hour == 0 and minute >= 58) or (hour == 14 and minute >= 58) or (hour == 9 and minute <= 0) or (hour == 21 and minute <= 0) or (hour == 11 and minute >= 28)): idx[k] = False else: idx[k] = True cu_t1 = cu_t1[idx] cu_price1 = cu_price1[idx] cu_price2 = cu_price2[idx] bc_price1 = bc_price1[idx] bc_price2 = bc_price2[idx] if (i%2==0): fig1.line(cu_t1, (cu_price1-cu_price2)-(bc_price1-bc_price2), line_width=2, line_color=many_colors[i%2+1]) else: fig2.line(cu_t1, (cu_price1-cu_price2)-(bc_price1-bc_price2), line_width=2, line_color=many_colors[i%2+1]) t = np.concatenate([t, cu_t1]) cu_price_c1 = np.concatenate([cu_price_c1, cu_price1]) cu_price_c2 = np.concatenate([cu_price_c2, cu_price2]) bc_price_c1 = np.concatenate([bc_price_c1, bc_price1]) bc_price_c2 = np.concatenate([bc_price_c2, bc_price2]) # fig1 = figure(frame_width=1400, frame_height=600, x_axis_type = "datetime", title='xæ28æ¥ - x+1æ28æ¥, x+2,x+3å约') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" show(column(fig1,fig2)) cu_diff = cu_price_c1 - cu_price_c2 bc_diff = bc_price_c1 - bc_price_c2 slope, intercept, r, _, _ = linregress(cu_diff, bc_diff) print(linregress(cu_diff, bc_diff)) fig3 = figure(frame_width=900, frame_height=900) fig3.circle(x=cu_diff, y=bc_diff, color="purple", legend='x = 沪éä»·å·®, y = å½é éä»·å·®, y = 0.917*x - 89, r^2 = ' + str(r*r)) yy = cu_diff * slope + intercept fig3.line(x=cu_diff, y=yy, color="black") fig3.legend.location='top_left' # show(fig3) pass # ééæ¯ å å©ç def test12(): path = os.path.join(data_dir, 'å©ç'+'.csv') df = pd.read_csv(path) t0 = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) cn02y = np.array(df['ä¸å½å½åºæ¶çç2å¹´'], dtype=float) cn10y = np.array(df['ä¸å½å½åºæ¶çç10å¹´'], dtype=float) cn30y = np.array(df['ä¸å½å½åºæ¶çç30å¹´'], dtype=float) t01, cn02y = get_period_data(t0, cn02y, start_time, end_time) t02, cn10y = get_period_data(t0, cn10y, start_time, end_time) t03, cn30y = get_period_data(t0, cn30y, start_time, end_time) path1 = os.path.join(data_dir, 'éé¶'+'.csv') df1 = pd.read_csv(path1) t1 = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) gold_price_usd = np.array(df1['æè´§æ¶çä»·:COMEXé»é'], dtype=float) gold_price_cny = np.array(df1['æè´§æ¶çä»·(主å):é»é'], dtype=float) path2 = os.path.join(data_dir, 'é'+'.csv') df2 = pd.read_csv(path2) t2 = pd.DatetimeIndex(pd.to_datetime(df2['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df2['æè´§æ¶çä»·:LME3个æé'], dtype=float) shfe_cu = np.array(df2['æè´§æ¶çä»·(主å):é´æé'], dtype=float) t3, cu_au1 = data_div(t2, lme_cu_3m, t1, gold_price_usd) t4, cu_au2 = data_div(t2, shfe_cu/1.13, t1, gold_price_cny*31.103481) fig1 = figure(frame_width=1800, frame_height=300, tools=TOOLS, x_axis_type = "datetime") fig1.line(t3, cu_au1, line_width=2, line_color='gray', legend_label='ééæ¯usd') fig1.line(t4, cu_au2, line_width=2, line_color='black', legend_label='ééæ¯cny') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig2 = figure(frame_width=1800, frame_height=300, tools=TOOLS, x_range=fig1.x_range, x_axis_type = "datetime", y_axis_location="right") fig2.line(t01, cn02y/100, line_width=2, line_color='orange', legend_label='ä¸å½å½åºæ¶çç2å¹´') fig2.line(t02, cn10y/100, line_width=2, line_color='blue', legend_label='ä¸å½å½åºæ¶çç:10å¹´') fig2.yaxis[0].formatter = NumeralTickFormatter(format='0.0%') fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" show(column(fig1,fig2)) # LME 0-3 def test13(): path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) lme_cu_03 = np.array(df['LMEéåè´´æ°´(0-3)'], dtype=float) t00, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) t01, lme_cu_03 = get_period_data(t, lme_cu_03, start_time, end_time, remove_nan=True) fig1 = figure(frame_width=1800, frame_height=300, x_axis_type = "datetime") fig1.line(t01, lme_cu_3m, line_width=2, line_color='black', legend_label='æè´§æ¶çä»·:LME3个æé') fig1.y_range = Range1d(np.min(lme_cu_3m)*0.9, np.max(lme_cu_3m)*1.1) y_column2_name = 'y2' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(lme_cu_03)*1.1, end=np.max(lme_cu_03)*1.1, ), } fig1.line(t01, lme_cu_03, tools=TOOLS, line_width=2, color='blue', y_range_name=y_column2_name, legend_label='LMEéåè´´æ°´(0-3)') fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" show(fig1) # æ¾³å def test14(): start_time = '2005-1-1' end_time = '2029-12-31' path = os.path.join(data_dir, 'æ±ç'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) audusd = np.array(df['澳大å©äºå å ç¾å '], dtype=float) t3, audusd = get_period_data(t, audusd, start_time, end_time, remove_nan=True) idx = np.where(audusd > 5)[0][0] audusd[idx] = audusd[idx-1] path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) t, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) cftc_plot_financial(t3, audusd, 'CME AUDUSD', t, lme_cu_3m, 'LME3个æé', code='232741', inst_name='CME:AUDUSD') # 人æ°å¸ def test15(): start_time = '2016-1-1' end_time = '2028-12-31' path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) t00, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) path = os.path.join(data_dir, 'æ±ç'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) usdcny = np.array(df['å³ææ±ç:ç¾å å 人æ°å¸'], dtype=float) t01, usdcny = get_period_data(t, usdcny, start_time, end_time, remove_nan=True) fig1 = figure(frame_width=1800, frame_height=300, x_axis_type = "datetime", tools=TOOLS) fig1.line(t00, lme_cu_3m, line_width=2, line_color='black', legend_label='æè´§æ¶çä»·:LME3个æé') fig1.y_range = Range1d(np.min(lme_cu_3m)*0.9, np.max(lme_cu_3m)*1.1) y_column2_name = 'y2' fig1.extra_y_ranges = { y_column2_name: Range1d( start=np.min(usdcny)*0.9, end=np.max(usdcny)*1.1, ), } fig1.line(t01, usdcny, line_width=2, color='blue', y_range_name=y_column2_name, legend_label='USDCNY') fig1.add_layout(LinearAxis(y_range_name="y2"), 'right') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" show(fig1) # BCOM def test16(): start_time = '2005-1-1' end_time = '2029-12-31' path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) lme_cu_3m = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) t, lme_cu_3m = get_period_data(t, lme_cu_3m, start_time, end_time, remove_nan=True) cftc_plot_financial(t, lme_cu_3m, 'LME3个æé', code='221602', inst_name='CBT:BCOM') # CU/CNY å CU/USD def test17(): path = os.path.join(data_dir, 'é'+'.csv') df = pd.read_csv(path) t = pd.DatetimeIndex(pd.to_datetime(df['time'], format='%Y-%m-%d')) cu_price_usd = np.array(df['æè´§æ¶çä»·:LME3个æé'], dtype=float) cu_price_cny = np.array(df['æè´§æ¶çä»·(主å):é´æé'], dtype=float) t1, cu_price_cny = get_period_data(t, cu_price_cny, start_time, end_time, remove_nan=True) t2, cu_price_usd = get_period_data(t, cu_price_usd, start_time, end_time, remove_nan=True) path = os.path.join(data_dir, 'æ±ç'+'.csv') df1 = pd.read_csv(path) df1 = df1[['time', 'å³ææ±ç:ç¾å å 人æ°å¸']].dropna() dxy_t = pd.DatetimeIndex(pd.to_datetime(df1['time'], format='%Y-%m-%d')) usdcny = np.array(df1['å³ææ±ç:ç¾å å 人æ°å¸'], dtype=float) cu_price_cny = cu_price_cny/1.13 # å ³ç¨ t1, cu_price_cny_to_usd = data_div(t1, cu_price_cny, dxy_t, usdcny) t3, diff = data_sub(t1, cu_price_cny_to_usd, t2, cu_price_usd) fig1 = figure(frame_width=1400, frame_height=300, tools=TOOLS, x_axis_type = "datetime", y_axis_location="right") fig1.line(t1, cu_price_cny_to_usd, line_width=2, line_color='blue', legend_label='cu cny to usd') fig1.line(t2, cu_price_usd, line_width=2, line_color='orange', legend_label='cu usd') fig1.xaxis[0].ticker.desired_num_ticks = 20 fig1.legend.click_policy="hide" fig2 = figure(frame_width=1400, frame_height=300, tools=TOOLS, x_axis_type = "datetime", x_range=fig1.x_range, y_axis_location="right") fig2.line(t3, diff, line_width=2, line_color='blue', legend_label='CNY 溢价') fig2.xaxis[0].ticker.desired_num_ticks = 20 fig2.legend.click_policy="hide" show(column(fig1,fig2)) if __name__=="__main__": # ä»ä½ test1() test11() # åºå test2() # éåPMI test3() # # # # # éåMSCIææ° # test33() # # # # # ç²¾ç¼éè¿å£ # # # # test4() # ç°è´§åè´´æ°´ test5() time.sleep(0.25) # TC/RC test6() # # # # ééæ± # # # test66() # ééæ¯ å å©ç test12() # LME 0-3 # test13() # æ¾³å test14() # 人æ°å¸ test15() # # å½åååææ° # test16() # CU/CNY å CU/USD test17() # plot_option_position_basis_data('shfe', 'cu') # åè´´æ°´æçæ¯æè´§ä¸ç°è´§ç差价,ä¸åæç°è´§ææè´§ã # æLMEéç°è´§ä»·å¯¹3个ææ货价çåè´´æ°´ï¼è®¡ç®æ¹æ³æ¯åè´´æ°´(0-3)=ç°è´§æ¶çä»·æ ¼(åºå ç)-ä¸ä¸ªææè´§æ¶çä»·(åºå ç), # å³LMEéåè´´æ°´(0-3)=ç°è´§æ¶çä»·(åºå ç):LMEé-æè´§æ¶çä»·(åºå ç):LMEé # åè´´æ°´(0-3)âæç°è´§ä»·å¯¹3个ææ货价çåè´´æ°´ã # åè´´æ°´(3-15)âæ3个ææ货价对15个ææ货价çåè´´æ°´ã # "æ´å±±é溢价"æ¯æä»¥å¾ æ¥å ³è¿å£è³ä¸æµ·(å æ¬æ´å±±åå¤é«æ¡¥)çåºååçç²¾ç¼é交æä»·æ ¼å¯¹ä¼¦æ¦æ货交ææ(LME)ä¸æéçå½æ¥æº¢ä»·ã # ååºçæ¯ç±äºæ´å±±ï¼é´æ¥ååºä¸æµ·ãä¸å½ï¼ç°è´§éçä¾é导è´è´ä¹°ç°è´§ééè¦ä»åºçè¶ è¿éä»·æ¬èº«ï¼LMEä¸ææè´§éä»·ï¼çè¶ é¢é¨ååä»·ã # "éåè´´æ°´ é¿æ±æè²å¸åº"æ¯ç°è´§éä»·æ ¼ä¸æ²ªæéå½ææ货价çå·®å¼ï¼ææ ç®æ³ä¸æ²ªæéå½ææ货价ï¼åçæ¯æè¿æ份å约çæ货价ï¼æè´§ä»·æ ¼ä¸ºæ¶çä»· # "1#éåè´´æ°´"表示ç°è´§ä»·æ ¼ä¸æ²ªæéå½æå³æ¶åä»·æå½¢æçä»·å·®åºé´ï¼åçæ¯11:30çå¼ # åºåå°è®¡ä¸ºç¬¦å交å²åè´¨çè´§ç©æ°éï¼åºåæ货为已å¶æä»åçè´§ç©æ°éï¼å¯ç¨åºå®¹ä¸ºå¯å¶æä»åçæ°éã # éä¸é£çµæ¯GWèéé0.54ä¸å¨ï¼ # æµ·ä¸é£æ¯GWèéé1.53ä¸å¨éï¼ # å ¶ä»æèµæºææ°æ®æ¾ç¤ºï¼ # éä¸é£çµæ¯GWèéé0.4ä¸å¨ï¼ # æµ·ä¸é£çµæ¯GWèéé1ä¸å¨ã # ç»å两家ç»è®¡ååå¼ï¼ # é¢è®¡æµ·ä¸é£çµæ¯GWè£ æºéè¦æ¶è1.26ä¸å¨éï¼ # éä¸é£çµæ¯GWèéé0.47ä¸å¨ï¼ # é£çµèééä¸å¼çº¦ä¸º0.9ä¸å¨/GWã # Navigant Researchçæ°æ®ä¸º # å çµæ¯GWèéé0.55ä¸å¨ï¼ # Joint Research Centreæ°æ®ä¸º # æ¯GWèéé0.46ä¸å¨ï¼ä¹æèµæ认为å çµæ¯GWèéé0.4ä¸å¨ï¼å çµçèééä¸å¼çº¦ä¸º0.45ä¸å¨/GWã # 举ä¾æ¥è¯´ï¼è¿å£éç²¾ç¿å«é30%ï¼åæ¶ç 96.5%ï¼TC为45ç¾å /å¨ï¼RC 4.5 ç¾å/ç£ ï¼è®¡ç®å¦ä¸ï¼(1 å¨ =2204.62 ç£ ï¼1 ç¾å =100 ç¾å ) # ç²ç¼è´¹ï¼1÷Cu%÷åæ¶ç %ÃTC =1÷30%÷96.5%Ã45=155.44 ç¾å /å¨é # ç²¾ç¼è´¹ï¼RCÃ2204.62 ÷100ï¼ 4.5 Ã2204.62 ÷100ï¼99.21 ç¾å /å¨é # ç±æ¤ï¼ç»¼åå 工费ï¼ç²ç¼è´¹ï¼ç²¾ç¼è´¹ï¼155.44ï¼99.21ï¼254.65 ç¾å /å¨é # ä¸å°ç©ºè°å«éï¼8.3kg(估计) # ä¸å°å°ç®±å«éï¼ # ä¸å°çº¯çµå¨è½¦å«éï¼83kg