Jelajahi Sumber

Improve ordering

Eren Yilmaz 5 tahun lalu
induk
melakukan
5b606c2b9e
2 mengubah file dengan 48 tambahan dan 52 penghapusan
  1. 37 52
      client_controller.py
  2. 11 0
      server_controller.py

+ 37 - 52
client_controller.py

@@ -196,21 +196,35 @@ def activate_key(key=''):
         print('Key activation failed with message:', response['error_message'])
 
 
-def buy(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
+def _order(is_buy_order, obj_name, amount, limit, stop_loss, expiry):
+    if stop_loss not in [None, '1', '0']:
+        print('Invalid value for flag stop loss (only 0 or 1 allowed).')
+        return
     if obj_name is None:  # TODO list some available objects
-        obj_name = input('Name of object to buy: ')
+        obj_name = input('Name of object to sell: ')
     if amount is None:
         amount = input('Amount: ')
-    if limit == '':
+
+    if limit is None:
         set_limit = yn_dialog('Do you want to place a limit?')
         if set_limit:
             limit = input('Limit: ')
             stop_loss = yn_dialog('Is this a stop-loss limit?')
-        else:
-            limit = None
-            stop_loss = None
-    if limit != '' and stop_loss == '':
-        stop_loss = yn_dialog('Is this a stop-loss limit?')
+
+    if limit is not None:
+        try:
+            limit = float(limit)
+        except ValueError:
+            print('Invalid limit.')
+            return
+
+        if stop_loss is None:
+            stop_loss = yn_dialog('Is this a stop-loss limit?')
+
+        question = 'Are you sure you want to use such a low limit (limit=' + str(limit) + ')?:'
+        if not is_buy_order and limit <= 0 and not yn_dialog(question):
+            print('Order was not placed.')
+            return
 
     if expiry is None:
         expiry = input('Time until order expires (minutes, default ' + str(DEFAULT_ORDER_EXPIRY) + '):')
@@ -223,7 +237,7 @@ def buy(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
         return
 
     fake_loading_bar('Sending Data', duration=1.3)
-    response = client_request('order', {"buy": True,
+    response = client_request('order', {"buy": is_buy_order,
                                         "session_id": connection.session_id,
                                         "amount": amount,
                                         "ownable": obj_name,
@@ -237,51 +251,22 @@ def buy(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
               'to see if the order has been executed already.')
 
 
-def sell(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
-    if obj_name is None:  # TODO list some available objects
-        obj_name = input('Name of object to sell: ')
-    if amount is None:
-        amount = input('Amount: ')
-    if limit == '':
-        set_limit = yn_dialog('Do you want to place a limit?')
-        if set_limit:
-            limit = input('Limit: ')
-            stop_loss = yn_dialog('Is this a stop-loss limit?')
-        else:
-            limit = None
-            stop_loss = None
-    if limit != '' and stop_loss == '':
-        stop_loss = yn_dialog('Is this a stop-loss limit?')
-
-    question = 'Are you sure you want to use such a low limit (limit=' + str(limit) + \
-               ')? (type in "yes" or something else):'
-    if limit is not None and float(limit) <= 0 and input(question) != 'yes':
-        print('Order was not placed.')
-        return
+def sell(obj_name=None, amount=None, limit=None, stop_loss=None, expiry=None):
+    _order(is_buy_order=False,
+           obj_name=obj_name,
+           amount=amount,
+           limit=limit,
+           stop_loss=stop_loss,
+           expiry=expiry)
 
-    if expiry is None:
-        expiry = input('Time until order expires (minutes, default ' + str(DEFAULT_ORDER_EXPIRY) + '):')
-        if expiry == '':
-            expiry = DEFAULT_ORDER_EXPIRY
-    try:
-        expiry = float(expiry)
-    except ValueError:
-        print('Invalid expiration time.')
-        return
 
-    fake_loading_bar('Sending Data', duration=1.3)
-    response = client_request('order', {"buy": False,
-                                        "session_id": connection.session_id,
-                                        "amount": amount,
-                                        "ownable": obj_name,
-                                        "limit": limit,
-                                        "stop_loss": stop_loss,
-                                        "time_until_expiration": expiry})
-    if 'error_message' in response:
-        print('Order placement failed with message:', response['error_message'])
-    else:
-        print('You might want to use the `trades` or `depot` commands',
-              'to see if the order has been executed already.')
+def buy(obj_name=None, amount=None, limit=None, stop_loss=None, expiry=None):
+    _order(is_buy_order=True,
+           obj_name=obj_name,
+           amount=amount,
+           limit=limit,
+           stop_loss=stop_loss,
+           expiry=expiry)
 
 
 def orders():

+ 11 - 0
server_controller.py

@@ -126,6 +126,9 @@ def order():
     except KeyError:  # for example when limit was not specified
         limit = None
 
+    if limit < 0:
+        return bad_request('Limit must not be negative.')
+
     try:
         if request.json['stop_loss'] == '':
             stop_loss = None
@@ -283,3 +286,11 @@ def bad_request(msg=''):
         msg = str(response.status) + ': ' + msg
     response.content_type = 'application/json'
     return json.dumps({"error_message": msg})
+
+
+def internal_server_error(msg=''):
+    response.status = 500
+    if debug:
+        msg = str(response.status) + ': ' + msg
+    response.content_type = 'application/json'
+    return json.dumps({"error_message": msg})