Beispiel #1
0
    def _start(self, app_context, **kwargs):
        self.length = self.get_stg_config_value("length", 10)

        self.bars = [
            self.app_context.inst_data_mgr.get_series("Bar.%s.Time.300" % i)
            for i in self.app_context.app_config.instrument_ids
        ]

        for bar in self.bars:
            bar.start(app_context)

        self.opens = MakeVector(self.bars, input_key='Open')
        self.volumes = MakeVector(self.bars, input_key="Volume")
        self.rank_opens = Rank(self.bars, input_key='open')
        self.rank_opens.start(app_context)

        self.rank_volumes = Rank(self.bars, input_key='Volume')
        self.rank_volumes.start(app_context)
        #
        self.pair_correlation = PairCorrelation(self.rank_opens,
                                                self.rank_volumes,
                                                length=self.length)
        self.pair_correlation.start(app_context)

        super(AlphaFormula3, self)._start(app_context, **kwargs)
Beispiel #2
0
class AlphaFormula3(Strategy):
    def __init__(self, stg_id=None, stg_configs=None):
        super(AlphaFormula3, self).__init__(stg_id=stg_id,
                                            stg_configs=stg_configs)
        self.day_count = 0
        self.order = None

    def _start(self, app_context, **kwargs):
        self.length = self.get_stg_config_value("length", 10)

        self.bars = [
            self.app_context.inst_data_mgr.get_series("Bar.%s.Time.300" % i)
            for i in self.app_context.app_config.instrument_ids
        ]

        for bar in self.bars:
            bar.start(app_context)

        self.opens = MakeVector(self.bars, input_key='Open')
        self.volumes = MakeVector(self.bars, input_key="Volume")
        self.rank_opens = Rank(self.bars, input_key='open')
        self.rank_opens.start(app_context)

        self.rank_volumes = Rank(self.bars, input_key='Volume')
        self.rank_volumes.start(app_context)
        #
        self.pair_correlation = PairCorrelation(self.rank_opens,
                                                self.rank_volumes,
                                                length=self.length)
        self.pair_correlation.start(app_context)

        super(AlphaFormula3, self)._start(app_context, **kwargs)

    def _stop(self):
        super(AlphaFormula3, self)._stop()

    def on_bar(self, bar):
        # rank = self.rank_opens.now('value')
        # logger.info("[%s] %s" % (self.__class__.__name__, rank))
        # if np.all(np.isnan(rank)):
        #     return
        corr = self.pair_correlation.now('value')
        if np.any(np.isnan(corr)):
            return

        weight = [corr[i, i + 2] for i in range(len(self.bars))]
        # weight = rank
        weight = -1 * weight[0]

        portfolio = self.get_portfolio()
        allocation = portfolio.total_equity * weight
        delta = allocation - portfolio.stock_value

        index = self.app_context.app_config.instrument_ids.index(bar.inst_id)
        qty = delta[index]
        # logger.info("%s,B,%.2f" % (bar.timestamp, bar.close))
        self.order = self.market_order(inst_id=bar.inst_id, action=OrdAction.BUY, qty=qty) if qty > 0 else \
            self.market_order(inst_id=bar.inst_id, action=OrdAction.SELL, qty=-qty)
Beispiel #3
0
    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)
class AlphaFormula3(Strategy):
    def __init__(self, stg_id=None, stg_configs=None):
        super(AlphaFormula3, self).__init__(stg_id=stg_id, stg_configs=stg_configs)
        self.day_count = 0
        self.order = None

    def _start(self, app_context, **kwargs):
        self.length = self.get_stg_config_value("length", 10)

        self.bars = [self.app_context.inst_data_mgr.get_series(
            "Bar.%s.Time.300" % i) for i in self.app_context.app_config.instrument_ids]

        for bar in self.bars:
            bar.start(app_context)

        self.opens = MakeVector(self.bars, input_key='Open')
        self.volumes = MakeVector(self.bars, input_key="Volume")
        self.rank_opens = Rank(self.bars, input_key='open')
        self.rank_opens.start(app_context)

        self.rank_volumes = Rank(self.bars, input_key='Volume')
        self.rank_volumes.start(app_context)
        #
        self.pair_correlation = PairCorrelation(self.rank_opens, self.rank_volumes, length=self.length)
        self.pair_correlation.start(app_context)

        super(AlphaFormula3, self)._start(app_context, **kwargs)

    def _stop(self):
        super(AlphaFormula3, self)._stop()

    def on_bar(self, bar):
        # rank = self.rank_opens.now('value')
        # logger.info("[%s] %s" % (self.__class__.__name__, rank))
        # if np.all(np.isnan(rank)):
        #     return
        corr = self.pair_correlation.now('value')
        if np.any(np.isnan(corr)):
            return


        weight = [corr[i, i+2] for i in range(len(self.bars))]
        # weight = rank
        weight = -1*weight[0]

        portfolio = self.get_portfolio()
        allocation = portfolio.total_equity * weight
        delta = allocation - portfolio.stock_value

        index = self.app_context.app_config.instrument_ids.index(bar.inst_id)
        qty = delta[index]
        # logger.info("%s,B,%.2f" % (bar.timestamp, bar.close))
        self.order = self.market_order(inst_id=bar.inst_id, action=OrdAction.BUY, qty=qty) if qty > 0 else \
            self.market_order(inst_id=bar.inst_id, action=OrdAction.SELL, qty=-qty)
    def test_empty_at_initialize(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)
        sma3 = SMA(close, input_key='close', length=3)
        sma20 = SMA(close, input_key='close', length=20)
        sma50 = SMA(close, input_key='close', length=50)

        sma3.start(self.app_context)
        sma20.start(self.app_context)

        rank = Rank([sma3, sma20, sma50], input_key='close')
        rank.start(self.app_context)
        self.assertEquals(0, len(rank.get_data()))
Beispiel #6
0
    def test_empty_at_initialize(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)
        sma3 = SMA(inputs=close, input_keys='close', length=3)
        sma20 = SMA(inputs=close, input_keys='close', length=20)
        sma50 = SMA(inputs=close, input_keys='close', length=50)

        sma3.start(self.app_context)
        sma20.start(self.app_context)

        rank = Rank(inputs=[sma3, sma20, sma50], input_keys='close')
        rank.start(self.app_context)
        self.assertEquals(0, len(rank.get_data()))
    def test_shape(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)

        sma3 = SMA(close, input_key='close', length=3)
        sma20 = SMA(close, input_key='close', length=20)
        sma50 = SMA(close, 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.assertAlmostEqual([1, 3], rank.shape(), 5)
    def test_shape(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)

        sma3 = SMA(close, input_key='close', length=3)
        sma20 = SMA(close, input_key='close', length=20)
        sma50 = SMA(close, 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.assertAlmostEqual([1, 3], rank.shape(), 5)
Beispiel #9
0
    def test_shape(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)

        sma3 = SMA(inputs=close, input_keys='close', length=3)
        sma20 = SMA(inputs=close, input_keys='close', length=20)
        sma50 = SMA(inputs=close, 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)
        try:
            np.testing.assert_almost_equal(np.array([1, 3]), rank.shape(), 5)
        except AssertionError as e:
            self.fail(e.message)
    def test_shape(self):
        close = self.app_context.inst_data_mgr.get_series("bar")
        close.start(self.app_context)

        sma3 = SMA(close, input_key="close", length=3)
        sma20 = SMA(close, input_key="close", length=20)
        sma50 = SMA(close, 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)
        try:
            np.testing.assert_almost_equal(np.array([1, 3]), rank.shape(), 5)
        except AssertionError as e:
            self.fail(e.message)
    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_with_multi_bar_multi_indicator_with_tail_start(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)

        # sma_2_bar0 = SMA(bar0, "close", 2)
        # sma_4_bar0 = SMA(bar0, "close", 4)
        # sma_3_bar1 = SMA(bar1, "close", 3)

        #
        # sma_2_bar0.start(self.app_context)
        # sma_4_bar0.start(self.app_context)
        # sma_3_bar1.start(self.app_context)

        # rank = Rank([sma_2_bar0, sma_3_bar1, sma_4_bar0], input_key=Indicator.VALUE)
        rank = Rank([
            SMA(bar0, "close", 2),
            SMA(bar1, "close", 3),
            SMA(bar0, "close", 4)
        ],
                    input_key=Indicator.VALUE)
        rank.start(self.app_context)  # only start at the tail is enough

        t = datetime.datetime.now()
        bar0.add({"timestamp": t, "close": 80.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})

        nan_arr = np.empty([1, 3])
        nan_arr[:] = np.nan
        # self.assertListEqual(nan_arr.tolist(), rank.now()["value"])
        self.__np_assert_almost_equal(nan_arr, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 85.0, "open": 0})
        bar1.add({"timestamp": t, "close": 93.0, "open": 0})
        target = nan_arr
        target[0, 0] = 0.
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 86.0, "open": 0})
        bar1.add({"timestamp": t, "close": 91.0, "open": 0})
        target[0, 1] = 0.5
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 90.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})
        target = np.array([[0.5, 1.0, 0.]])

        # self.assertAlmostEqual(target.tolist(), rank.now()["value"])
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))
    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 _start(self, app_context, **kwargs):
        self.length = self.get_stg_config_value("length", 10)

        self.bars = [self.app_context.inst_data_mgr.get_series(
            "Bar.%s.Time.300" % i) for i in self.app_context.app_config.instrument_ids]

        for bar in self.bars:
            bar.start(app_context)

        self.opens = MakeVector(self.bars, input_key='Open')
        self.volumes = MakeVector(self.bars, input_key="Volume")
        self.rank_opens = Rank(self.bars, input_key='open')
        self.rank_opens.start(app_context)

        self.rank_volumes = Rank(self.bars, input_key='Volume')
        self.rank_volumes.start(app_context)
        #
        self.pair_correlation = PairCorrelation(self.rank_opens, self.rank_volumes, length=self.length)
        self.pair_correlation.start(app_context)

        super(AlphaFormula3, self)._start(app_context, **kwargs)
Beispiel #15
0
    def test_with_multi_bar_multi_indicator(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)

        sma_2_bar0 = SMA(bar0, "close", 2)
        sma_4_bar0 = SMA(bar0, "close", 4)
        sma_3_bar1 = SMA(bar1, "close", 3)

        sma_2_bar0.start(self.app_context)
        sma_4_bar0.start(self.app_context)
        sma_3_bar1.start(self.app_context)

        rank = Rank([sma_2_bar0, sma_3_bar1, sma_4_bar0], input_key=Indicator.VALUE)
        rank.start(self.app_context)

        t = datetime.datetime.now()
        bar0.add({"timestamp": t, "close": 80.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})

        nan_arr = np.empty([1,3])
        nan_arr[:] = np.nan
        self.__np_assert_almost_equal(nan_arr, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 85.0, "open": 0})
        bar1.add({"timestamp": t, "close": 93.0, "open": 0})
        target = nan_arr
        target[0, 0] = 0.
        self.__np_assert_almost_equal(target, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 86.0, "open": 0})
        bar1.add({"timestamp": t, "close": 91.0, "open": 0})
        target[0, 1] = 0.5
        self.__np_assert_almost_equal(target, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 90.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})
        target = np.array([[0.5, 1.0, 0.]])

        self.__np_assert_almost_equal(target, rank.now()["value"])
    def test_with_multi_bar_multi_indicator_with_tail_start(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)

        # sma_2_bar0 = SMA(bar0, "close", 2)
        # sma_4_bar0 = SMA(bar0, "close", 4)
        # sma_3_bar1 = SMA(bar1, "close", 3)

        #
        # sma_2_bar0.start(self.app_context)
        # sma_4_bar0.start(self.app_context)
        # sma_3_bar1.start(self.app_context)

        # rank = Rank([sma_2_bar0, sma_3_bar1, sma_4_bar0], input_key=Indicator.VALUE)
        rank = Rank([SMA(bar0, "close", 2), SMA(bar1, "close", 3), SMA(bar0, "close", 4)], input_key=Indicator.VALUE)
        rank.start(self.app_context) # only start at the tail is enough

        t = datetime.datetime.now()
        bar0.add({"timestamp": t, "close": 80.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})

        nan_arr = np.empty([1,3])
        nan_arr[:] = np.nan
        # self.assertListEqual(nan_arr.tolist(), rank.now()["value"])
        self.__np_assert_almost_equal(nan_arr, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 85.0, "open": 0})
        bar1.add({"timestamp": t, "close": 93.0, "open": 0})
        target = nan_arr
        target[0, 0] = 0.
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 86.0, "open": 0})
        bar1.add({"timestamp": t, "close": 91.0, "open": 0})
        target[0, 1] = 0.5
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 90.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})
        target = np.array([[0.5, 1.0, 0.]])

        # self.assertAlmostEqual(target.tolist(), rank.now()["value"])
        self.__np_assert_almost_equal(target, np.array(rank.now()["value"]))
    def test_with_multi_bar_multi_indicator(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)

        sma_2_bar0 = SMA(bar0, "close", 2)
        sma_4_bar0 = SMA(bar0, "close", 4)
        sma_3_bar1 = SMA(bar1, "close", 3)

        sma_2_bar0.start(self.app_context)
        sma_4_bar0.start(self.app_context)
        sma_3_bar1.start(self.app_context)

        rank = Rank([sma_2_bar0, sma_3_bar1, sma_4_bar0], input_key=Indicator.VALUE)
        rank.start(self.app_context)

        t = datetime.datetime.now()
        bar0.add({"timestamp": t, "close": 80.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})

        nan_arr = np.empty([1, 3])
        nan_arr[:] = np.nan
        self.__np_assert_almost_equal(nan_arr, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 85.0, "open": 0})
        bar1.add({"timestamp": t, "close": 93.0, "open": 0})
        target = nan_arr
        target[0, 0] = 0.0
        self.__np_assert_almost_equal(target, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 86.0, "open": 0})
        bar1.add({"timestamp": t, "close": 91.0, "open": 0})
        target[0, 1] = 0.5
        self.__np_assert_almost_equal(target, rank.now()["value"])

        t = t + datetime.timedelta(0, 3)
        bar0.add({"timestamp": t, "close": 90.0, "open": 0})
        bar1.add({"timestamp": t, "close": 95.0, "open": 0})
        target = np.array([[0.5, 1.0, 0.0]])

        self.__np_assert_almost_equal(target, rank.now()["value"])
