Browse Source

Lots of fixes on trading bot

Eren Yilmaz 6 years ago
parent
commit
8156483736
1 changed files with 33 additions and 26 deletions
  1. 33 26
      trading_bot.py

+ 33 - 26
trading_bot.py

@@ -18,14 +18,20 @@ def place_order(ownable_id):
         return False
     investors_id = model.bank_id()
 
-    orders = model.get_ownable_orders(investors_id, ownable_id)
-    orders = [random.choice(orders) for _ in range(int(ceil(log2(len(orders)))))]
-    amounts = [order[3] for order in orders]
+    amounts = model.cursor.execute('''
+        SELECT ordered_amount 
+        FROM orders, ownership
+        WHERE orders.ownership_id = ownership.rowid
+        AND ownership.ownable_id = ?
+        ''', (ownable_id,)).fetchall()
+    if len(amounts) < 2:
+        raise AssertionError('We should have found at least two orders.')
+    amounts = [random.choice(amounts)[0] for _ in range(int(ceil(log2(len(amounts)))))]
     amount = ceil(sum(amounts) / len(amounts))
 
     expiry = datetime.strptime(model.current_db_time(), '%Y-%m-%d %H:%M:%S') + timedelta(minutes=DEFAULT_ORDER_EXPIRY)
 
-    limit = random.uniform(best_buy_order, cheapest_sell_order)
+    limit = round(random.uniform(best_buy_order, cheapest_sell_order) * 10000) / 10000
     if limit - best_buy_order < cheapest_sell_order - limit:
         model.place_order(buy=True,
                           ownership_id=model.get_ownership_id(ownable_id, investors_id),
@@ -43,23 +49,6 @@ def place_order(ownable_id):
     return True
 
 
-def main():  # TODO testen
-    """the initial part of the trading bot algorithm"""
-    if model.get_user_orders(model.bank_id()):
-        raise AssertionError('The trading bot already has some orders.')
-    if input('Are you sure you want to place the initial orders? (type in "yes" or something else):') == 'yes':
-        for ownable_id in model.ownable_ids():
-            if ownable_id != model.currency_id():
-                place_order(ownable_id)
-    else:
-        print('Not placing orders.')
-    model.cleanup()
-
-
-if __name__ == '__main__':
-    main()
-
-
 def notify_expired_orders(orders):
     for order in orders:
         # order_id = order[0]
@@ -104,26 +93,27 @@ def notify_order_traded(ownable_id):
         SELECT * FROM (
             SELECT NULL, ordered_amount, expiry_dt
             FROM order_history
+            WHERE ownership_id = ?
             ORDER BY rowid DESC -- equivalent to ordering by time created
         )
         LIMIT 1
-    ''', (ownership_id,))
+    ''', (ownership_id, ownership_id,))
     data = model.cursor.fetchall()
+
     if not data:
         return place_order(ownable_id)
-
     my_last_order = data[0]
-    last_order_open = my_last_order[0] is None
+    last_order_open = my_last_order[0] is not None
     last_order_id = my_last_order[0]
     last_amount = my_last_order[1]
     expiry = my_last_order[2]
     dt_order_placed = datetime.strptime(expiry, '%Y-%m-%d %H:%M:%S') - timedelta(minutes=DEFAULT_ORDER_EXPIRY)
 
     model.cursor.execute('''
-        SELECT 2 * SUM(amount) >= ?
+        SELECT SUM(amount) >= 2 * ?
         FROM transactions
         WHERE ownable_id = ? 
-          AND dt >= ?
+          AND dt > ? -- interestingly >= would be problematic
     ''', (last_amount, ownable_id, dt_order_placed))
 
     if model.cursor.fetchone()[0]:
@@ -132,3 +122,20 @@ def notify_order_traded(ownable_id):
         return place_order(ownable_id)
 
     return False
+
+
+def main():
+    """the initial part of the trading bot algorithm"""
+    if model.get_user_orders(model.bank_id()):
+        raise AssertionError('The trading bot already has some orders.')
+    if input('Are you sure you want to place the initial orders? (type in "yes" or something else):') == 'yes':
+        for ownable_id in model.ownable_ids():
+            if ownable_id != model.currency_id():
+                place_order(ownable_id)
+    else:
+        print('Not placing orders.')
+    model.cleanup()
+
+
+if __name__ == '__main__':
+    main()