Skip to content

Instantly share code, notes, and snippets.

@andrewkim316
Created August 23, 2019 22:16
Show Gist options
  • Save andrewkim316/0653d28330a5dcbdc97cb357eb6e6d06 to your computer and use it in GitHub Desktop.
Save andrewkim316/0653d28330a5dcbdc97cb357eb6e6d06 to your computer and use it in GitHub Desktop.
# Execute an order. Must contain 5, 6, or 7 arguments: type, symbol,
# quantity, side, time in force, limit price (optional), and stop price
# (optional).
@app.route("/order", methods=["POST"])
def order_handler():
args = request.form.get("text").split(" ")
if(len(args) == 0):
return WRONG_NUM_ARGS
async def sub_order(api, request, args):
if(args[0].lower() == "market"):
if(len(args) != 5):
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": WRONG_NUM_ARGS}), headers={"Content-type": "application/json"})
try:
args[3] = args[3].upper()
order = api.submit_order(
args[3],
args[2],
args[1],
args[0].lower(),
args[4])
price = api.get_barset(args[3], 'minute', 1)[args[3]][0].c
text = f'Market order of | {args[1]} {args[2]} {args[3]} {args[4]} |, current equity price at {price}. Order id = {order.id}.'
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:
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": f"ERROR: {str(e)}"}), headers={"Content-type": "application/json"})
elif(args[0].lower() == "limit"):
if(len(args) != 6):
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": WRONG_NUM_ARGS}), headers={"Content-type": "application/json"})
try:
args[3] = args[3].upper()
order = api.submit_order(
args[3],
args[2],
args[1],
args[0].lower(),
args[4],
limit_price=args[5])
text = f'Limit order of | {args[1]} {args[2]} {args[3]} {args[4]} at limit price {args[5]} | submitted. Order id = {order.id}.'
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:
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": f"ERROR: {str(e)}"}), headers={"Content-type": "application/json"})
elif(args[0].lower() == "stop"):
if(len(args) != 6):
return WRONG_NUM_ARGS
try:
args[3] = args[3].upper()
order = api.submit_order(
args[3],
args[2],
args[1],
args[0].lower(),
args[4],
stop_price=args[5])
text = f'Stop order of | {args[1]} {args[2]} {args[3]} {args[4]} at stop price {args[5]} | submitted. Order id = {order.id}.'
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:
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": f"ERROR: {str(e)}"}), headers={"Content-type": "application/json"})
elif(args[0].lower() == "stop_limit"):
if(len(args) != 7):
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": WRONG_NUM_ARGS}), headers={"Content-type": "application/json"})
try:
args[3] = args[3].upper()
order = api.submit_order(
args[3],
args[2],
args[1],
args[0].lower(),
args[4],
limit_price=args[5],
stop_price=args[6])
text = f'Stop-Limit order of | {args[1]} {args[2]} {args[3]} {args[4]} at stop price {args[6]} and limit price {args[5]} | submitted. Order id = {order.id}.'
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:
response = requests.post(url=request.form.get("response_url"), data=json.dumps(
{"text": f"ERROR: {str(e)}"}), headers={"Content-type": "application/json"})
else:
return BAD_ARGS
asyncio.run(sub_order(api, request, args))
return ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment