Skip to content

Commit

Permalink
Adding initial support to delivery futures COIN-M wallet + open orders.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomanuel committed Nov 22, 2021
1 parent cbe1e29 commit 42178ae
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
3 changes: 2 additions & 1 deletion misc/config.gs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*/

let DEBUG = false;
const VERSION = "v0.5.1";
const VERSION = "v0.5.2";
const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
const SPOT_API_URL = "https://api.binance.com";
const FUTURES_API_URL = "https://fapi.binance.com";
const DELIVERY_API_URL = "https://dapi.binance.com";
const TICKER_AGAINST = "USDT";
const REQUEST_RETRY_MAX_ATTEMPTS = 10; // Max number of attempts when the API responses with status != 200
const REQUEST_RETRY_DELAY = 1000; // Delay between API calls when it fails in milliseconds
12 changes: 11 additions & 1 deletion misc/request.gs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function BinRequest(OPTIONS) {
*/
function _request(method, url, qs, payload, opts) {
const CACHE_OK_KEY = method+"_"+url+"_"+qs;
const API_URL = opts["futures"] ? FUTURES_API_URL : _makeSpotApiUrl();
const API_URL = _makeApiUrl(opts);
const need_auth = !opts["public"]; // Calling a private endpoint
const headers = opts["headers"] || {};
const da_payload = payload ? JSON.stringify(payload) : "";
Expand Down Expand Up @@ -141,6 +141,16 @@ function BinRequest(OPTIONS) {
throw new Error("Request failed with status: "+response.getResponseCode());
}

function _makeApiUrl(opts) {
if (opts["futures"]) {
return FUTURES_API_URL;
}
if (opts["delivery"]) {
return DELIVERY_API_URL;
}
return _makeSpotApiUrl();
}

/**
* Builds an URL for the Spot API, using one of the 4 available clusters at random.
* Thank you @fabiob for the PR! :: https://github.com/fabiob
Expand Down
26 changes: 24 additions & 2 deletions misc/wallet.gs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ function BinWallet(OPTIONS) {
getCrossAssets,
getIsolatedAssets,
getFuturesAssets,
getDeliveryAssets,
getSubAccountAssets,
setSpotAssets,
setCrossAssets,
setIsolatedAssets,
setFuturesAssets,
setDeliveryAssets,
setSubAccountAssets,
getIsolatedPairs,
setIsolatedPairs,
Expand Down Expand Up @@ -61,12 +63,19 @@ function BinWallet(OPTIONS) {
}

/**
* Returns the account wallet assets for FUTURES
* Returns the account wallet assets for FUTURES USD-M
*/
function getFuturesAssets(symbol) {
return getAssets("futures", symbol);
}

/**
* Returns the account wallet assets for FUTURES COIN-M
*/
function getDeliveryAssets(symbol) {
return getAssets("delivery", symbol);
}

/**
* Returns the account wallet assets for SUB-ACCOUNTS
*/
Expand Down Expand Up @@ -102,12 +111,19 @@ function BinWallet(OPTIONS) {
}

/**
* Sets account wallet data for FUTURES
* Sets account wallet data for FUTURES USD-M
*/
function setFuturesAssets(data) {
return setAssetsData("futures", data);
}

/**
* Sets account wallet data for FUTURES COIN-M
*/
function setDeliveryAssets(data) {
return setAssetsData("delivery", data);
}

/**
* Sets account wallet data for SUB-ACCOUNTS
*/
Expand Down Expand Up @@ -290,6 +306,12 @@ function BinWallet(OPTIONS) {
return _accAssetHelper(acc, symbol, futures[symbol]);
}, totals);
}
if (isEnabled("delivery")) {
const delivery = getDeliveryAssets();
totals = Object.keys(delivery).reduce(function(acc, symbol) {
return _accAssetHelper(acc, symbol, delivery[symbol]);
}, totals);
}
if (!exclude_sub_accounts) { // Include sub-account assets
const subaccs = getSubAccountAssets();
totals = Object.keys(subaccs).reduce(function(acc, symbol) {
Expand Down
34 changes: 28 additions & 6 deletions tasks/do-orders-open.gs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ function BinDoOrdersOpen() {
const dataSpot = fetchSpotOrders(opts); // Get all SPOT orders
const dataCross = bw.isEnabled("cross") ? fetchCrossOrders(opts) : []; // Get all CROSS MARGIN orders
const dataIsolated = bw.isEnabled("isolated") ? fetchIsolatedOrders(opts) : []; // Get all ISOLATED MARGIN orders
const dataFutures = bw.isEnabled("futures") ? fetchFuturesOrders(opts) : []; // Get all FUTURES orders
return [...dataSpot, ...dataCross, ...dataIsolated, ...dataFutures];
const dataFutures = bw.isEnabled("futures") ? fetchFuturesOrders(opts) : []; // Get all FUTURES orders (USD-M)
const dataDelivery = bw.isEnabled("delivery") ? fetchDeliveryOrders(opts) : []; // Get all DELIVERY orders (COIN-M)
return [...dataSpot, ...dataCross, ...dataIsolated, ...dataFutures, ...dataDelivery];
}

function fetchSpotOrders(opts) {
Expand Down Expand Up @@ -110,21 +111,32 @@ function BinDoOrdersOpen() {
}

function fetchFuturesOrders(opts) {
Logger.log("[BinDoOrdersOpen][FUTURES] Fetching orders..");
Logger.log("[BinDoOrdersOpen][FUTURES USD-M] Fetching orders..");
const options = Object.assign({futures: true}, opts);
const orders = new BinRequest(options).get("fapi/v1/openOrders") || []; // It may fail if wallet isn't enabled!
return orders.map(function(order) {
order.market = "FUTURES";
order.market = "FUTURES USD-M";
return order;
});
}

function fetchDeliveryOrders(opts) {
Logger.log("[BinDoOrdersOpen][FUTURES COIN-M] Fetching orders..");
const options = Object.assign({delivery: true}, opts);
const orders = new BinRequest(options).get("dapi/v1/openOrders") || []; // It may fail if wallet isn't enabled!
return orders.map(function(order) {
order.market = "FUTURES COIN-M";
// Convert order.origQty that represent contracts amount
order.origQty = Math.round(parseFloat(order.origQty) / _parseOrderPrice(order) * 100000000) / 100000000;
return order;
});
}

function parse(data, {headers: show_headers}) {
const bu = BinUtils();
const header = ["Date", "Pair", "Market", "Type", "Side", "Price", "Amount", "Executed", "Total"];
const parsed = data.reduce(function(rows, order) {
const symbol = order.symbol;
const price = parseFloat(order.price) ? bu.parsePrice(order.price) : bu.parsePrice(order.stopPrice);
const price = _parseOrderPrice(order);
const amount = parseFloat(order.origQty);
const row = [
new Date(parseInt(order.time)),
Expand All @@ -145,6 +157,16 @@ function BinDoOrdersOpen() {
return BinUtils().parseBool(show_headers) ? [header, ...sorted] : sorted;
}

function _parseOrderPrice(order) {
if (parseFloat(order.price)) {
return BinUtils().parsePrice(order.price);
}
if (parseFloat(order.activatePrice)) { // Only returned for TRAILING_STOP_MARKET orders
return BinUtils().parsePrice(order.activatePrice);
}
return BinUtils().parsePrice(order.stopPrice);
}

// Return just what's needed from outside!
return {
tag,
Expand Down
14 changes: 11 additions & 3 deletions ui/menu.gs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function BinMenu(ui) {
return ui.createMenu("Wallets")
.addItem(walletEnabled("cross")+" CROSS Margin Wallet", "toggleWalletCross")
.addItem(walletEnabled("isolated")+" ISOLATED Margin Wallet", "toggleWalletIsolated")
.addItem(walletEnabled("futures")+" FUTURES Wallet", "toggleWalletFutures");
.addItem(walletEnabled("futures")+" FUTURES USD-M Wallet", "toggleWalletFutures")
.addItem(walletEnabled("delivery")+" FUTURES COIN-M Wallet", "toggleWalletDelivery");
}

function addSubAccountsMenu() {
Expand Down Expand Up @@ -140,10 +141,17 @@ function toggleWalletIsolated() {
}

/**
* Displays a confirmation to enable/disable FUTURES wallet
* Displays a confirmation to enable/disable FUTURES USD-M wallet
*/
function toggleWalletFutures() {
BinSetup().toggleWalletDisabled("futures", SpreadsheetApp.getUi());
BinSetup().toggleWalletDisabled("futures", SpreadsheetApp.getUi(), "FUTURES USD-M");
}

/**
* Displays a confirmation to enable/disable FUTURES COIN-M wallet
*/
function toggleWalletDelivery() {
BinSetup().toggleWalletDisabled("delivery", SpreadsheetApp.getUi(), "FUTURES COIN-M");
}

/**
Expand Down
16 changes: 10 additions & 6 deletions ui/setup.gs
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,23 @@ function BinSetup() {
/**
* Toggles the enabled/disabled status for a given wallet type
*/
function toggleWalletDisabled(type, ui) {
function toggleWalletDisabled(type, ui, desc) {
const wallets = getDisabledWallets();
const disabled = !!wallets[type];
const title = (disabled?"En":"Dis")+"able wallet: "+type.toUpperCase();
const text = "You will "+(disabled?"EN":"DIS")+"ABLE the "+type.toUpperCase()+" wallet!\nProceed?";
const description = (desc || type).toUpperCase();
const title = (disabled?"En":"Dis")+"able wallet: "+description;
const text = "You will "+(disabled?"EN":"DIS")+"ABLE the "+description+" wallet!\nProceed?";
const result = ui.alert(title, text, ui.ButtonSet.OK_CANCEL);
if (result !== ui.Button.OK) {
return false; // Cancel
}

Logger.log("[BinSetup] "+(disabled?"En":"Dis")+"abling wallet: "+type.toUpperCase());
Logger.log("[BinSetup] "+(disabled?"En":"Dis")+"abling wallet: "+description);
wallets[type] = !disabled;
user_props.setProperty(WALLETS_DISABLED_NAME, JSON.stringify(wallets));
const bu = BinUtils();
bu.refreshMenu();
bu.toast("The "+type.toUpperCase()+" wallet was "+(disabled?"EN":"DIS")+"ABLED!\nPlease wait while assets are refreshed..", "", 30);
bu.toast("The "+description+" wallet was "+(disabled?"EN":"DIS")+"ABLED!\nPlease wait while assets are refreshed..", "", 30);

Logger.log("[BinSetup] Clearing wallet assets and refreshing formulas..");
const bw = BinWallet();
Expand All @@ -240,9 +241,12 @@ function BinSetup() {
function getDisabledWallets() {
const data = user_props.getProperty(WALLETS_DISABLED_NAME);
const parsed = data ? JSON.parse(data) : {};
if (parsed["futures"] === undefined) { // Disabled futures wallet by default
if (parsed["futures"] === undefined) { // Disabled FUTURES USD-M wallet by default
parsed["futures"] = true;
}
if (parsed["delivery"] === undefined) { // Disabled FUTURES COIN-M wallet by default
parsed["delivery"] = true;
}
return parsed;
}

Expand Down

0 comments on commit 42178ae

Please sign in to comment.