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