def testPlotAssignedMergedPrice(self): start = datetime(2019, 2, 18) end = datetime(2019, 2, 22) call = 2550 put = 2550 close = self.obj.getMergedPrice(start, end, self.obj.getAssignedContract, callStrikePrice=call, putStrikePrice=put) self.obj.plotMergedPrice( '{}-to-{}-straddle'.format(dateToStr(start), dateToStr(end)), close) call = 2750 put = 2750 start = datetime(2019, 2, 25) end = datetime(2019, 3, 1) close = self.obj.getMergedPrice(start, end, self.obj.getAssignedContract, callStrikePrice=call, putStrikePrice=put) # close.to_csv(getTestPath('bug.csv')) self.obj.plotMergedPrice( '{}-to-{}-straddle'.format(dateToStr(start), dateToStr(end)), close)
def testDateConvert(self): # self.setTestData() print('Remove Dash. {} to {}'.format(self.dateWithDash, fn.rmDateDash(self.dateWithDash))) print('Add Dash. {} to {}'.format(self.dateWithoutDash, fn.addDateDash( self.dateWithoutDash))) print('TodayStr: {}'.format(fn.getTodayStr())) print('strToDate 1: {}'.format(fn.strToDate(self.dateWithDash))) print('strToDate 2: {}'.format(fn.strToDate(self.dateWithoutDash))) print(fn.strToDate('2018-04-20')) print(fn.dateToStr(self.dt)) print(fn.dateToDtStr(self.dt))
def analyze_ohlc_relationship(data, a, b, direction): """ 研究OHLC的关系 :param data: DataFrame :param a: :param b: :param direction: :return: """ df = data # print(df.index) start_date = pd.to_datetime(df.index.values[0]) end_date = pd.to_datetime(df.index.values[-1]) print(start_date) all_trade_days = len(df) abs_change_name = '{}_{}_abs'.format(a, b) relative_change_name = '{}_{}_ratio'.format(a, b) df[abs_change_name] = df[b] - df[a] df[relative_change_name] = (df[abs_change_name] / df[a]) * 100 if direction == 'up': df2 = df[df[abs_change_name] > 0] zh_cn = u'大于' elif direction == 'down': df2 = df[df[abs_change_name] < 0] zh_cn = u'小于' else: print('Wrong Direction') return df2 = copy(df2) select_trade_days = len(df2) select_ratio = float(select_trade_days) / float(all_trade_days) * 100 for i in [5, 10, 20]: abs_ma_name = '{}_{}_ma{}'.format(a[0], b[0], i) df2[abs_ma_name] = df2[abs_change_name].rolling(i).mean() ratio_ma_name = '{}_{}_ratio_ma{}'.format(a[0], b[0], i) df2[ratio_ma_name] = df2[relative_change_name].rolling(i).mean() new_file = 'vix_bar_{}_{}_ma.csv'.format(a, b) fp2 = os.path.join(getDataDir(), RESEARCH, OPTION, 'qvix', new_file) df2.to_csv(fp2) print('-' * 30) print(u'自{}_{},期权论坛波值{}{}{}的交易统计:'.format(dateToStr(start_date), dateToStr(end_date), b, zh_cn, a)) print('-' * 30) print(u'总交易日:%s' % all_trade_days) print(u'%s%s%s的交易日:%s, 占比:%.3f %%' % (b, zh_cn, a, select_trade_days, select_ratio)) # for n in range(0, 15): # ratio_array = df2[relative_change_name].values() # count = len(ratio_array) # df_n = df[df['h-o-ratio'] > n] # c_n = len(df_n) # print(u'百分比超过%s交易日:%s,占比:%.3f %%' % (n, c_n, (float(c_n) / float(h_gt_o_trade_days)) * 100)) fig = plt.figure(figsize=(16, 12)) ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) ax1.set_title(u'Ratio({} to {}) Distribution'.format(a, b)) ax2.set_title(u'Ratio({} to {}) Cumulative Distribution'.format(a, b)) plt.subplots_adjust(hspace=0.2) data = df[relative_change_name].values sns.distplot(data, bins=100, color='g', ax=ax1) sns.kdeplot(data, color='r', cumulative=True, ax=ax2) # ax1.set_xticks(ax1.get_xticks() / 2) # ax2.set_xticks(ax2.get_xticks() / 2) # ax1.set_yticks(ax1.get_yticks() / 2) # ax2.set_yticks(ax2.get_yticks() / 2) ax1.xaxis.set_major_locator(ticker.MultipleLocator(5)) ax2.xaxis.set_major_locator(ticker.MultipleLocator(5)) ax2.yaxis.set_major_locator(ticker.MultipleLocator(0.1)) filename = '{}_to_{}_{}_{}_relationship.png'.format(dateToStr(start_date), dateToStr(end_date), a, b) fig.savefig(getTestPath(filename))