Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View andrewkim316's full-sized avatar
🙃
Proud user of Github Desktop

Andrew Kim andrewkim316

🙃
Proud user of Github Desktop
View GitHub Profile
const StopLimitOrderIntentHandler = {
// Triggers when user wants to execute a stop limit order
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'StopLimitOrderIntent';
},
async handle(handlerInput) {
// Get user inputs and declare the Alpaca object
const slots = handlerInput.requestEnvelope.request.intent.slots;
const api = new Alpaca({
const Alexa = require('ask-sdk-core');
const Alpaca = require('@alpacahq/alpaca-trade-api');
const keyId = "KEY_ID_HERE";
const secretKey = "SECRET_KEY_HERE";
asyncio.run(sub_order(api, request, args))
return ""
# Run on local port 3000
if __name__ == "__main__":
app.run(port=3000)
async def sub_order(api, request, args):
order_type = args[0].lower()
if order_type == "market":
if len(args) < 5:
reply_private(request, WRONG_NUM_ARGS)
return
try:
side = args[1]
qty = args[2]
symbol = args[3].upper()
@app.route("/order", methods=["POST"])
def order_handler():
'''Execute an order. Must contain 5 arguments: type, symbol,
quantity, side, time in force
'''
args = request.form.get("text").split(" ")
if len(args) == 0:
return WRONG_NUM_ARGS
from flask import Flask, request
import alpaca_trade_api as tradeapi
import requests
import asyncio
import json
import os
# Constants used throughout the script (names are self-explanatory)
WRONG_NUM_ARGS = "ERROR: Incorrect amount of args. Action did not complete."
const MarketOrderIntentHandler = {
// Triggers when user invokes a market order
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'MarketOrderIntent';
},
async handle(handlerInput) {
// Get user inputs and declare the Alpaca object
const slots = handlerInput.requestEnvelope.request.intent.slots;
const api = new Alpaca({
# Stream listeners
@conn.on(r'trade_updates')
async def trade_updates_handler(conn, chan, data):
if data.event == "new":
return ""
elif data.event == "fill" or data.event == "partial_fill":
text = f'*Event*: {data.event}, {data.order["type"]} order of | {data.order["side"]} {data.order["qty"]} {data.order["symbol"]} {data.order["time_in_force"]} | {data.event} at {data.price}'
else:
# Unsubsribe to streaming channel(s). Must contain one or more arguments
# representing streams you want to disconnect to.
@app.route("/unsubscribe_streaming", methods=["POST"])
def unsubscribe_handler():
args = request.form.get("text").split(" ")
if len(args) == 1 and args[0].strip() == "":
return BAD_ARGS
try:
# Subscribe to streaming channel(s). Must contain one or more arguments
# representing streams you want to connect to.
@app.route("/subscribe_streaming", methods=["POST"])
def stream_data_handler():
args = request.form.get("text").split(" ")
if len(args) == 1 and args[0].strip() == "":
return BAD_ARGS
try: