コード例 #1
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)
コード例 #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)
コード例 #3
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))
コード例 #4
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))
コード例 #5
0
    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))
コード例 #6
0
    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))