change to persist transactions and handle pairs as provided by commandline parameter.
This commit is contained in:
@@ -1,12 +1,32 @@
|
|||||||
/*
|
/*
|
||||||
2020-01-09 - get the gains generated by my automated ETC trading.
|
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-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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const key = 'SOOOJ7OhVCOpJE4DAWz8v5B25UpdYya9buF8DFAkHzmUab5E9i0PlEPh'; // API Key
|
const key = 'SOOOJ7OhVCOpJE4DAWz8v5B25UpdYya9buF8DFAkHzmUab5E9i0PlEPh'; // API Key
|
||||||
const secret = 'UytAbgU8ty3n5a+JhaoIlcfh8zM4OBGY6PCC+Cfg2NI4waVzM52BBp9qmmyczrTDQpeha8q3nHeE/p7wNWKiVA=='; // API Private Key
|
const secret = 'UytAbgU8ty3n5a+JhaoIlcfh8zM4OBGY6PCC+Cfg2NI4waVzM52BBp9qmmyczrTDQpeha8q3nHeE/p7wNWKiVA=='; // API Private Key
|
||||||
const KrakenClient = require('kraken-api');
|
const KrakenClient = require('kraken-api');
|
||||||
const kraken = new KrakenClient(key, secret);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadData = (path) => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(fs.readFileSync(path, 'utf8'));
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -19,26 +39,55 @@ const kraken = new KrakenClient(key, secret);
|
|||||||
if (thePair===undefined) {
|
if (thePair===undefined) {
|
||||||
thePair="XXMRZUSD";
|
thePair="XXMRZUSD";
|
||||||
}
|
}
|
||||||
console.log("Processing pair: ", thePair);
|
thePair=thePair.toUpperCase(); // the pairs are always upper case.
|
||||||
let allBalances=await kraken.api('Balance');
|
|
||||||
//console.log(JSON.stringify(allBalances));
|
|
||||||
let etcUsdQuote = await kraken.api('Ticker', { pair : thePair });
|
|
||||||
//console.log(JSON.stringify(etcUsdQuote));
|
|
||||||
let allTransactions = await kraken.api('TradesHistory', params);
|
|
||||||
|
|
||||||
let dailyResults = [];
|
console.log("Processing pair: ", thePair);
|
||||||
//console.log("Get Trade History ",JSON.stringify(allTransactions,null," "));
|
|
||||||
|
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 allTradeIDs=Object.keys(allTransactions.result.trades);
|
||||||
let sumUSD=0;
|
|
||||||
let sumCommission=0;
|
|
||||||
let numTrans=allTradeIDs.length;
|
let numTrans=allTradeIDs.length;
|
||||||
let numTrades=0;
|
|
||||||
|
console.log("Number of new Transactions: ",numTrans);
|
||||||
for (let i=0;i<numTrans;i++){
|
for (let i=0;i<numTrans;i++){
|
||||||
params.ofs+=1;
|
params.ofs+=1;
|
||||||
let theTrade=allTransactions.result.trades[allTradeIDs[i]];
|
let theTrade=allTransactions.result.trades[allTradeIDs[i]];
|
||||||
|
|
||||||
|
// the timestamp is inclusive, therefore we have to skip that record.
|
||||||
|
if (theTrade.time === maxTimestamp) { continue; }
|
||||||
|
console.log(theTrade);
|
||||||
|
|
||||||
|
allTrades.push(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the trades by time oldest first.
|
||||||
|
allTrades=allTrades.sort(function (a,b){return(a.time - b.time);});
|
||||||
|
|
||||||
|
storeData(allTrades,"allTrades.json");
|
||||||
|
|
||||||
|
let dailyResults = [];
|
||||||
|
let sumUSD=0;
|
||||||
|
let sumCommission=0;
|
||||||
|
let numTrades=0;
|
||||||
|
for (let i=0;i<allTrades.length;i++){
|
||||||
|
let theTrade=allTrades[i];
|
||||||
if (theTrade.pair===thePair) {
|
if (theTrade.pair===thePair) {
|
||||||
numTrades++;
|
numTrades++;
|
||||||
console.log(JSON.stringify(theTrade));
|
let tradeLocalTime=new Date(theTrade.time * 1000);
|
||||||
|
//console.log(theTrade);
|
||||||
|
console.log(`${tradeLocalTime.toISOString()} [${theTrade.pair}] ${theTrade.type.substring(0,1)}#${Number(theTrade.vol).toFixed(4).padStart(8," ")}@${Number(theTrade.price).toFixed(2).padStart(8," ")} -> ${Number(theTrade.cost).toFixed(2).padStart(9," ")}(-${Number(theTrade.fee).toFixed(3).padStart(5," ")})`);//JSON.stringify(theTrade));
|
||||||
let multiplier=1;
|
let multiplier=1;
|
||||||
if (theTrade.type==="buy") {
|
if (theTrade.type==="buy") {
|
||||||
multiplier=-1;
|
multiplier=-1;
|
||||||
@@ -54,28 +103,12 @@ const kraken = new KrakenClient(key, secret);
|
|||||||
dailyResults[transactionDate] += (Number(theTrade.cost) * multiplier)
|
dailyResults[transactionDate] += (Number(theTrade.cost) * multiplier)
|
||||||
dailyResults[transactionDate] -= Number(theTrade.fee);
|
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=>{
|
let allBalances=await kraken.api('Balance');
|
||||||
// console.log(date," -> ",dailyResults[date]);
|
let pairQuote = await kraken.api('Ticker', { pair : thePair });
|
||||||
});
|
|
||||||
|
|
||||||
console.log("# trades : ", numTrades);
|
// find the currency by looking for "startsWith" match in the symbol in the pair.
|
||||||
console.log("SUM USD : ", sumUSD);
|
|
||||||
console.log("SUM fees : ", sumCommission);
|
|
||||||
let netGain = sumUSD - sumCommission;
|
|
||||||
console.log("Net Gain : ", netGain);
|
|
||||||
|
|
||||||
// find the currency by looking for "startsWith" match in the symbol in the pair..
|
|
||||||
let theCurrency=undefined;
|
let theCurrency=undefined;
|
||||||
Object.keys(allBalances.result).forEach((currency)=>{
|
Object.keys(allBalances.result).forEach((currency)=>{
|
||||||
if (thePair.indexOf(currency)===0) {
|
if (thePair.indexOf(currency)===0) {
|
||||||
@@ -83,9 +116,14 @@ const kraken = new KrakenClient(key, secret);
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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]);
|
let numCoinsHeld = Number(allBalances.result[theCurrency]);
|
||||||
console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]");
|
console.log("# coins Held : ", numCoinsHeld, " [", theCurrency,"]");
|
||||||
let sellPrice = Number(etcUsdQuote.result[thePair].b[0]);
|
let sellPrice = Number(pairQuote.result[thePair].b[0]);
|
||||||
console.log("Current Price : ", sellPrice);
|
console.log("Current Price : ", sellPrice);
|
||||||
let worthHeldCoins = numCoinsHeld * sellPrice;
|
let worthHeldCoins = numCoinsHeld * sellPrice;
|
||||||
console.log("Worth held coins : ", worthHeldCoins);
|
console.log("Worth held coins : ", worthHeldCoins);
|
||||||
|
|||||||
Reference in New Issue
Block a user