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
from flask import Flask, request | |
import alpaca_trade_api as tradeapi | |
import requests | |
import asyncio | |
import json | |
# Constants used throughout the script (names are self-explanatory). | |
# Must hard code SLACK TOKEN, KEY_ID, SECRET_KEY, channel | |
WRONG_NUM_ARGS = "ERROR: Incorrect amount of args. Action did not complete." | |
BAD_ARGS = "ERROR: Request error. Action did not complete." |
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
# Cancels most recent order. Takes no arguments. | |
@app.route("/cancel_recent_order", methods=["POST"]) | |
def cancel_recent_order_handler(): | |
args = request.form.get("text").split(" ") | |
if(len(args) != 0 and not (len(args) == 1 and args[0].strip() == "")): | |
return WRONG_NUM_ARGS | |
try: | |
orders = api.list_orders(status="open", limit=1) |
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
# Gets Polygon price specified stock symbols. Must include one or more | |
# arguments representing stock symbols. Must have live account to access. | |
@app.route("/get_price_polygon", methods=["POST"]) | |
def get_price_polygon_handler(): | |
args = request.form.get("text").split(" ") | |
if(len(args) == 1 and args[0].strip() == ""): | |
return WRONG_NUM_ARGS |
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
# Gets basic account info. Takes no arguments. | |
@app.route("/account_info", methods=["POST"]) | |
def account_info_handler(): | |
args = request.form.get("text").split(" ") | |
if(len(args) != 0 and not (len(args) == 1 and args[0].strip() == "")): | |
return WRONG_NUM_ARGS | |
try: | |
account = api.get_account() |
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
# 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): |
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"): |
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
# 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 |
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
async run(){ | |
... | |
else { | |
// Rebalance the portfolio. | |
await this.rebalance(); | |
} | |
}, 60000); | |
} |
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
// Get percent changes of the stock prices over the past 10 minutes. | |
getPercentChanges(allStocks){ | |
var length = 10; | |
var promStocks = []; | |
allStocks.forEach((stock) => { | |
promStocks.push(new Promise(async (resolve, reject) => { | |
await this.alpaca.getBars('minute', stock.name, {limit: length}).then((resp) => { | |
stock.pc = (resp[stock.name][length - 1].c - resp[stock.name][0].o) / resp[stock.name][0].o; | |
}).catch((err) => {console.log(err.error);}); | |
resolve(); |
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
// Determine amount to long/short based on total stock price of each bucket. | |
var equity; | |
await this.alpaca.getAccount().then((resp) => { | |
equity = resp.equity; | |
}).catch((err) => {console.log(err.error);}); | |
this.shortAmount = 0.30 * equity; | |
this.longAmount = Number(this.shortAmount) + Number(equity); |