diff --git a/kraken-api_plain/KrakenTradeHistory.js b/kraken-api_plain/KrakenTradeHistory.js index 3fd316f..91d7849 100644 --- a/kraken-api_plain/KrakenTradeHistory.js +++ b/kraken-api_plain/KrakenTradeHistory.js @@ -9,130 +9,118 @@ const secret = 'UytAbgU8ty3n5a+JhaoIlcfh8zM4OBGY6PCC+Cfg2NI4waVzM52BBp9qmm const KrakenClient = require('kraken-api'); const kraken = new KrakenClient(key, secret); const fs = require('fs') - - + const storeData = (data, path) => { - try { - fs.writeFileSync(path, JSON.stringify(data)); - } catch (err) { - console.error(err); - } + try { + fs.writeFileSync(path, JSON.stringify(data)); + } catch (err) { + console.error("error in storeData ",err); + } } const loadData = (path) => { - try { - return JSON.parse(fs.readFileSync(path, 'utf8')); - } catch (err) { - console.error(err); - return false; - } + try { + return JSON.parse(fs.readFileSync(path, 'utf8')); + } catch (err) { + console.error("error in loadData ",err); + return false; + } +} + +const utcToLocal = (utc) => { + return new Date(utc.getTime() - (utc.getTimezoneOffset() * 60000)); } (async () => { - try { - let params={ - ofs : 0 - }; - try { - // get the pair to process from the first parameter and fall back if not provided. - let thePair=process.argv[2]; - if (thePair===undefined) { - thePair="XXMRZUSD"; - } - thePair=thePair.toUpperCase(); // the pairs are always upper case. - - console.log("Processing pair: ", thePair); - - let maxTimestamp=0; - let allTrades=loadData("allTrades.json"); - 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; - - console.log("Number of new Transactions: ",numTrans); - for (let i=0;i ${Number(theTrade.cost).toFixed(2).padStart(9," ")}(-${Number(theTrade.fee).toFixed(3).padStart(5," ")})`);//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); - } - } - - let allBalances=await kraken.api('Balance'); - let pairQuote = await kraken.api('Ticker', { pair : thePair }); - - // find the currency by looking for "startsWith" match in the symbol in the pair. - let theCurrency=undefined; - Object.keys(allBalances.result).forEach((currency)=>{ - if (thePair.indexOf(currency)===0) { - theCurrency=currency; - } - }); - - console.log("# trades : ", numTrades); - console.log("SUM USD : ", sumUSD); - console.log("SUM fees : ", sumCommission); - let netGain = sumUSD - sumCommission; - console.log("Net Gain : ", netGain); - let numCoinsHeld = Number(allBalances.result[theCurrency]); - console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]"); - let sellPrice = Number(pairQuote.result[thePair].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); + try { + // get the pair to process from the first parameter and fall back if not provided. + let thePair=process.argv[2]; + 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 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; + + console.log("Number of new Transactions : ",numTrans); + for (let i=0;i ${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; + + sumUSD += Number(theTrade.cost) * multiplier; + sumCommission += Number(theTrade.fee); + } + } + + let allBalances=await kraken.api('Balance'); + let pairQuote = await kraken.api('Ticker', { pair : thePair }); + + // find the currency by looking for "startsWith" type match in the symbol in the pair. + // (not sure if that always works, but it works for all cases I checked it so far). + let theCurrency=undefined; + Object.keys(allBalances.result).forEach((currency)=>{ + if (thePair.indexOf(currency)===0) { + theCurrency=currency; + } + }); + + console.log("# trades : ", numTrades); + console.log("SUM USD : ", sumUSD); + console.log("SUM fees : ", sumCommission); + let netGain = sumUSD - sumCommission; + console.log("Net Gain : ", netGain); + let numCoinsHeld = Number(allBalances.result[theCurrency]); + console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]"); + let sellPrice = Number(pairQuote.result[thePair].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("Hit general Error-handler: ", ex.message, "\n", ex.stack); + console.error("General Error: ", ex.message, "\n", ex.stack); } })(); diff --git a/kraken-api_plain/KrakenTradeHistoryDOT.js b/kraken-api_plain/KrakenTradeHistoryDOT.js deleted file mode 100644 index a364a69..0000000 --- a/kraken-api_plain/KrakenTradeHistoryDOT.js +++ /dev/null @@ -1,87 +0,0 @@ -/* -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{ - // 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); - } -})(); diff --git a/kraken-api_plain/KrakenTradeHistoryETC.js b/kraken-api_plain/KrakenTradeHistoryETC.js deleted file mode 100644 index 6b08cd7..0000000 --- a/kraken-api_plain/KrakenTradeHistoryETC.js +++ /dev/null @@ -1,210 +0,0 @@ -/* -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={ - ofs : 0 - }; - try { - let allBalances=await kraken.api('Balance'); - //console.log(JSON.stringify(allBalances)); - let etcUsdQuote = await kraken.api('Ticker', { pair : 'XETCZUSD' }); - //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{ - // 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.XETC); - console.log("# coins Held : ", numCoinsHeld); - let sellPrice = Number(etcUsdQuote.result.XETCZUSD.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); - } -})(); - - - -/* -{ - "error": [], - "result": { - "trades": { - "TUJ6CI-YOYLW-S6LDBQ": { - "ordertxid": "OWBBBM-UQS7T-BH5RTY", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578609866.7625, - "type": "sell", - "ordertype": "market", - "price": "5.11900000", - "cost": "51.18999999", - "fee": "0.13309399", - "vol": "10.00000000", - "margin": "0.00000000", - "misc": "" - }, - "T7VTAJ-W5HBL-KVG2BQ": { - "ordertxid": "OJKOCP-RFMVO-D5AD3W", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578604045.7998, - "type": "buy", - "ordertype": "market", - "price": "5.01700000", - "cost": "50.17000000", - "fee": "0.13044200", - "vol": "10.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TEXK75-SRRBQ-5DUVIG": { - "ordertxid": "ON4CNP-RDAWZ-A77OMH", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578600324.9445, - "type": "sell", - "ordertype": "market", - "price": "5.11800000", - "cost": "51.18000001", - "fee": "0.13306799", - "vol": "10.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TAJGH2-677GW-PLUYFF": { - "ordertxid": "OZFB5U-D4PX7-TA23BP", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578599314.3601, - "type": "sell", - "ordertype": "limit", - "price": "5.08400000", - "cost": "50.84000000", - "fee": "0.13218400", - "vol": "10.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TRPXQ3-FSM2Q-6BC3P7": { - "ordertxid": "OTJC4F-6MXXU-JHICN4", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578590114.8608, - "type": "buy", - "ordertype": "market", - "price": "4.89700000", - "cost": "48.97000000", - "fee": "0.12732200", - "vol": "10.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TYIRWY-3JLW4-3GM4OO": { - "ordertxid": "OZB455-X4YWN-X3A7J4", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578586116.836, - "type": "buy", - "ordertype": "limit", - "price": "4.96600000", - "cost": "44.69400000", - "fee": "0.11620440", - "vol": "9.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TDZJSE-PCTDA-5H6GHW": { - "ordertxid": "OUUCEQ-BQRXP-AWNWUX", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETCZUSD", - "time": 1578560505.5051, - "type": "buy", - "ordertype": "market", - "price": "5.01500000", - "cost": "5.01500000", - "fee": "0.01303900", - "vol": "1.00000000", - "margin": "0.00000000", - "misc": "" - }, - "TTNMCG-CPYYR-DIMUQY": { - "ordertxid": "O4SEFZ-GFJWL-JHQE6F", - "postxid": "TKH2SE-M7IF5-CFI7LT", - "pair": "XETHZUSD", - "time": 1578499686.8045, - "type": "buy", - "ordertype": "limit", - "price": "140.82000", - "cost": "50.00000", - "fee": "0.13000", - "vol": "0.35506320", - "margin": "0.00000", - "misc": "" - } - }, - "count": 8 - } -} -*/ \ No newline at end of file