Skip to content

Instantly share code, notes, and snippets.

@andrewkim316
Last active August 28, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkim316/d410917c62a27be3b7d5c19024454e8c to your computer and use it in GitHub Desktop.
Save andrewkim316/d410917c62a27be3b7d5c19024454e8c to your computer and use it in GitHub Desktop.
# Lists certain things. Must contain 1 argument: orders, positions, or
# streams.
@app.route("/list", methods=["POST"])
def list_handler():
args = request.form.get("text").split(" ")
if(len(args) == 0):
return WRONG_NUM_ARGS
if(args[0] == "positions"):
try:
positions = api.list_positions()
if(len(positions) == 0):
return "No positions."
positions = map(
lambda x: (f'Symbol: {x.symbol}, Qty: {x.qty}, Side: {x.side}, Entry price: {x.avg_entry_price}, Current price: {x.current_price}'),
positions)
return "Listing positions...\n" + '\n'.join(positions)
except Exception as e:
reply_private(request, f"ERROR: {str(e)}")
elif(args[0] == "orders"):
try:
orders = api.list_orders(status="open")
if(len(orders) == 0):
return "No orders."
orders = map(
lambda x: (f'Symbol: {x.symbol}, Qty: {x.qty}, Side: {x.side}, Type: {x.type}, Time in Force: {x.time_in_force}, Amount Filled: {x.filled_qty}{(f", Stop Price = {x.stop_price}","")[x.stop_price == None]}{(f", Limit Price = {x.limit_price}","")[x.limit_price == None]}, Order id = {x.id}'),
orders)
return "Listing orders...\n" + '\n'.join(orders)
except Exception as e:
reply_private(request, f"ERROR: {str(e)}")
elif(args[0] == "streams"):
text = "Listing active streams...\n"
try:
for stream in streams:
if(streams[stream] is not None):
text += (stream + "\n")
if(text == "Listing active streams...\n"):
return "No active streams."
return text
except Exception as e:
reply_private(request, f"ERROR: {str(e)}")
else:
return BAD_ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment