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)
def select_q_val(self, time, start_time, start_action): rec = self.select_q(time, start_time, start_action) q = None if rec: q = QValue() q.set_q_records(rec) # print('selectq', time, start_time, start_action, q) return q
def test_q_set_rec(self): r = (100, 101, 1, 2, 3, 4, 5, 6) q = QValue() q.set_q_records(r) self.assertEqual(q.time, 100) self.assertEqual(q.start_time, 101) self.assertEqual(q.start_action, 1) self.assertEqual(q[ACTION.NOP], 2) self.assertEqual(q[ACTION.BUY], 3) self.assertEqual(q[ACTION.BUY_NOW], 4) self.assertEqual(q[ACTION.SELL], 5) self.assertEqual(q[ACTION.SELL_NOW], 6)
def test_market_prices(self): r = (100, 101, 99, 100, 101, 102, 103) q = QValue() q.set_price_record(r) price = q.order_prices self.assertEqual(price.time, 100) self.assertEqual(price.market_order_sell, 101) self.assertEqual(price.market_order_buy, 99) self.assertEqual(price.fix_order_sell, 100) self.assertEqual(price.fix_order_sell_time, 101) self.assertEqual(price.fix_order_buy, 102) self.assertEqual(price.fix_order_buy_time, 103)
def __init__(self): super().__init__() # init members self.episode_done = None self.db_path = '/bitlog/bitlog.db' self.generator = Generator() self.board_generator = None self.board = None self.margin = 0 self.q_value = QValue() self.episode_text = '' self.start_action = ACTION.NOP self.start_time = None self.sell_order_price = None self.buy_order_price = None self.episode_start_time = 0 # init gym environment self.action_space = gym.spaces.Discrete( 5) # 5 actions nop, buy, BUY, sell, SELL self.observation_space = gym.spaces.Box(low=0, high=255, shape=(TIME_WIDTH, BOARD_WIDTH, 6), dtype=np.uint8) # self.reward_range = [-255., 255.] self.reset()
def select_q_values(self, time, action): ''' :param time time to select :param action :return: q values for NOP, buy, sell, BUY, SELL ''' select_q_sql = """select time, start_time, start_action, nop_q, buy_q, buy_now_q, sell_q, sell_now_q from q where start_time = ? and start_action= ? order by start_time, time """ self.cursor.execute(select_q_sql, ( time, action, )) rec = self.cursor.fetchone() q = QValue() q.set_q_records(rec) return q
def test_accessor(self): q = QValue() q[ACTION.NOP] = 1 self.assertEqual(q[ACTION.NOP], 1) q[ACTION.BUY] = 2 self.assertEqual(q[ACTION.BUY], 2) q[ACTION.SELL] = 3 self.assertEqual(q[ACTION.SELL], 3)
def insert_updated_q(self): ''' update q values according to the list of prices :return: None ''' old_price = None for line in self.list_price(): price = OrderPrices() price.set_price_record(line) q_val = QValue() if not price.is_equal_prices(old_price): print(price.time) # update if price.market_order_sell: q = self.create_q_sequence( start_time=price.time, action=ACTION.SELL_NOW, start_price=price.market_order_sell, skip_time=60) q_val[ACTION.SELL_NOW] = q if price.market_order_buy: q = self.create_q_sequence( start_time=price.time, action=ACTION.BUY_NOW, start_price=price.market_order_buy, skip_time=60) q_val[ACTION.BUY_NOW] = q if price.fix_order_sell: q = self.create_q_sequence( start_time=price.time, action=ACTION.SELL, start_price=price.fix_order_sell, skip_time=60) q_val[ACTION.SELL] = q if price.fix_order_buy: q = self.create_q_sequence(start_time=price.time, action=ACTION.BUY, start_price=price.fix_order_buy, skip_time=60) q_val[ACTION.BUY] = q self.insert_q(time=price.time, start_action=ACTION.NOP, start_time=0, q_value=q_val) old_price = price
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()
def test_crate_db_insert_q(self): db = LogDb() # create on memory db.connect() db.create_cursor() db.create() db.commit() q = QValue() q.q[ACTION.NOP] = 1 q.q[ACTION.BUY] = 1 q.q[ACTION.SELL] = 1 q.q[ACTION.SELL_NOW] = 1 q.q[ACTION.BUY_NOW] = 1 db.insert_q(100, 101, ACTION.BUY, q)
def test_select_db_q(self): db = LogDb() # create on memory db.connect() db.create_cursor() db.create() db.commit() q = QValue() q.q[ACTION.NOP] = 1 q.q[ACTION.BUY] = 2 q.q[ACTION.SELL] = 3 q.q[ACTION.SELL_NOW] = 4 q.q[ACTION.BUY_NOW] = 5 db.insert_q(100, 101, ACTION.BUY, q) r = db.select_q(100, 101, ACTION.BUY) print(r) def test_list_db_q(self): db = LogDb() # create on memory db.connect() db.create_cursor() db.create() db.commit() q = QValue() q.q[ACTION.NOP] = 1 q.q[ACTION.BUY] = 2 q.q[ACTION.SELL] = 3 q.q[ACTION.SELL_NOW] = 4 q.q[ACTION.BUY_NOW] = 5 db.insert_q(100, 0, ACTION.BUY, q) db.insert_q(101, 100, ACTION.BUY, q) db.insert_q(102, 100, ACTION.BUY, q) db.insert_q(103, 100, ACTION.BUY, q) db.insert_q(101, 101, ACTION.BUY, q) db.commit() r = db.select_q(102, 100, ACTION.BUY) print(r) r = db.list_q(100, ACTION.BUY) print(r) for q in r: print(q)
def test_list_update_q(self): db = LogDb('/bitlog/bitlog.db') # create on memory db.connect() db.create_cursor() db.create() db.commit() q = QValue() q.q[ACTION.NOP] = 1 q.q[ACTION.BUY] = 2 q.q[ACTION.SELL] = 3 q.q[ACTION.SELL_NOW] = 4 q.q[ACTION.BUY_NOW] = 5 db.insert_q(100, 0, ACTION.BUY, q) db.insert_q(101, 100, ACTION.BUY, q) db.insert_q(102, 100, ACTION.BUY, q) db.insert_q(103, 100, ACTION.BUY, q) db.insert_q(101, 101, ACTION.BUY, q) db.commit() db.insert_updated_q()
def __init__(self, time_width=TIME_WIDTH): self.time_width = time_width self.current_time = 0 self.center_price = 0 self.sell_trade = SparseMatrix(time_width) self.buy_trade = SparseMatrix(time_width) self.sell_order = SparseMatrix(time_width) self.buy_order = SparseMatrix(time_width) self.buy_book_price = 0 self.buy_book_vol = 0 self.sell_book_price = 0 self.sell_book_vol = 0 self.sell_trade_price = 0 self.sell_trade_volume = 0 self.buy_trade_price = 0 self.buy_trade_volume = 0 self.my_sell_order = {} self.my_buy_order = {} # sell simulation result self.market_sell_price = None self.market_buy_price = None self.fix_sell_price = None self.fix_sell_price_time = None self.fix_buy_price = None self.fix_buy_price_time = None self.funding_ttl = 0 self.funding = 0 self.q_value = QValue()
def test_calc_q_values(self): r = (100, 101, 99, 100, 101, 102, 103) q = QValue() q.set_price_record(r) q.update_q(sell_price=1) self.assertEqual(q[ACTION.SELL_NOW], -10) self.assertEqual(q[ACTION.SELL], -10) self.assertNotEqual(q[ACTION.BUY], -10) self.assertNotEqual(q[ACTION.BUY_NOW], -10) price = q.order_prices self.assertEqual(price.time, 100) self.assertEqual(price.market_order_sell, 101) self.assertEqual(price.market_order_buy, 99) self.assertEqual(price.fix_order_sell, 100) self.assertEqual(price.fix_order_sell_time, 101) self.assertEqual(price.fix_order_buy, 102) self.assertEqual(price.fix_order_buy_time, 103)
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()
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))