def test_with_pair_corr_with_vec(self): bar0 = self.app_context.inst_data_mgr.get_series("bar0") bar1 = self.app_context.inst_data_mgr.get_series("bar1") bar2 = self.app_context.inst_data_mgr.get_series("bar2") bar3 = self.app_context.inst_data_mgr.get_series("bar3") bar0.start(self.app_context) bar1.start(self.app_context) bar2.start(self.app_context) bar3.start(self.app_context) vec0 = MakeVector([bar0, bar1], input_key='close') vec1 = MakeVector([bar2, bar3], input_key='close') vec0.start(self.app_context) vec1.start(self.app_context) pcorr = PairCorrelation(vec0, vec1, length=4, input_key=PipeLine.VALUE) pcorr.start(self.app_context) now = datetime.datetime.now() x0 = np.array([80.0, 102.0, 101.0, 99.0 ]) x1 = np.array([102.0, 101.5, 99.0, 97.0]) x2 = np.array([94.0, 98.5, 91.0, 87.0]) x3 = np.array([104.5, 107.5, 97.0, 91.0]) ts = [now + datetime.timedelta(0, i*3) for i in range(4)] for i in range(4): bar0.add({"timestamp": ts[i], "close": x0[i], "open": 0}) bar1.add({"timestamp": ts[i], "close": x1[i], "open": 0}) bar2.add({"timestamp": ts[i], "close": x2[i], "open": 0}) bar3.add({"timestamp": ts[i], "close": x3[i], "open": 0}) x = np.vstack([x0, x1]) y = np.vstack([x2, x3]) self.__np_assert_almost_equal(pcorr.now('value'), np.corrcoef(x, y))
def test_with_pair_corr_with_vec(self): bar0 = self.app_context.inst_data_mgr.get_series("bar0") bar1 = self.app_context.inst_data_mgr.get_series("bar1") bar2 = self.app_context.inst_data_mgr.get_series("bar2") bar3 = self.app_context.inst_data_mgr.get_series("bar3") bar0.start(self.app_context) bar1.start(self.app_context) bar2.start(self.app_context) bar3.start(self.app_context) vec0 = MakeVector([bar0, bar1], input_key='close') vec1 = MakeVector([bar2, bar3], input_key='close') vec0.start(self.app_context) vec1.start(self.app_context) pcorr = PairCorrelation(vec0, vec1, length=4, input_key=PipeLine.VALUE) pcorr.start(self.app_context) now = datetime.datetime.now() x0 = np.array([80.0, 102.0, 101.0, 99.0]) x1 = np.array([102.0, 101.5, 99.0, 97.0]) x2 = np.array([94.0, 98.5, 91.0, 87.0]) x3 = np.array([104.5, 107.5, 97.0, 91.0]) ts = [now + datetime.timedelta(0, i * 3) for i in range(4)] for i in range(4): bar0.add({"timestamp": ts[i], "close": x0[i], "open": 0}) bar1.add({"timestamp": ts[i], "close": x1[i], "open": 0}) bar2.add({"timestamp": ts[i], "close": x2[i], "open": 0}) bar3.add({"timestamp": ts[i], "close": x3[i], "open": 0}) x = np.vstack([x0, x1]) y = np.vstack([x2, x3]) self.__np_assert_almost_equal(pcorr.now('value'), np.corrcoef(x, y))
def test_sync(self): bar0 = self.app_context.inst_data_mgr.get_series("bar0") bar1 = self.app_context.inst_data_mgr.get_series("bar1") bar2 = self.app_context.inst_data_mgr.get_series("bar2") bar3 = self.app_context.inst_data_mgr.get_series("bar3") bar4 = self.app_context.inst_data_mgr.get_series("bar4") bar5 = self.app_context.inst_data_mgr.get_series("bar5") bar6 = self.app_context.inst_data_mgr.get_series("bar6") bar7 = self.app_context.inst_data_mgr.get_series("bar7") bar0.start(self.app_context) bar1.start(self.app_context) bar2.start(self.app_context) bar3.start(self.app_context) bar4.start(self.app_context) bar5.start(self.app_context) bar6.start(self.app_context) bar7.start(self.app_context) basket = MakeVector(inputs=[bar0, bar1, bar2, bar3], input_keys='close') basket2 = MakeVector(inputs=[bar4, bar5, bar6, bar7], input_keys='close') basket.start(self.app_context) basket2.start(self.app_context) basket_open = MakeVector(inputs=[bar0, bar1, bar2, bar3], input_keys='open') basket_open2 = MakeVector(inputs=[bar4, bar5, bar6, bar7], input_keys='open') cross_basket_spread = Minus(inputs=[basket2, basket], input_keys=PipeLine.VALUE) basket_open.start(self.app_context) basket_open2.start(self.app_context) cross_basket_spread.start(self.app_context) nan_arr = np.empty([1, 4]) nan_arr[:] = np.nan t1 = 1 bar0.add(data={"timestamp": t1, "close": 80.0, "open": 0}) self.__np_assert_almost_equal(nan_arr, basket.now()["value"]) bar1.add(data={"timestamp": t1, "close": 95.0, "open": 0}) self.__np_assert_almost_equal(nan_arr, basket.now()["value"]) bar2.add(data={"timestamp": t1, "close": 102.0, "open": 0}) self.__np_assert_almost_equal(nan_arr, basket.now()["value"]) sync_vec = np.array([[80.0, 95.0, 102.0, 105.0]]) bar3.add(data={"timestamp": t1, "close": 105.0, "open": 0}) self.__np_assert_almost_equal(sync_vec, basket.now()["value"]) bar4.add(data={"timestamp": t1, "close": 102.0, "open": 0}) bar5.add(data={"timestamp": t1, "close": 95.0, "open": 0}) bar6.add(data={"timestamp": t1, "close": 107.0, "open": 0}) bar7.add(data={"timestamp": t1, "close": 101.0, "open": 0}) sync_vec2 = np.array([[102.0, 95.0, 107.0, 101.0]]) self.__np_assert_almost_equal(sync_vec2, basket2.now()["value"]) target_spread = np.array([[22.0, 0.0, 5.0, -4.0]]) self.__np_assert_almost_equal(target_spread, cross_basket_spread.now()["value"]) self.__np_assert_almost_equal(sync_vec, basket.now()["value"])
def test_sync(self): bar0 = self.app_context.inst_data_mgr.get_series("bar0") bar1 = self.app_context.inst_data_mgr.get_series("bar1") bar2 = self.app_context.inst_data_mgr.get_series("bar2") bar3 = self.app_context.inst_data_mgr.get_series("bar3") bar4 = self.app_context.inst_data_mgr.get_series("bar4") bar5 = self.app_context.inst_data_mgr.get_series("bar5") bar6 = self.app_context.inst_data_mgr.get_series("bar6") bar7 = self.app_context.inst_data_mgr.get_series("bar7") bar0.start(self.app_context) bar1.start(self.app_context) bar2.start(self.app_context) bar3.start(self.app_context) bar4.start(self.app_context) bar5.start(self.app_context) bar6.start(self.app_context) bar7.start(self.app_context) basket = MakeVector([bar0, bar1, bar2, bar3], input_key='close') basket2 = MakeVector([bar4, bar5, bar6, bar7], input_key='close') basket.start(self.app_context) basket2.start(self.app_context) basket_open = MakeVector([bar0, bar1, bar2, bar3], input_key='open') basket_open2 = MakeVector([bar4, bar5, bar6, bar7], input_key='open') cross_basket_spread = Minus(basket2, basket, input_key=PipeLine.VALUE) basket_open.start(self.app_context) basket_open2.start(self.app_context) cross_basket_spread.start(self.app_context) nan_arr = np.empty([1,4]) nan_arr[:] = np.nan t1 = datetime.datetime.now() bar0.add({"timestamp": t1, "close": 80.0, "open": 0}) # self.assertListEqual(nan_arr.tolist(), basket.now()["value"]) self.__np_assert_almost_equal(nan_arr, np.array(basket.now()["value"])) bar1.add({"timestamp": t1, "close": 95.0, "open": 0}) # self.assertListEqual(nan_arr.tolist(), basket.now()["value"]) self.__np_assert_almost_equal(nan_arr, basket.now()["value"]) bar2.add({"timestamp": t1, "close": 102.0, "open": 0}) # self.assertListEqual(nan_arr.tolist(), basket.now()["value"]) self.__np_assert_almost_equal(nan_arr, np.array(basket.now()["value"])) # sync_vec = np.array([[80.0, 95.0, 102.0, 105.0]]) sync_vec = [[80.0, 95.0, 102.0, 105.0]] bar3.add({"timestamp": t1, "close": 105.0, "open": 0}) # self.__np_assert_almost_equal(sync_vec, basket.now()["value"]) self.assertAlmostEqual(sync_vec, basket.now()["value"]) bar4.add({"timestamp": t1, "close": 102.0, "open": 0}) bar5.add({"timestamp": t1, "close": 95.0, "open": 0}) bar6.add({"timestamp": t1, "close": 107.0, "open": 0}) bar7.add({"timestamp": t1, "close": 101.0, "open": 0}) # sync_vec2 = np.array([[102.0, 95.0, 107.0, 101.0]]) sync_vec2 = [[102.0, 95.0, 107.0, 101.0]] self.assertAlmostEqual(sync_vec2, basket2.now()["value"]) # self.__np_assert_almost_equal(sync_vec2, basket2.now()["value"]) # target_spread = np.array([[22.0, 0.0, 5.0, -4.0]]) target_spread = [[22.0, 0.0, 5.0, -4.0]] self.assertAlmostEqual(target_spread, cross_basket_spread.now()["value"]) self.assertAlmostEqual(sync_vec, basket.now()["value"])