Last active
April 7, 2021 11:10
-
-
Save shiwork/6d7ff6b70d5824b7d8048b2aaa2a719c to your computer and use it in GitHub Desktop.
Bybitの資産残高をGASで取り込むためのScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"timeZone": "Asia/Tokyo", | |
"dependencies": {}, | |
"exceptionLogging": "STACKDRIVER", | |
"runtimeVersion": "V8" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() { | |
var apiKey = PropertiesService.getScriptProperties().getProperty('BYBIT_API_KEY'); | |
var secret = PropertiesService.getScriptProperties().getProperty('BYBIT_SECRET'); | |
var params = { | |
"coin":"BTC", | |
"timestamp":Date.now(), | |
"api_key":apiKey, | |
}; | |
params.sign = getSign(params, secret); | |
var url = 'https://api.bybit.com/v2/private/wallet/balance' + '?' + Object.entries(params).map(function(e) { return `${e[0]}=${e[1]}`; }).join('&'); | |
var balance_response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); | |
var tiker_response = UrlFetchApp.fetch('https://api.bybit.com/v2/public/tickers?symbol=BTCUSD', {'muteHttpExceptions': true}); | |
var parsed_balance = JSON.parse(balance_response.getContentText()); | |
var parsed_ticker = JSON.parse(tiker_response.getContentText()); | |
var btc_equity = parsed_balance.result.BTC.equity; | |
var btc_index_price = parsed_ticker.result[0].index_price; | |
var btc_valuation = btc_equity * btc_index_price; | |
var params = { | |
"coin":"ETH", | |
"timestamp":Date.now(), | |
"api_key":apiKey, | |
}; | |
params.sign = getSign(params, secret); | |
var url = 'https://api.bybit.com/v2/private/wallet/balance' + '?' + Object.entries(params).map(function(e) { return `${e[0]}=${e[1]}`; }).join('&'); | |
var balance_response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true}); | |
var tiker_response = UrlFetchApp.fetch('https://api.bybit.com/v2/public/tickers?symbol=ETHUSD', {'muteHttpExceptions': true}); | |
var parsed_balance = JSON.parse(balance_response.getContentText()); | |
var parsed_ticker = JSON.parse(tiker_response.getContentText()); | |
var eth_equity = parsed_balance.result.ETH.equity; | |
var eth_index_price = parsed_ticker.result[0].index_price; | |
var eth_valuation = eth_equity * eth_index_price; | |
var fx_rate_usdjpy = getExchangeRateUSDJPY(); | |
var valuation = btc_valuation + eth_valuation; | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName('bybit'); | |
sheet.appendRow([Utilities.formatDate(new Date(), 'JST', 'yyyy/MM/dd HH:mm:ss'), fx_rate_usdjpy*valuation, valuation, btc_valuation, btc_equity, btc_index_price, eth_valuation, eth_equity, eth_index_price, fx_rate_usdjpy]); | |
} | |
function getExchangeRateUSDJPY() { | |
var url = 'https://www.gaitameonline.com/rateaj/getrate'; | |
var response = UrlFetchApp.fetch(url); | |
var context = response.getContentText(); | |
var fx = JSON.parse(context); | |
for (var index in fx.quotes) { | |
var quote = fx.quotes[index]; | |
if (quote.currencyPairCode == 'USDJPY') { | |
return (parseFloat(quote.bid) + parseFloat(quote.ask)) / 2; | |
} | |
} | |
} | |
function getSign(parameters, secretKey) { | |
var orderedParams = ""; | |
Object.keys(parameters).sort().forEach(function(key) { | |
orderedParams += key + "=" + parameters[key] + "&"; | |
}); | |
orderedParams = orderedParams.substring(0, orderedParams.length - 1); | |
var signature = Utilities.computeHmacSha256Signature(orderedParams, secretKey); | |
return signature.reduce(function(str, chr){ | |
chr = (chr < 0 ? chr + 256 : chr).toString(16); | |
return str + (chr.length==1?'0':'')+chr; | |
}, ''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setProperty() { | |
PropertiesService.getScriptProperties().setProperty('BYBIT_API_KEY', ''); | |
PropertiesService.getScriptProperties().setProperty('BYBIT_SECRET', ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment