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 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()