Beispiel #18
0
    def test_with_multi_bar_multi_indicator(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)

        sma_2_bar0 = SMA(inputs=bar0, input_keys="close", length=2)
        sma_4_bar0 = SMA(inputs=bar0, input_keys="close", length=4)
        sma_3_bar1 = SMA(inputs=bar1, input_keys="close", length=3)

        sma_2_bar0.start(self.app_context)
        sma_4_bar0.start(self.app_context)
        sma_3_bar1.start(self.app_context)

        rank = Rank(inputs=[sma_2_bar0, sma_3_bar1, sma_4_bar0],
                    input_keys=Indicator.VALUE)
        rank.start(self.app_context)

        t = 1
        bar0.add(data={"timestamp": t, "close": 80.0, "open": 0})
        bar1.add(data={"timestamp": t, "close": 95.0, "open": 0})
        print(rank.now(keys=PipeLine.VALUE))

        t = t + 3
        bar0.add(data={"timestamp": t, "close": 85.0, "open": 0})
        bar1.add(data={"timestamp": t, "close": 93.0, "open": 0})
        print(rank.now(keys=PipeLine.VALUE))

        t = t + 3
        bar0.add(data={"timestamp": t, "close": 86.0, "open": 0})
        bar1.add(data={"timestamp": t, "close": 91.0, "open": 0})
        print(rank.now(keys=PipeLine.VALUE))

        t = t + 3
        bar0.add(data={"timestamp": t, "close": 90.0, "open": 0})
        bar1.add(data={"timestamp": t, "close": 95.0, "open": 0})
        print(rank.now(keys=PipeLine.VALUE))
