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
class LongShort { | |
constructor(API_KEY, API_SECRET, PAPER){ | |
this.Alpaca = require('@alpacahq/alpaca-trade-api'); | |
this.alpaca = new this.Alpaca({ | |
keyId: API_KEY, | |
secretKey: API_SECRET, | |
paper: PAPER | |
}); | |
this.allStocks = ['DOMO', 'TLRY', 'SQ', 'MRO', 'AAPL', 'GM', 'SNAP', 'SHOP', 'SPLK', 'BA', 'AMZN', 'SUI', 'SUN', 'TSLA', 'CGC', 'SPWR', 'NIO', 'CAT', 'MSFT', 'PANW', 'OKTA', 'TWTR', 'TM', 'RTN', 'ATVI', 'GS', 'BAC', 'MS', 'TWLO', 'QCOM']; |
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(){ | |
// First, cancel any existing orders so they don't impact our buying power. | |
var orders; | |
await this.alpaca.getOrders({ | |
status: 'open', | |
direction: 'desc' | |
}).then((resp) => { | |
orders = resp; | |
}).catch((err) => {console.log(err.error);}); | |
var promOrders = []; |
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
// Rebalance our position after an update. | |
async rebalance(){ | |
await this.rerank(); | |
// Clear existing orders again. | |
var orders; | |
await this.alpaca.getOrders({ | |
status: 'open', | |
direction: 'desc' | |
}).then((resp) => { |
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
const API_KEY = 'YOUR_API_KEY_HERE'; | |
const API_SECRET = 'YOUR_API_SECRET_HERE'; | |
const PAPER = true; |
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
var longShortAmount = Math.floor(this.allStocks.length / 4); |
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); |
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
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
# 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
# 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"): |
OlderNewer