Last active
August 28, 2019 16:57
-
-
Save andrewkim316/d410917c62a27be3b7d5c19024454e8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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