-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschwab.gs
285 lines (229 loc) · 10.4 KB
/
schwab.gs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var userProperties = PropertiesService.getUserProperties();
var scriptProperties = PropertiesService.getScriptProperties();
var schwab_apikey = scriptProperties.getProperty('schwab_apikey')
var schwab_secret = scriptProperties.getProperty('schwab_secret')
var encodedCredentials = Utilities.base64Encode(schwab_apikey + ":" + schwab_secret);
// On Spreadsheet open, add a menu for Schwab API
function onOpen(e) {
var refresh_time_expiry = userProperties.getProperty("refresh_time_expiry");
var mynow = new Date();
var ui = SpreadsheetApp.getUi();
ui.createMenu('Schwab API')
.addItem('Authenticate', 'schwab_ShowPane')
.addSeparator()
.addItem("Authentication expires: " + refresh_time_expiry, 'null')
.addToUi();
if ( Date.parse(mynow) > Date.parse(refresh_time_expiry) ) {
Logger.log("Schwab Authentication has expired, user needs to reauth.")
}
}
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('schwab_SidePane')
.setWidth(400)
.setHeight(400);
SpreadsheetApp.getUi().showModalDialog(html, 'Dialog Title');
}
//Open a SidePane asynchronously. The html will return by calling the function schwab_backfromPane
function schwab_ShowPane() {
linkURL = "https://api.schwabapi.com/v1/oauth/authorize?client_id="+ schwab_apikey +"&redirect_uri=https%3A%2F%2F127.0.0.1";
var html = HtmlService.createTemplateFromFile('schwab_SidePane')
.evaluate();
SpreadsheetApp.getUi().showSidebar(html);
}
// Call this function to open the sidebar
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('schwab_SidePane.html')
.setTitle('Schwab API Auth')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function schwab_backfromPane(d) {
// Called after user clicks Step 2 button on SidePane, return here with dictionary d
schwab_GetTokens(d.returnURI);
}
//******************************MAIN FUNCTIONS***************************************************
/**
* Call Ameritrade API to get the price of stockSymbol.
*
* @param {"GOOG"} stockSymbol the stock's ticker symbol
* @param {"lastPrice"} priceType the price field to use. Valid examples are [ "lastPrice" | "openPrice" | "highPrice" | "lowPrice" | "closePrice" | "regularMarketLastPrice" ]
* @customfunction
*/
function schwab_GetQuote(stockSymbol,priceType) {
// Check if stockSymbol is empty
if (stockSymbol === "" || stockSymbol === null || typeof stockSymbol === "undefined") {
return "Stock symbol is empty";
}
// Check if priceType is empty
if (priceType === "" || priceType === null || typeof priceType === "undefined") {
return "Price type is empty";
}
var authorization = schwab_GetBearerString();
var options = {
"method" : "GET",
"headers" : {"Authorization" : authorization},
};
var myurl="https://api.schwabapi.com/marketdata/v1/quotes?symbols=" + stockSymbol;
var result=UrlFetchApp.fetch(myurl, options);
var contents = result.getContentText();
var json = JSON.parse(contents);
var price = json[stockSymbol]["quote"][priceType];
Logger.log(stockSymbol + ": " + price);
return price;
}
/**
* Returns a balance value of your Schwab account.
*
* @param {"equity"} balance the balance to obtain. Valid examples are [ "cashBalance" | "liquidationValue" | "longMarketValue" | "availableFunds" | "buyingPower" | "equity" | "longMarginValue" | "maintenanceRequirement" | "marginBalance" ]. Additional examples can be found in the API documentation.
* @customfunction
*/
function schwab_Balance(balance) {
var authorization = schwab_GetBearerString();
var options = {
"method" : "GET",
"headers" : {"Authorization" : authorization},
};
var myUrl =
"https://api.schwabapi.com/trader/v1/accounts";
var result = UrlFetchApp.fetch(myUrl, options);
var contents = result.getContentText();
var json = JSON.parse(contents);
var value = json[0]["securitiesAccount"]["currentBalances"][balance];
Logger.log(value)
return value;
}
/**
* Returns the positions in your Schwab portfolio with the following fields in an array: [ Stock Symbol | Quantity | Average Price | Market Value | Current Day P/L | Current Day P/L % ]
*
* @customfunction
*/
function schwab_Positions() {
var authorization = schwab_GetBearerString();
var options = {
"method" : "GET",
"headers" : {"Authorization" : authorization},
};
var extraOptions = "?fields=positions";
var myUrl =
"https://api.schwabapi.com/trader/v1/accounts/" + extraOptions;
var result = UrlFetchApp.fetch(myUrl, options);
//Parse JSON
var contents = result.getContentText();
var json = JSON.parse(contents);
var positions = json[0]["securitiesAccount"]["positions"];
var attributes = [
"instrument", // The stock symbol is inside this returned "instrument" object.
"longQuantity",
"averagePrice",
"marketValue",
"currentDayProfitLoss",
"currentDayProfitLossPercentage"
];
var array = [];
var item = [];
for (var stocki = 0; stocki < positions.length; stocki++) {
// Iterate over all returned positions
for (var attributei = 0; attributei < attributes.length; attributei++) {
// Iterate over the wanted attributes to find the corresponding value in the returned positions
if (attributes[attributei] === "instrument") {
item.push(positions[stocki][attributes[attributei]]["symbol"]);
} else {
item.push(positions[stocki][attributes[attributei]]);
}
}
array.push(item);
item = [];
}
array.sort(function(b, a) {
// Sorted by currentDayProfitLossPercentage
return a[5] - b[5];
});
Logger.log(array)
return array;
}
//*****************************AUTHENTICATION FUNCTIONS****************************************************************
function schwab_GetBearerString(){
// Call schwab get access token using the refresh token - check validity of both access and refresh tokens.
// Access token lasts for 30 minutes, refresh token lasts for 7 days before having to require user to authenticate again
// curl -X POST --header "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&refresh_token=<refresh_token>&redirect_uri=https%3A%2F%2F127.0.0.1" "https://api.schwabapi.com/v1/oauth2/token"
var refresh_token = userProperties.getProperty("refresh_token");
var refresh_time_expiry = userProperties.getProperty("refresh_time_expiry");
var access_token = userProperties.getProperty("access_token");
var access_time_expiry = userProperties.getProperty("access_time_expiry");
var mynow = new Date();
// if ( (Date.parse(mynow) - Date.parse(access_time)) <29*60*1000 ) { //Access token is still not expired
// Logger.log(1800 - (Date.parse(mynow) - Date.parse(access_time))/1000 + " seconds until access token expires, using existing token.")
// return "Bearer " + access_token;
// } else if ( (Date.parse(mynow) - Date.parse(refresh_time)) >7*24*60*60*1000 ) { //Refresh token expired
// //re-authenticate - schwab_showPane() ?
// Logger.log("Refresh Token has expired. Reauthentication is probably needed, but trying to proceed with renewing the Access Token")
// // return "Re-authentication needed!";
// }
if ( Date.parse(mynow) < Date.parse(access_time_expiry) ) {
// Logger.log(( (Date.parse(access_time_expiry) - Date.parse(mynow)) / 1000) + " seconds until access token expires, using existing access token")
return "Bearer " + access_token;
}
Logger.log("Access Token expired " + ((Date.parse(access_time_expiry) - Date.parse(mynow))/1000) + " seconds ago. Generating a new one.")
// refresh access_token with refresh token
// curl -X POST \https://api.schwabapi.com/v1/oauth/token \-H 'Authorization: Basic {BASE64_ENCODED_Client_ID:Client_Secret} \-H 'Content-Type: application/x-www-form-urlencoded' \-d 'grant_type=refresh_token&refresh_token={REFRESH_TOKEN_GENERATED_FROM_PRIOR_STEP}
var formData = {
"grant_type" : "refresh_token",
"refresh_token" : refresh_token,
}
var options = {
"method" : "post",
"headers": {
"Authorization": "Basic " + encodedCredentials
},
"payload" : formData
}
var myurl="https://api.schwabapi.com/v1/oauth/token";
var result=UrlFetchApp.fetch(myurl, options);
//Parse JSON
var contents = result.getContentText();
var json = JSON.parse(contents);
// Logger.log("JSON string: " + JSON.stringify(json))
access_token = json["access_token"];
userProperties.setProperty("access_token", access_token);
userProperties.setProperty("access_time_expiry", new Date(mynow.getTime() + (30 * 60 * 1000)));
return "Bearer " + access_token;
}
function schwab_GetTokens(s){
// Receive the URI, strip out the code, and call Schwab to receive Bearer Token and Refresh Token
// Access token lasts for 30 minutes, refresh token lasts for 7 days before having to require user to authenticate again
// TODO: figure out a way to not require reauth every 7 days, since that is painfully stupid
//curl -X POST \https://api.schwabapi.com/v1/oauth/token \-H 'Authorization: Basic {BASE64_ENCODED_Client_ID:Client_Secret} \-H 'Content-Type: application/x-www-form-urlencoded' \-d 'grant_type=authorization_code&code={AUTHORIZATION_CODE_VALUE}&redirect_uri=https://127.0.0.1'
mycode = decodeURIComponent(s.split("code=")[1].split("&session")[0]);
var formData = {
"grant_type" : "authorization_code",
"code" : mycode,
"redirect_uri" : "https://127.0.0.1"
}
var options = {
"method" : "post",
"headers": {
"Authorization": "Basic " + encodedCredentials
},
"payload" : formData
}
var myurl="https://api.schwabapi.com/v1/oauth/token";
var result=UrlFetchApp.fetch(myurl, options);
Logger.log(result)
//Parse JSON
var contents = result.getContentText();
var json = JSON.parse(contents);
Logger.log(json)
access_token = json["access_token"];
refresh_token = json["refresh_token"];
var mynow = new Date();
userProperties.setProperty("access_token", access_token);
userProperties.setProperty("access_time_expiry", new Date(mynow.getTime() + (30 * 60 * 1000)));
userProperties.setProperty("refresh_token", refresh_token);
userProperties.setProperty("refresh_time_expiry", new Date(mynow.getTime() + (7 * 24 * 60 * 60 * 1000)));
}
//*****************************UTILITY FUNCTIONS****************************************************************
function printUserProperties() {
for (var property in userProperties.getProperties()) {
Logger.log(property + ": " + userProperties.getProperty(property));
}
}