Skip to content

Instantly share code, notes, and snippets.

@andrewkim316
Last active August 28, 2019 16:59
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/86ef17192b983a398ff1c8b834afa508 to your computer and use it in GitHub Desktop.
Save andrewkim316/86ef17192b983a398ff1c8b834afa508 to your computer and use it in GitHub Desktop.
# Clears positions or orders. Must contain 1 argument: positions or orders
@app.route("/clear", methods=["POST"])
def clear_handler():
args = request.form.get("text").split(" ")
if(len(args) == 0):
return WRONG_NUM_ARGS
if(args[0] == "positions"):
async def sub_clear_positions(api, request):
try:
positions = api.list_positions()
positions = map(lambda x: [x.symbol, x.qty, x.side], positions)
for position in positions:
api.submit_order(position[0], abs(
int(position[1])), "sell" if position[2] == "long" else "buy", "market", "day")
text = "Position clearing orders sent."
response = requests.post(
url="https://slack.com/api/chat.postMessage",
data={
"token": SLACK_TOKEN,
"channel": request.form.get("channel_name"),
"text": text})
except Exception as e:
reply_private(request, f"ERROR: {str(e)}")
asyncio.run(sub_clear_positions(api, request))
return ""
elif(args[0] == "orders"):
async def sub_clear_orders(api, request):
try:
orders = api.list_orders()
orders = map(lambda x: x.id, orders)
for order in orders:
api.cancel_order(order)
text = "Order cancels sent."
response = requests.post(
url="https://slack.com/api/chat.postMessage",
data={
"token": SLACK_TOKEN,
"channel": request.form.get("channel_name"),
"text": text})
except Exception as e:
reply_private(request, f"ERROR: {str(e)}")
asyncio.run(sub_clear_orders(api, request))
return ""
else:
return BAD_ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment