From 388965e34cd3cbc444ec8bf4d4c5766193485715 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Thu, 31 Dec 2020 15:51:05 -0600 Subject: [PATCH] Make datafile dynamic --- kraken-api_plain/KrakenTradeHistory.js | 79 +++++++++++++++----------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/kraken-api_plain/KrakenTradeHistory.js b/kraken-api_plain/KrakenTradeHistory.js index 91d7849..1ef86b3 100644 --- a/kraken-api_plain/KrakenTradeHistory.js +++ b/kraken-api_plain/KrakenTradeHistory.js @@ -2,8 +2,14 @@ 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. 2020-12-02 - change to persist transactions and handle pairs as provided by commandline parameter. +2020-12-04 - handle commandline inlcuding verbosity of output. */ +const yargs = require('yargs/yargs') +const { hideBin } = require('yargs/helpers') +const argv = yargs(hideBin(process.argv)).argv + + const key = 'SOOOJ7OhVCOpJE4DAWz8v5B25UpdYya9buF8DFAkHzmUab5E9i0PlEPh'; // API Key const secret = 'UytAbgU8ty3n5a+JhaoIlcfh8zM4OBGY6PCC+Cfg2NI4waVzM52BBp9qmmyczrTDQpeha8q3nHeE/p7wNWKiVA=='; // API Private Key const KrakenClient = require('kraken-api'); @@ -34,48 +40,53 @@ const utcToLocal = (utc) => { (async () => { try { // get the pair to process from the first parameter and fall back if not provided. - let thePair=process.argv[2]; + let thePair=argv.pair; if (thePair===undefined) { thePair="XXMRZUSD"; } thePair=thePair.toUpperCase(); // the pairs are always upper case. console.log("Processing pair : ", thePair); - let allTrades=loadData("allTrades.json"); + let dataFile = argv.dataFile; + if (dataFile===undefined) { + dataFile = "allTrades.json"; + } + let allTrades=loadData(dataFile); let params={ ofs : 0 }; let maxTimestamp=0; allTrades.forEach(dta=>{if (dta.time>maxTimestamp) {maxTimestamp=dta.time;} }); params.start=maxTimestamp; console.log("Max-Timestamp : ",maxTimestamp); - let allTransactions = await kraken.api('TradesHistory', params); - let allTradeIDs=Object.keys(allTransactions.result.trades); - let numTrans=allTradeIDs.length; + if (!argv.skipRequestTrades) { + let allTransactions = await kraken.api('TradesHistory', params); + let allTradeIDs=Object.keys(allTransactions.result.trades); + let numTrans=allTradeIDs.length; - console.log("Number of new Transactions : ",numTrans); - for (let i=0;i { numTrades++; let tradeLocalTime=utcToLocal(new Date(theTrade.time * 1000)).toISOString().substr(0,19).replace('T',' '); - console.log(`${tradeLocalTime} [${theTrade.pair}] ${theTrade.type.substring(0,1)}#${Number(theTrade.vol).toFixed(4).padStart(10," ")}@${Number(theTrade.price).toFixed(2).padStart(8," ")} -> ${Number(theTrade.cost).toFixed(2).padStart(9," ")}(-${Number(theTrade.fee).toFixed(3).padStart(5," ")})`); + if (argv.v) {console.log(`${tradeLocalTime} [${theTrade.pair}] ${theTrade.type.substring(0,1)}#${Number(theTrade.vol).toFixed(4).padStart(10," ")}@${Number(theTrade.price).toFixed(2).padStart(8," ")} -> ${Number(theTrade.cost).toFixed(2).padStart(9," ")}(-${Number(theTrade.fee).toFixed(3).padStart(5," ")})`);} // selling adds $$$ to our Account, buying takes money out of the account let multiplier=theTrade.type==="buy"?-1:1; @@ -107,19 +118,19 @@ const utcToLocal = (utc) => { } }); - console.log("# trades : ", numTrades); - console.log("SUM USD : ", sumUSD); - console.log("SUM fees : ", sumCommission); + console.log("# trades : ", numTrades); + console.log("SUM USD : ", sumUSD); + console.log("SUM fees : ", sumCommission); let netGain = sumUSD - sumCommission; - console.log("Net Gain : ", netGain); + console.log("Net Gain : ", netGain); let numCoinsHeld = Number(allBalances.result[theCurrency]); - console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]"); + console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]"); let sellPrice = Number(pairQuote.result[thePair].b[0]); - console.log("Current Price : ", sellPrice); + console.log("Current Price : ", sellPrice); let worthHeldCoins = numCoinsHeld * sellPrice; - console.log("Worth held coins : ", worthHeldCoins); + console.log("Worth held coins : ", worthHeldCoins); let netLiq = netGain + worthHeldCoins; - console.log("Net Liquidation Gain : " , netLiq); + console.log("Net Liquidation Gain : " , netLiq); } catch (ex) { console.error("General Error: ", ex.message, "\n", ex.stack); }