Skip to content

Instantly share code, notes, and snippets.

@yukihirai0505
Last active July 23, 2021 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yukihirai0505/c4b469a1f77e6fe0e4d3f8f3a60bdd7f to your computer and use it in GitHub Desktop.
Save yukihirai0505/c4b469a1f77e6fe0e4d3f8f3a60bdd7f to your computer and use it in GitHub Desktop.
ZaifのAPIを使用してイーサリアム(ETH)とネム(XEM)を指値注文するためのGoogle Apps Script

Get Started

スプレッドシートを用意して シートの名前を zaif に設定

ZaifのAPIキーやシークレット、積立したい金額を入力

dglxcxuvaaeipji

1日1回動くトリガーを設定すれば

dglxcxyvaaid_iv

勝手にコイン積立

の出来上がり

/**
* API Document => http://techbureau-api-document.readthedocs.io/ja/latest/index.html
*/
var BK = SpreadsheetApp.getActiveSpreadsheet(),
SHEET = BK.getSheetByName('zaif'),
DATA = SHEET.getRange(1, 2, 3, 1).getValues(),
API_KEY = DATA[0][0],
API_SECRET = DATA[1][0],
AMOUNT = DATA[2][0],
ZAIF = {
URL: {
PUBLIC: 'https://api.zaif.jp/api/1',
PRIVATE: 'https://api.zaif.jp/tapi'
},
CURRENCY: {
XEM: {
JPY_PAIR: 'xem_jpy',
LIMIT_DIFF: 0.0001,
DECIMAL: 0
},
ETH: {
JPY_PAIR: 'eth_jpy',
LIMIT_DIFF: 5,
DECIMAL: 4
}
}
};
function getLastPrice(pair) {
return fetchJson(ZAIF.URL.PUBLIC + '/last_price/' + pair, 'GET', true).last_price;
}
function orders() {
[ZAIF.CURRENCY.XEM, ZAIF.CURRENCY.ETH].forEach(function (currency) {
order(currency);
})
}
function order(currency) {
var lastPrice = getLastPrice(currency.JPY_PAIR),
limitPrice = lastPrice - currency.LIMIT_DIFF,
nonce = (new Date().getTime() / 1000).toFixed(0),
params = 'method=trade';
params += '&nonce=' + nonce;
params += '&currency_pair=' + currency.JPY_PAIR;
params += '&action=bid&price=' + limitPrice;
params += '&amount=' + Math.floor(AMOUNT / limitPrice * Math.pow(10, currency.DECIMAL)) / Math.pow(10, currency.DECIMAL);
params += '&comment=bot';
Logger.log(params);
var result = fetchJson(ZAIF.URL.PRIVATE, 'POST', false, params);
if (result.error === 'trade temporarily unavailable.') {
Logger.log(result.error);
Utilities.sleep(3000);
return order(currency);
}
Logger.log(result);
}
function fetchJson(url, method, isPublic, body) {
var option = isPublic ? {
method: method,
contentType: 'application/json'
} : {
method: method,
payload: body,
headers: {
'key': API_KEY,
'sign': createSignature(body)
}
};
return JSON.parse(UrlFetchApp.fetch(url, option));
}
function createSignature(body) {
var hmac = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, body, API_SECRET);
return hmac.map(function (e) {
return ('0' + (e & 0xFF).toString(16)).slice(-2)
}).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment