Example #1
0
    def test_is_same_q(self):
        q1 = QValue()
        q2 = QValue()
        self.assertTrue(q1.is_same_q_except_nop(q2))

        q1[1] = 1
        q2[1] = 1
        self.assertTrue(q1.is_same_q_except_nop(q2))

        q1[3] = 4
        q2[3] = 4
        self.assertTrue(q1.is_same_q_except_nop(q2))

        q1[ACTION.NOP] = 4
        q2[ACTION.NOP] = 1
        self.assertTrue(q1.is_same_q_except_nop(q2))

        q1 = QValue()
        q2 = QValue()
        q1[1] = 2
        q2[1] = 4
        self.assertFalse(q1.is_same_q_except_nop(q2))

        q1 = QValue()
        q2 = QValue()
        q1[2] = 2
        q2[2] = 4
        self.assertFalse(q1.is_same_q_except_nop(q2))

        q1 = QValue()
        q2 = QValue()
        q1[3] = 2
        q2[3] = 4
        self.assertFalse(q1.is_same_q_except_nop(q2))

        q1 = QValue()
        q2 = QValue()
        q1[4] = 2
        q2[4] = 4
        self.assertFalse(q1.is_same_q_except_nop(q2))
Example #2
0
    def update_q_on_nop(self):
        q_list = self.list_q(start_time=0, start_action=ACTION.NOP, asc=False)

        last_q = None

        for q_rec in q_list:
            q = QValue()
            q.set_q_records(q_rec)

            # todo check if redundunt q values is not produced.
            if last_q and not q.is_same_q_except_nop(last_q):
                t = last_q.time - q.time
                last_max_q = last_q.max_q() * Q_FIRST_DISCOUNT_RATE * (
                    Q_DISCOUNT_RATE**t)

                q[ACTION.NOP] = last_max_q
                self.insert_q(time=q.time,
                              start_time=0,
                              start_action=ACTION.NOP,
                              q_value=q)

                last_q = q

        self.commit()
Example #3
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()