-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtrading.js
More file actions
69 lines (58 loc) · 2.28 KB
/
trading.js
File metadata and controls
69 lines (58 loc) · 2.28 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// trading.js -- some examples of getting trading data using schwab-client-js
//
import { TradingApiClient } from "schwab-client-js";
const trdclient = new TradingApiClient();
let data = await trdclient.accountsNumbers();
// Assuming you have only one authorized Schwab account number,
// we'll grab the first hashed account number to use.
const acctHash = data[0].hashValue; // Grab the account hash for use in our calls
console.log("accountsNumbers DATA=", JSON.stringify(data));
console.log("\n\n");
data = await trdclient.transactByAcct(
acctHash,
"TRADE",
"2024-12-10T12:17:41-05:00",
"2025-01-03T12:17:41.000Z",
);
console.log("transactByAcct DATA=", JSON.stringify(data));
console.log("\n\n");
data = await trdclient.ordersByAccount(
acctHash,
"2024-12-10T12:17:41+02:00",
"2024-12-31T12:17:41.000Z",
"FILLED",
);
console.log("ordersByAccount DATA=", JSON.stringify(data));
console.log("\n\n");
data = await trdclient.orderAll(
"2024-11-10T12:17:41+02:00",
"2025-01-01T12:17:41.000Z",
"FILLED",
);
console.log("orderAll DATA=", JSON.stringify(data));
console.log("\n\n");
data = await trdclient.prefs();
console.log("prefs DATA=", JSON.stringify(data));
console.log("\n\n");
/* */
/* We use the helper functions to creating a JSON */
/* trading object. */
/* */
console.log("Creating trading object.....");
import { equityBuyLimit } from "schwab-client-js/orderhelp";
let tradeObj = equityBuyLimit("CDT", 1, "0.50");
console.log("Here is your trading object:");
console.log(JSON.stringify(tradeObj, null, 2));
console.log(
"Edit this file and uncomment the five lines below to make a trade using this trading object and then immediately check the status of the trade.....",
);
// const orderIdObj = await trdclient.placeOrderByAcct(acctHash, tradeObj);
// const orderId = orderIdObj.orderId;
// console.log("ORDERID=", orderId);
// const status = await trdclient.orderById(acctHash, orderId);
// console.log("ORDERSTATUS=", JSON.stringify(status));
// To delete the order:
// let data = await trdclient.orderDelete(acctHash, orderid);
// orderDelete returns null if successful
// You can also login to your account on schwab.com and manually delete orders
//