Beispiel #1
0
 def testRunOverTimeFrame(self):
     """test for generating the delivery date"""
     seq, days = run_over_time_frame()
     self.assertEqual('2013-01-16', seq[0])
     self.assertEqual('2013-02-13', seq[1])
     self.assertEqual('2013-03-20', seq[2])
     self.assertEqual('2013-04-17', seq[3])
     self.assertEqual('2013-05-22', seq[4])
     self.assertEqual('2013-06-19', seq[5])
     self.assertEqual('2013-07-17', seq[6])
     self.assertEqual('2013-08-21', seq[7])
     self.assertEqual('2013-09-18', seq[8])
     self.assertEqual('2013-10-16', seq[9])
     self.assertEqual('2013-11-20', seq[10])
     self.assertEqual('2013-12-18', seq[11])
     self.assertEqual('2019-08-21', seq[79])
     self.assertEqual('2019-09-18', seq[80])
     self.assertEqual('2019-10-16', seq[81])
     self.assertEqual('2019-11-20', seq[82])
     self.assertEqual('2019-12-18', seq[83])
     self.assertEqual('2020-01-22', seq[84])
     self.assertEqual('2020-02-19', seq[85])
     self.assertEqual('2020-03-18', seq[86])
     self.assertEqual('2020-04-15', seq[87])
     self.assertEqual('2020-05-20', seq[88])
     self.assertEqual('2020-06-17', seq[89])
     self.assertEqual('2020-07-22', seq[90])
     self.assertEqual('2020-08-19', seq[91])
     self.assertEqual('2020-09-16', seq[92])
     self.assertEqual('2020-10-21', seq[93])
     self.assertEqual('2020-11-18', seq[94])
     self.assertEqual('2020-12-16', seq[95])
 def testDeliveryDates(self):
     """"""
     delivery_dates, schedul_days = run_over_time_frame()
     flist = filter_delivery_dates(delivery_dates, '2020-09-10')
     np.testing.assert_array_equal(['2019-09-18', '2019-10-16',
                                    '2019-11-20', '2019-12-18',
                                    '2020-01-22', '2020-02-19',
                                    '2020-03-18', '2020-04-15',
                                    '2020-05-20', '2020-06-17',
                                    '2020-07-22', '2020-08-19'], flist)
     flist2 = shift_delivery_dates(delivery_dates, flist, -1)
     np.testing.assert_array_equal(['2019-08-21', '2019-09-18', '2019-10-16',
                                    '2019-11-20', '2019-12-18',
                                    '2020-01-22', '2020-02-19',
                                    '2020-03-18', '2020-04-15',
                                    '2020-05-20', '2020-06-17',
                                    '2020-07-22'], flist2)
     flist3 = shift_delivery_dates(delivery_dates, flist2, -1)
     np.testing.assert_array_equal(['2019-07-17', '2019-08-21',
                                    '2019-09-18', '2019-10-16',
                                    '2019-11-20', '2019-12-18',
                                    '2020-01-22', '2020-02-19',
                                    '2020-03-18', '2020-04-15',
                                    '2020-05-20', '2020-06-17'], flist3)
     flist4 = shift_delivery_dates(delivery_dates, flist, -2)
     np.testing.assert_array_equal(flist4, flist3)
 def testCombineAll(self):
     """"""
     self.assertEqual((True, '2020-08-19'), is_futures_file('2020-08-19.csv'))
     self.assertEqual((True, '2020-09-16'), is_futures_file('2020-09-16.csv'))
     self.assertEqual((True, '2020-08-19'), is_futures_file('./data/2020-08-19.csv'))
     self.assertEqual((False, None), is_futures_file('VIX.csv'))
     delivery_dates, schedule_days = run_over_time_frame()
     info = combine_all(delivery_dates, TEST_DATA_ROOT)
     self.assertEqual((211, 12), info.shape)
     np.testing.assert_array_equal([28.775, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2020-09-10'])
     np.testing.assert_array_equal([21.71, 26.225, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2020-08-19'])
     np.testing.assert_array_equal([28.775, 30.275, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2020-07-23'])
     np.testing.assert_array_equal([0, 27.25, 29.125, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2020-07-22'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 17.175, 17.475, 0, 0, 0], info.loc['2020-01-17'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.6, 19.1, 0, 0], info.loc['2019-12-12'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 19.075, 19.2, 0, 0], info.loc['2019-12-09'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.75, 0, 0, 0], info.loc['2019-11-25'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2013-01-16'])
     np.testing.assert_array_equal([14.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2013-01-15'])
     np.testing.assert_array_equal([15.6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], info.loc['2013-01-02'])
     delta_p = analyze_diff_percent(info)
     np.testing.assert_array_equal(
         [np.nan, -1, np.nan, np.nan, np.nan, np.nan,
          np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], delta_p.loc['2020-09-10'])
     np.testing.assert_array_almost_equal(
         [np.nan, (26.225 - 21.71) / 21.71, -1, np.nan, np.nan, np.nan,
          np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], delta_p.loc['2020-08-19'])
     np.testing.assert_array_almost_equal(
         [np.nan, (30.275 - 28.775) / 28.775, -1, np.nan, np.nan, np.nan,
          np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], delta_p.loc['2020-07-23'])
     np.testing.assert_array_almost_equal(
         [np.nan, np.inf, (29.125 - 27.25) / 27.25, -1, np.nan, np.nan,
          np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], delta_p.loc['2020-07-22'])
 def testNotificationMsg(self):
     """"""
     delivery_dates, schedule_days = run_over_time_frame()
     mgr = VIXDataManager(delivery_dates)
     df = mgr.combine_all()
     rets_v = mgr.analyze()
     gvz = GVZDataManager()
     rets_g = gvz.analyze()
     ovx = OVXDataManager()
     rets_o = ovx.analyze()
     params = mk_notification_params(df, delivery_dates, rets_v, rets_g, rets_o)
     msg = mk_notification(**params)
     print(msg)
 def do_timeout(self):
     """"""
     logger.info('start schedule task. ')
     delivery_dates, schedule_days = run_over_time_frame()
     self._last_day = get_last_day(self._update_hour)
     self._day_index = datetime.strftime(self._last_day, DATE_FORMAT)
     if not is_business_day(self._last_day, schedule_days):
         logger.info('last day is not a business day. ')
         return self.clear_and_return_true()
     vdm = VIXDataManager(delivery_dates)
     vdm.download_raw_data(self._day_vix_downloaded)
     df = vdm.combine_all()
     rets_vix = vdm.analyze()
     if rets_vix['vix_diff'].index[-1] != self._day_index or \
        self._day_index not in rets_vix['vix'].index:
         # vix diff is not pulled, retry 5 minutes later
         logger.info(
             f"last_day: {self._last_day}, index: {self._day_index}, vix_index: {rets_vix['vix'].index[-1]}, vix_diff_index: {rets_vix['vix_diff'].index[-1]}"
         )
         logger.info("vix info download failed. ")
         return False
     elif df.iloc[-1][0] <= 1 or df.iloc[-1][1] <= 1:
         logger.info(
             f'vix info download failed due to vix 0 or 1 is zero. ')
         return False
     self._day_vix_downloaded = True
     # gvz futures are delisted
     gvzm = GVZDataManager([])
     gvzm.download_raw_data(self._day_gvz_downloaded)
     rets_gvzm = gvzm.analyze()
     if self._day_index not in rets_gvzm['gvz'].index:
         logger.info("gvz info download failed. ")
         return False
     self._day_gvz_downloaded = True
     # ovx futures are delisted
     ovxm = OVXDataManager([])
     ovxm.download_raw_data(self._day_ovx_downloaded)
     rets_ovxm = ovxm.analyze()
     if self._day_index not in rets_ovxm['ovx'].index:
         logger.info("ovx info download failed. ")
         return False
     self._day_ovx_downloaded = True
     params = mk_notification_params(df, delivery_dates, rets_vix,
                                     rets_gvzm, rets_ovxm)
     title, msg = mk_notification(**params)
     if self._push_msg:
         send_md_msg(title, msg)
     logger.info('schedule task done. ')
     return self.clear_and_return_true()
 def testTermStucture(self):
     """for mask test"""
     delivery_dates, schedule_days = run_over_time_frame()
     futures_0916 = load_futures_by_csv(os.path.join(TEST_DATA_ROOT, '2020-09-16.csv'))
     term1 = generate_term_structure(
         delivery_dates, futures_0916, '2020-09-10')
     self.assertEqual((191, 12), term1.shape)
     np.testing.assert_array_equal([28.775, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term1.loc['2020-09-10'])
     np.testing.assert_array_equal([0, 26.225, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term1.loc['2020-08-19'])
     np.testing.assert_array_equal([0, 30.275, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term1.loc['2020-07-23'])
     np.testing.assert_array_equal([0, 0, 29.125, 0, 0, 0, 0, 0, 0, 0, 0, 0], term1.loc['2020-07-22'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 17.475, 0, 0, 0], term1.loc['2020-01-17'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 19.1, 0, 0], term1.loc['2019-12-12'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 19.2, 0, 0], term1.loc['2019-12-09'])
     # boundary test for deliver date
     futures_0819 = load_futures_by_csv(os.path.join(TEST_DATA_ROOT, '2020-08-19.csv'))
     term2 = generate_term_structure(
         delivery_dates, futures_0819, '2020-08-19')
     np.testing.assert_array_equal([21.71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term2.loc['2020-08-19'])
     np.testing.assert_array_equal([28.775, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term2.loc['2020-07-23'])
     np.testing.assert_array_equal([0, 27.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term2.loc['2020-07-22'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 17.175, 0, 0, 0, 0], term2.loc['2020-01-17'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.6, 0, 0, 0], term2.loc['2019-12-12'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 19.075, 0, 0, 0], term2.loc['2019-12-09'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.75, 0, 0, 0], term2.loc['2019-11-25'])
     # test for combine
     term3 = combine_data(term1, term2)
     np.testing.assert_array_equal([28.775, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term3.loc['2020-09-10'])
     np.testing.assert_array_equal([21.71, 26.225, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term3.loc['2020-08-19'])
     np.testing.assert_array_equal([28.775, 30.275, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], term3.loc['2020-07-23'])
     np.testing.assert_array_equal([0, 27.25, 29.125, 0, 0, 0, 0, 0, 0, 0, 0, 0], term3.loc['2020-07-22'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 17.175, 17.475, 0, 0, 0], term3.loc['2020-01-17'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.6, 19.1, 0, 0], term3.loc['2019-12-12'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 19.075, 19.2, 0, 0], term3.loc['2019-12-09'])
     np.testing.assert_array_equal([0, 0, 0, 0, 0, 0, 0, 0, 18.75, 0, 0, 0], term3.loc['2019-11-25'])
     futures_0116 = load_futures_by_csv(os.path.join(TEST_DATA_ROOT, '2013-01-16.csv'))
     term4 = generate_term_structure(delivery_dates, futures_0116, '2013-01-16')
     np.testing.assert_array_equal([0], term4.loc['2013-01-16'])
     np.testing.assert_array_equal([14.2], term4.loc['2013-01-15'])
     np.testing.assert_array_equal([15.6], term4.loc['2013-01-02'])
     futures_0116 = load_futures_by_csv(os.path.join(TEST_DATA_ROOT, '2012-01-16.csv'))
     term5 = generate_term_structure(delivery_dates, futures_0116, '2012-01-16')
     self.assertEqual(None, term5)
Beispiel #7
0
def get_vix_info(last_date):
    # query data from the data manager
    # param last_date is for lru_cache only
    delivery_dates, schedule_days = run_over_time_frame()
    vdm = VIXDataManager(delivery_dates)
    df = vdm.combine_all(24)
    rets_vix = vdm.analyze()
    gvzm = GVZDataManager([])
    rets_gvzm = gvzm.analyze()
    ovxm = OVXDataManager([])
    rets_ovxm = ovxm.analyze()
    vix_diff = rets_vix['vix_diff'][[1]]
    vix_diff.rename({1: 'diff'}, axis=1, inplace=True)
    df_gvz = rets_gvzm['gvz'][[CLOSE_PRICE_NAME]]
    df_gvz.rename({CLOSE_PRICE_NAME: 'gvz'}, axis=1, inplace=True)
    df_ovx = rets_ovxm['ovx'][[CLOSE_PRICE_NAME]]
    df_ovx.rename({CLOSE_PRICE_NAME: 'ovx'}, axis=1, inplace=True)
    df = df.join(vix_diff)
    df = df.join(df_gvz)
    df = df.join(df_ovx)
    return df, delivery_dates