accounts:read and data:read scopes. A sheet never needs trading:execute.FIVESOCKET_KEY with the key as its value. A script property stays out of the cells and out of the code, but anyone who can edit the script can read it, so use a read-only key.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657const BASE = "https://api.fivesocket.com/v1" function fivesocket(path) { const key = PropertiesService.getScriptProperties().getProperty("FIVESOCKET_KEY") for (let attempt = 0; attempt < 6; attempt++) { const response = UrlFetchApp.fetch(BASE + path, { headers: { Authorization: "Bearer " + key }, muteHttpExceptions: true, }) const status = response.getResponseCode() if (status === 200) { return JSON.parse(response.getContentText()) } if (status !== 202 && status !== 429 && status !== 503) { throw new Error("FiveSocket " + status + ": " + response.getContentText()) } const headers = response.getHeaders() const wait = Number(headers["Retry-After"] || headers["retry-after"] || 5) Utilities.sleep(Math.min(wait, 30) * 1000) } throw new Error("FiveSocket did not answer in time") } function writeTab(name, rows) { const book = SpreadsheetApp.getActiveSpreadsheet() const sheet = book.getSheetByName(name) || book.insertSheet(name) sheet.clearContents() sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows) } function refreshAccount() { const accountId = fivesocket("/accounts").data[0].id const account = "/accounts/" + accountId const balance = fivesocket(account + "/balance") writeTab("Balance", [ ["Balance", "Equity", "Margin", "Free margin", "Currency", "Read at"], [Number(balance.balance), Number(balance.equity), Number(balance.margin), Number(balance.marginFree), balance.currency, balance.observedAt], ]) const positions = fivesocket(account + "/positions").positions writeTab("Positions", [ ["Symbol", "Side", "Volume", "Open price", "Current price", "Profit", "Opened at"], ...positions.map((p) => [p.symbol, p.side, Number(p.volume), Number(p.openPrice), Number(p.currentPrice), Number(p.profit), p.openedAt]), ]) const rows = [["Closed at", "Symbol", "Side", "Volume", "Close price", "Profit", "Commission", "Swap"]] let query = "?limit=1000&to=" + encodeURIComponent(new Date().toISOString()) while (query) { const page = fivesocket(account + "/trades" + query) for (const t of page.data) { rows.push([t.closedAt, t.symbol, t.side, Number(t.volume), Number(t.closePrice), Number(t.profit), Number(t.commission), Number(t.swap)]) } query = page.nextCursor ? "?limit=1000&cursor=" + encodeURIComponent(page.nextCursor) : null } writeTab("Trades", rows) }
refreshAccount once from the editor and approve the two permissions Google asks for: connecting to an external service and editing this spreadsheet.GET /v1/accounts lists linked accounts. The script takes the first one; put an account ID in its place to choose another.Number for charts and formulas.GET /v1/accounts/{accountId}/trades needs to. Without from it covers the 30 days before to, and a window can be at most 90 days. A cursor request carries no from or to.202 account_warming means the session is logging in to your broker. The script waits for Retry-After and repeats the read.refreshAccount, Time-driven, and an interval such as every 15 minutes. Each run makes four requests when the trades fit one page. Requests count toward your plan's per-minute limits and, before a trial starts, toward the free usage allowance. See errors and limits.