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_name(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) bar0_plus_bar1 = Plus(bar0, bar1, input_key='close') bar0_plus_bar1.start(self.app_context) self.assertEquals("Plus('bar0','bar1',close)", bar0_plus_bar1.name) spread = Minus(bar0, bar1, input_key='close') spread.start(self.app_context) self.assertEquals("Minus('bar0','bar1',close)", spread.name)
def test_name(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) bar0_plus_bar1 = Plus(inputs=[bar0, bar1], input_keys='close') bar0_plus_bar1.start(self.app_context) self.assertEquals("Plus(bar0[close],bar1[close],length=1)", bar0_plus_bar1.name) spread = Minus(inputs=[bar0, bar1], input_keys='close') spread.start(self.app_context) self.assertEquals("Minus(bar0[close],bar1[close],length=1)", spread.name)
def test_name(self): bar = self.app_context.inst_data_mgr.get_series("bar") bar.start(self.app_context) sma3 = SMA(inputs=bar, input_keys='close', length=3) sma20 = SMA(inputs=bar, input_keys='close', length=20) sma50 = SMA(inputs=bar, input_keys='close', length=50) sma3.start(self.app_context) sma20.start(self.app_context) sma50.start(self.app_context) rank = Rank(inputs=[sma3, sma20, sma50], input_keys='close') rank.start(self.app_context) self.assertEquals( "Rank(SMA(bar[close],length=3)[close],SMA(bar[close],length=20)[close],SMA(bar[close],length=50)[close],ascending=True)", rank.name) 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) barlist = [bar0, bar1, bar2, bar3] avg = Average(inputs=barlist, input_keys='close') gssum = GSSum(inputs=barlist, input_keys='close') basket = MakeVector(inputs=barlist, input_keys='close') # TODO: the name printed by pipeline now break the "parse" machnism so we have to review it self.assertEquals( "Average(bar0[close],bar1[close],bar2[close],bar3[close],length=1)", avg.name) self.assertEquals( "Sum(bar0[close],bar1[close],bar2[close],bar3[close],length=1)", gssum.name) self.assertEquals( "MakeVector(bar0[close],bar1[close],bar2[close],bar3[close],length=1)", basket.name) 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") basket2 = MakeVector(inputs=[bar4, bar5, bar6, bar7], input_keys='close') self.assertEquals( "MakeVector(bar4[close],bar5[close],bar6[close],bar7[close],length=1)", basket2.name) cross_basket_spread = Minus(inputs=[basket2, basket]) self.assertEquals( "Minus(MakeVector(bar4[close],bar5[close],bar6[close],bar7[close],length=1)[close],MakeVector(bar0[close],bar1[close],bar2[close],bar3[close],length=1)[close],length=1)", cross_basket_spread.name)
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_name(self): bar = self.app_context.inst_data_mgr.get_series("bar") bar.start(self.app_context) sma3 = SMA(bar, input_key='close', length=3) sma20 = SMA(bar, input_key='close', length=20) sma50 = SMA(bar, input_key='close', length=50) sma3.start(self.app_context) sma20.start(self.app_context) sma50.start(self.app_context) rank = Rank([sma3, sma20, sma50], input_key='close') rank.start(self.app_context) self.assertEquals( "Rank(SMA('bar',close,3),SMA('bar',close,20),SMA('bar',close,50),close)", rank.name) 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) barlist = [bar0, bar1, bar2, bar3] avg = Average([bar0, bar1, bar2, bar3], input_key='close') gssum = GSSum([bar0, bar1, bar2, bar3], input_key='close') basket = MakeVector([bar0, bar1, bar2, bar3], input_key='close') # TODO: the name printed by pipeline now break the "parse" machnism so we have to review it self.assertEquals("Average('bar0','bar1','bar2','bar3',close)", avg.name) self.assertEquals("Sum('bar0','bar1','bar2','bar3',close)", gssum.name) self.assertEquals("MakeVector('bar0','bar1','bar2','bar3',close)", basket.name) 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") basket2 = MakeVector([bar4, bar5, bar6, bar7], input_key='close') cross_basket_spread = Minus(basket2, basket)
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"])
def PipeLineMinus(x, y): return Minus(x, y, input_key='close')