Ejemplo n.º 1
0
    def test_best_action(self):
        q = QValue()
        self.assertEqual(q.get_best_action(), 0)

        q[ACTION.BUY] = 1
        self.assertEqual(q.get_best_action(), ACTION.BUY)

        print(q)
Ejemplo n.º 2
0
    def create_q_sequence(self,
                          *,
                          start_time,
                          action,
                          start_price,
                          skip_time=1):
        if (action == ACTION.BUY) or (action == ACTION.BUY_NOW):
            peak_price = self.select_highest_price_time(start_time +
                                                        EXECUTE_TIME_MIN)
        elif (action == ACTION.SELL) or (action == ACTION.SELL_NOW):
            peak_price = self.select_lowest_price_time(start_time +
                                                       EXECUTE_TIME_MIN)

        prices = self.list_price(start_time=start_time + skip_time,
                                 end_time=peak_price.time,
                                 desc=True)

        nop_q = 0
        old_q = QValue(start_time=start_time,
                       start_action=action,
                       start_price=start_price)

        skip_count = 1

        # print('creawteq_seq', start_time, action)

        for price_rec in prices:
            q = QValue(start_time=start_time,
                       start_action=action,
                       start_price=start_price)
            q.set_price_record(price_rec)
            nop_q *= Q_DISCOUNT_RATE

            if q.is_same_q_except_nop(old_q):
                skip_count += 1
                continue

            best_action = old_q.get_best_action()

            # todo Ajudst Q values for negative values
            if best_action == ACTION.BUY or action == ACTION.SELL:
                new_q = old_q.max_q() * Q_FIRST_DISCOUNT_RATE * (
                    Q_DISCOUNT_RATE**skip_count)
            else:
                new_q = old_q.max_q() * (Q_DISCOUNT_RATE**skip_count)

            if nop_q < new_q:
                nop_q = new_q
            q[ACTION.NOP] = nop_q

            #if q.time != start_time:
            self.insert_q(time=q.time,
                          start_time=start_time,
                          start_action=action,
                          q_value=q)

            old_q = q
            skip_count = 1

        self.commit()

        return old_q.max_q()