cool_query.py 989 B

123456789101112131415161718192021
  1. import model
  2. model.connect()
  3. model.cursor.execute('''
  4. SELECT sell_order.stop_loss, buy_order.stop_loss
  5. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  6. WHERE buy_order.buy AND NOT sell_order.buy
  7. AND buyer.rowid = buy_order.ownership_id
  8. AND seller.rowid = sell_order.ownership_id
  9. AND buyer.ownable_id = ?
  10. AND seller.ownable_id = ?
  11. AND sell_order."limit" <= buy_order."limit"
  12. ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
  13. CASE WHEN buy_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
  14. buy_order."limit" DESC,
  15. sell_order."limit" ASC,
  16. buy_order.ordered_amount - buy_order.executed_amount DESC,
  17. sell_order.ordered_amount - sell_order.executed_amount DESC
  18. LIMIT 1
  19. ''', (3, 3,))
  20. print(model.cursor.fetchone())