88 lines
3.4 KiB
JavaScript
88 lines
3.4 KiB
JavaScript
/*
|
|
2020-01-09 - get the gains generated by my automated ETC trading.
|
|
2020-10-23 - fix problem because API requires windowing to handle more than 50 rows, implemented windowing.
|
|
*/
|
|
|
|
const key = 'SOOOJ7OhVCOpJE4DAWz8v5B25UpdYya9buF8DFAkHzmUab5E9i0PlEPh'; // API Key
|
|
const secret = 'UytAbgU8ty3n5a+JhaoIlcfh8zM4OBGY6PCC+Cfg2NI4waVzM52BBp9qmmyczrTDQpeha8q3nHeE/p7wNWKiVA=='; // API Private Key
|
|
const KrakenClient = require('kraken-api');
|
|
const kraken = new KrakenClient(key, secret);
|
|
|
|
(async () => {
|
|
try {
|
|
let params={
|
|
start : 1606759423,
|
|
ofs : 0
|
|
};
|
|
try {
|
|
let allBalances=await kraken.api('Balance');
|
|
//console.log(JSON.stringify(allBalances));
|
|
let etcUsdQuote = await kraken.api('Ticker', { pair : 'DOTUSD' });
|
|
//console.log(JSON.stringify(etcUsdQuote));
|
|
let allTransactions = await kraken.api('TradesHistory', params);
|
|
|
|
let dailyResults = [];
|
|
//console.log("Get Trade History ",JSON.stringify(allTransactions,null," "));
|
|
let allTradeIDs=Object.keys(allTransactions.result.trades);
|
|
let sumUSD=0;
|
|
let sumCommission=0;
|
|
let numTrans=allTradeIDs.length;
|
|
for (let i=0;i<numTrans;i++){
|
|
params.ofs+=1;
|
|
let theTrade=allTransactions.result.trades[allTradeIDs[i]];
|
|
if (theTrade.pair==="DOTUSD") {
|
|
console.log("DOT",JSON.stringify(theTrade));
|
|
let multiplier=1;
|
|
if (theTrade.type==="buy") {
|
|
multiplier=-1;
|
|
}
|
|
|
|
sumUSD += Number(theTrade.cost) * multiplier;
|
|
sumCommission += Number(theTrade.fee);
|
|
let transactionTime = x=new Date(theTrade.time*1000);
|
|
let transactionDate = transactionTime.toISOString().split("T")[0];
|
|
if (dailyResults[transactionDate] === undefined) {
|
|
dailyResults[transactionDate] =0;
|
|
}
|
|
dailyResults[transactionDate] += (Number(theTrade.cost) * multiplier)
|
|
dailyResults[transactionDate] -= Number(theTrade.fee);
|
|
}
|
|
|
|
// console.log(params.ofs,JSON.stringify(theTrade));
|
|
if ((params.ofs % 50) == 0) { // Not really great style to update the for condition var here, but it works...
|
|
console.log("Load more results; offset ",params.ofs);
|
|
params.nonce = new Date() * 1000;
|
|
allTransactions = await kraken.api('TradesHistory', params);
|
|
allTradeIDs=allTradeIDs.concat(Object.keys(allTransactions.result.trades));
|
|
numTrans=allTradeIDs.length;
|
|
}
|
|
}
|
|
|
|
Object.keys(dailyResults).forEach(date=>{
|
|
// console.log(date," -> ",dailyResults[date]);
|
|
});
|
|
|
|
console.log("# trades : ", params.ofs-1);
|
|
console.log("SUM USD : ", sumUSD);
|
|
console.log("SUM fees : ", sumCommission);
|
|
let netGain = sumUSD - sumCommission;
|
|
console.log("Net Gain : ", netGain);
|
|
|
|
let numCoinsHeld = Number(allBalances.result.DOT);
|
|
console.log("# coins Held : ", numCoinsHeld);
|
|
let sellPrice = Number(etcUsdQuote.result.DOTUSD.b[0]);
|
|
console.log("Current Price : ", sellPrice);
|
|
let worthHeldCoins = numCoinsHeld * sellPrice;
|
|
console.log("Worth held coins : ", worthHeldCoins);
|
|
let netLiq = netGain + worthHeldCoins;
|
|
console.log("Net Liquidation Gain : " , netLiq);
|
|
|
|
|
|
} catch (ex) {
|
|
console.error("Error getting trade history: ", ex.message);
|
|
}
|
|
} catch (ex) {
|
|
console.error("Hit general Error-handler: ", ex.message, "\n", ex.stack);
|
|
}
|
|
})();
|