Пример #1
0
    def test_with_single_bar_multi_time(self):
        bar0 = self.app_context.inst_data_mgr.get_series("bar0")
        bar1 = self.app_context.inst_data_mgr.get_series("bar1")

        bar0.start(self.app_context)
        bar1.start(self.app_context)

        plus = Plus(bar0, bar1, input_key='close')
        minus = Minus(bar0, bar1, input_key='close')
        times = Times(bar0, bar1, input_key='close')
        divides = Divides(bar0, bar1, input_key='close')
        pcorr = PairCorrelation(bar0, bar1, length=4, input_key='close')

        plus.start(self.app_context)
        minus.start(self.app_context)
        times.start(self.app_context)
        divides.start(self.app_context)
        pcorr.start(self.app_context)

        now = datetime.datetime.now()
        x = np.array([80.0, 102.0, 101.0, 99.0])
        y = np.array([95.0, 98.0, 105.2, 103.3])
        ts = [now + datetime.timedelta(0, i * 3) for i in range(4)]
        x_p_y = x + y
        x_m_y = x - y
        x_t_y = x * y
        x_d_y = x / y

        bar0.add({"timestamp": ts[0], "close": x[0], "open": 0})
        bar1.add({"timestamp": ts[0], "close": y[0], "open": 0})

        self.assertEqual(plus.now('value'), 175.0)
        self.assertEqual(minus.now('value'), -15.0)
        self.assertEqual(times.now('value'), 7600.0)
        self.assertEqual(divides.now('value'), 80.0 / 95.0)

        bar0.add({"timestamp": ts[1], "close": x[1], "open": 0})
        bar1.add({"timestamp": ts[1], "close": y[1], "open": 0})

        self.assertEqual(plus.now('value'), 200.0)
        self.assertEqual(minus.now('value'), 4.0)
        self.assertEqual(times.now('value'), 102.0 * 98.0)
        self.assertEqual(divides.now('value'), 102.0 / 98.0)

        bar0.add({"timestamp": ts[2], "close": x[2], "open": 0})
        bar1.add({"timestamp": ts[2], "close": y[2], "open": 0})

        bar0.add({"timestamp": ts[3], "close": x[3], "open": 0})
        bar1.add({"timestamp": ts[3], "close": y[3], "open": 0})

        # self.assertEqual(pcorr.now('value'), np.corrcoef(x, y))
        self.__np_assert_almost_equal(pcorr.now('value'), np.corrcoef(x, y))
    def test_with_single_bar_multi_time(self):
        bar0 = self.app_context.inst_data_mgr.get_series("bar0")
        bar1 = self.app_context.inst_data_mgr.get_series("bar1")

        bar0.start(self.app_context)
        bar1.start(self.app_context)

        plus = Plus(bar0, bar1, input_key='close')
        minus = Minus(bar0, bar1, input_key='close')
        times = Times(bar0, bar1, input_key='close')
        divides = Divides(bar0, bar1, input_key='close')
        pcorr = PairCorrelation(bar0, bar1, length=4, input_key='close')

        plus.start(self.app_context)
        minus.start(self.app_context)
        times.start(self.app_context)
        divides.start(self.app_context)
        pcorr.start(self.app_context)

        now = datetime.datetime.now()
        x = np.array([80.0, 102.0, 101.0, 99.0])
        y = np.array([95.0, 98.0, 105.2, 103.3])
        ts = [now + datetime.timedelta(0, i*3) for i in range(4)]
        x_p_y = x + y
        x_m_y = x - y
        x_t_y = x * y
        x_d_y = x / y

        bar0.add({"timestamp": ts[0], "close": x[0], "open": 0})
        bar1.add({"timestamp": ts[0], "close": y[0], "open": 0})

        self.assertEqual(plus.now('value'), 175.0)
        self.assertEqual(minus.now('value'), -15.0)
        self.assertEqual(times.now('value'), 7600.0)
        self.assertEqual(divides.now('value'), 80.0/95.0)

        bar0.add({"timestamp": ts[1], "close": x[1], "open": 0})
        bar1.add({"timestamp": ts[1], "close": y[1], "open": 0})

        self.assertEqual(plus.now('value'), 200.0)
        self.assertEqual(minus.now('value'), 4.0)
        self.assertEqual(times.now('value'), 102.0*98.0)
        self.assertEqual(divides.now('value'), 102.0/98.0)

        bar0.add({"timestamp": ts[2], "close": x[2], "open": 0})
        bar1.add({"timestamp": ts[2], "close": y[2], "open": 0})

        bar0.add({"timestamp": ts[3], "close": x[3], "open": 0})
        bar1.add({"timestamp": ts[3], "close": y[3], "open": 0})

        # self.assertEqual(pcorr.now('value'), np.corrcoef(x, y))
        self.__np_assert_almost_equal(pcorr.now('value'), np.corrcoef(x, y))
Пример #3
0
    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"])
Пример #4
0
    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"])