trading_bot.py 984 B

1234567891011121314151617181920212223242526
  1. import random
  2. from datetime import timedelta, datetime
  3. from math import log2, ceil
  4. import model
  5. def place_order(ownable_id):
  6. cheapest_buy_order, best_sell_order = model.abs_spread(ownable_id)
  7. if cheapest_buy_order is None or best_sell_order is None:
  8. return
  9. investors_id = model.bank_id()
  10. orders = model.get_ownable_orders(investors_id, ownable_id)
  11. orders = [random.choice(orders) for _ in range(int(ceil(log2(len(orders)))))]
  12. amounts = [order[3] for order in orders]
  13. amount = sum(amounts) / len(amounts)
  14. expiry = datetime.strptime(model.current_db_time(), '%Y-%m-%d %H:%M:%S') + timedelta(minutes=43200)
  15. model.place_order(buy=bool(random.getrandbits(1)),
  16. ownership_id=model.get_ownership_id(ownable_id, investors_id),
  17. limit=random.uniform(cheapest_buy_order, best_sell_order),
  18. stop_loss=False,
  19. amount=amount,
  20. expiry=expiry)