-
Notifications
You must be signed in to change notification settings - Fork 61
/
createOrder.js
35 lines (30 loc) · 1.05 KB
/
createOrder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Sample script to show creating an order works
const axios = require('axios')
require('dotenv').config()
// returns string: client_order_id, string: orderStatus, int: responseStatus
async function placeOrder(symbol, qty, side) {
const response = await axios({
method: 'POST',
url: "https://paper-api.alpaca.markets/v2/orders",
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'APCA-API-KEY-ID': process.env.ALPACA_KEY,
'APCA-API-SECRET-KEY': process.env.ALPACA_SECRET
},
data: {
side: side,
type: "market",
time_in_force: "gtc",
symbol: symbol,
qty: qty
}
})
const responseStatus = response.status
console.log(`\nResponse status: ${responseStatus}\n`)
console.log(response.data)
console.log(`\n`)
const { client_order_id, status: orderStatus } = response.data
return { client_order_id, orderStatus, responseStatus }
}
placeOrder("AAPL", 1, "sell")