Beispiel #19
0
    def test_with_multiple_bar(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)

        barlist = [bar0, bar1, bar2, bar3]

        rank = Rank(inputs=barlist, input_keys='close')
        avg = Average(inputs=barlist, input_keys='close')
        gssum = GSSum(inputs=barlist, input_keys='close')
        absv = Abs(inputs=barlist, input_keys='close')
        tail = Tail(inputs=barlist,
                    input_keys='close',
                    lb=94,
                    ub=103,
                    newval=101)
        signvec = Sign(inputs=barlist, input_keys='close')
        decaylinear = DecayLinear(inputs=barlist, input_keys='close', length=3)
        scale = Scale(inputs=barlist, input_keys='close')

        rank.start(self.app_context)
        avg.start(self.app_context)
        gssum.start(self.app_context)
        absv.start(self.app_context)
        tail.start(self.app_context)
        signvec.start(self.app_context)
        decaylinear.start(self.app_context)
        scale.start(self.app_context)

        t1 = 1
        bar_t1_array = np.array([80, 95, 102, 105])
        bar0.add(data={"timestamp": t1, "close": bar_t1_array[0], "open": 0})
        bar1.add(data={"timestamp": t1, "close": bar_t1_array[1], "open": 0})
        bar2.add(data={"timestamp": t1, "close": bar_t1_array[2], "open": 0})
        bar3.add(data={"timestamp": t1, "close": bar_t1_array[3], "open": 0})

        # self.assertEquals([{"timestamp": t1,
        #                     "name": "'Rank('bar0','bar1','bar2','bar3',close)'",
        #                     "value": np.arange(4)/3.0}],
        #                   rank.get_data())

        rank_target = np.arange(4) / 3.0
        rank_target = rank_target.reshape((1, 4))
        avg_target = np.array([[95.5]])
        sum_target = np.array([[382.0]])
        abs_target = np.array([[80.0, 95.0, 102.0, 105.0]])
        tail_target = np.array([[80.0, 101.0, 101.0, 105.0]])
        signvec_target = np.array([[1.0, 1.0, 1.0, 1.0]])
        scale_target = bar_t1_array / np.sum(bar_t1_array)
        scale_target = scale_target.reshape(1, 4)

        self.__np_assert_almost_equal(abs_target, absv.now()["value"])
        self.__np_assert_almost_equal(rank_target,
                                      rank.get_data()[0]["value"], 5)
        self.__np_assert_almost_equal(avg_target,
                                      avg.get_data()[0]["value"], 5)
        self.__np_assert_almost_equal(sum_target,
                                      gssum.get_data()[0]["value"], 5)
        self.__np_assert_almost_equal(tail_target,
                                      tail.get_data()[0]["value"], 5)
        self.__np_assert_almost_equal(signvec_target,
                                      signvec.get_data()[0]["value"], 5)
        self.__np_assert_almost_equal(scale_target,
                                      scale.get_data()[0]["value"], 5)

        t2 = t1 + 3
        bar_t2_array = np.array([85, 98, 101.5, 103])
        bar0.add(data={"timestamp": t2, "close": bar_t2_array[0], "open": 0})
        bar1.add(data={"timestamp": t2, "close": bar_t2_array[1], "open": 0})
        bar2.add(data={"timestamp": t2, "close": bar_t2_array[2], "open": 0})
        bar3.add(data={"timestamp": t2, "close": bar_t2_array[3], "open": 0})

        t3 = t2 + 3
        bar_t3_array = np.array([87, 91, 107.0, 115])
        bar0.add(data={"timestamp": t3, "close": bar_t3_array[0], "open": 0})
        bar1.add(data={"timestamp": t3, "close": bar_t3_array[1], "open": 0})
        bar2.add(data={"timestamp": t3, "close": bar_t3_array[2], "open": 0})
        bar3.add(data={"timestamp": t3, "close": bar_t3_array[3], "open": 0})

        stack = np.vstack([bar_t1_array, bar_t2_array, bar_t3_array])
        decaylinear_target = np.dot(np.arange(3, 0, -1), stack) / np.sum(
            np.arange(3, 0, -1))
        scale_target = bar_t3_array / np.sum(bar_t3_array)
        scale_target = scale_target.reshape(1, 4)
        self.__np_assert_almost_equal(decaylinear_target,
                                      decaylinear.now(keys=PipeLine.VALUE))
        self.__np_assert_almost_equal(scale_target,
                                      scale.now(keys=PipeLine.VALUE))
    def test_with_multiple_bar(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)

        barlist = [bar0, bar1, bar2, bar3]

        rank = Rank(barlist, input_key='close')
        avg = Average(barlist, input_key='close')
        gssum = GSSum(barlist, input_key='close')
        absv = Abs(barlist, input_key='close')
        tail = Tail(barlist, lb=94, ub=103, newval=101, input_key='close')
        signvec = Sign(barlist, input_key='close')
        decaylinear = DecayLinear(barlist, length=3, input_key='close')
        scale = Scale(barlist, input_key='close')

        rank.start(self.app_context)
        avg.start(self.app_context)
        gssum.start(self.app_context)
        absv.start(self.app_context)
        tail.start(self.app_context)
        signvec.start(self.app_context)
        decaylinear.start(self.app_context)
        scale.start(self.app_context)

        t1 = datetime.datetime.now()
        bar_t1_array = np.array([80, 95, 102, 105])
        bar0.add({"timestamp": t1, "close": bar_t1_array[0], "open": 0})
        bar1.add({"timestamp": t1, "close": bar_t1_array[1], "open": 0})
        bar2.add({"timestamp": t1, "close": bar_t1_array[2], "open": 0})
        bar3.add({"timestamp": t1, "close": bar_t1_array[3], "open": 0})

        # self.assertEquals([{"timestamp": t1,
        #                     "name": "'Rank('bar0','bar1','bar2','bar3',close)'",
        #                     "value": np.arange(4)/3.0}],
        #                   rank.get_data())

        rank_target = np.arange(4)/3.0
        rank_target = rank_target.reshape((1,4))
        avg_target = 95.5 # for those return scala, it is a scala
        sum_target = 382.0
        abs_target = np.array([[80.0, 95.0, 102.0, 105.0]])
        tail_target = np.array([[80.0, 101.0, 101.0, 105.0]])
        signvec_target = np.array([[1.0, 1.0, 1.0, 1.0]])
        scale_target = bar_t1_array / np.sum(bar_t1_array)
        scale_target = scale_target.reshape(1, 4)

        # self.__np_assert_almost_equal(abs_target, absv.now()["value"])
        # self.__np_assert_almost_equal(rank_target, rank.get_data()[0]["value"], 5)
        # self.__np_assert_almost_equal(avg_target, avg.get_data()[0]["value"], 5)
        # self.__np_assert_almost_equal(sum_target, gssum.get_data()[0]["value"], 5)
        # self.__np_assert_almost_equal(tail_target, tail.get_data()[0]["value"], 5)
        # self.__np_assert_almost_equal(signvec_target, signvec.get_data()[0]["value"], 5)
        # self.__np_assert_almost_equal(scale_target, scale.get_data()[0]["value"], 5)

        self.assertAlmostEqual(abs_target.tolist(), absv.now()["value"])
        self.assertAlmostEqual(abs_target.tolist(), absv.get_data()[0]["value"], 5)
        self.assertAlmostEqual(rank_target.tolist(), rank.get_data()[0]["value"], 5)
        self.assertAlmostEqual(avg_target, avg.get_data()[0]["value"], 5)
        self.assertAlmostEqual(sum_target, gssum.get_data()[0]["value"], 5)
        self.assertAlmostEqual(tail_target.tolist(), tail.get_data()[0]["value"], 5)
        self.assertAlmostEqual(signvec_target.tolist(), signvec.get_data()[0]["value"], 5)
        self.assertAlmostEqual(scale_target.tolist(), scale.get_data()[0]["value"], 5)

        t2 = t1 + datetime.timedelta(0, 3)
        bar_t2_array = np.array([85, 98, 101.5, 103])
        bar0.add({"timestamp": t2, "close": bar_t2_array[0], "open": 0})
        bar1.add({"timestamp": t2, "close": bar_t2_array[1], "open": 0})
        bar2.add({"timestamp": t2, "close": bar_t2_array[2], "open": 0})
        bar3.add({"timestamp": t2, "close": bar_t2_array[3], "open": 0})

        t3 = t2 + datetime.timedelta(0, 3)
        bar_t3_array = np.array([87, 91, 107.0, 115])
        bar0.add({"timestamp": t3, "close": bar_t3_array[0], "open": 0})
        bar1.add({"timestamp": t3, "close": bar_t3_array[1], "open": 0})
        bar2.add({"timestamp": t3, "close": bar_t3_array[2], "open": 0})
        bar3.add({"timestamp": t3, "close": bar_t3_array[3], "open": 0})

        stack = np.vstack([bar_t1_array, bar_t2_array, bar_t3_array])
        decaylinear_target = np.dot(np.arange(3, 0, -1), stack)/np.sum(np.arange(3, 0, -1))
        scale_target = bar_t3_array / np.sum(bar_t3_array)
        scale_target = scale_target.reshape(1, 4)
        self.assertAlmostEqual(decaylinear_target.tolist(), decaylinear.now(keys=PipeLine.VALUE))
        self.assertAlmostEqual(scale_target.tolist(), scale.now(keys=PipeLine.VALUE))