rewrite conversion from py to node

This commit is contained in:
Bitcarrot
2021-10-07 16:28:55 -07:00
parent ce0484689a
commit 17a8aaa56c
6 changed files with 53 additions and 32 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -3,14 +3,16 @@ const fs = require('fs');
module.exports = { module.exports = {
bfx: async function() { bfx: async function() {
const hkdrate = 0.1287 // approximate rate
const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD" const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
const response = await axios.get(btcDataURL) const response = await axios.get(btcDataURL)
const data = response.data const data = response.data
//console.log(data[6]) //console.log(data[6])
const satDenominator = 100000000 const satDenominator = 100000000
const hkdrate = 0.1287 // approximate rate // see docs : https://docs.bitfinex.com/reference#rest-public-ticker
btcLastPrice = data[6] btcLastPrice = data[6]
const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate) const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate)
//console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd) //console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd)
return sathkd return sathkd

View File

@@ -4,18 +4,14 @@ const fs = require('fs');
// this script is to convert the downloaded data // this script is to convert the downloaded data
// 1. get data from site // 1. get data from site
// bitfinex daily close data: https://www.investing.com/crypto/bitcoin/btc-usd-historical-data // bitfinex daily close data: https://www.investing.com/crypto/bitcoin/btc-usd-historical-data
// 2. save as BTC_USD_Bitfinex_HistoricalData.csv // 2. save as BTC_USD_Bitfinex_HistoricalData.csv,
// 3. convert to the format of historical. using convertformat() // convert to the format of historical. using convertformat()
// check result. // pipe result to new_history, concatenate to historical,
// 4. pipe result to new_history, concatenate to historical,
// use mergefiles(), check result
// new file is named historical_merged. copy over the public/static/historical // new file is named historical_merged. copy over the public/static/historical
// convert historical to hkd_historical using other script // convert historical to hkd_historical
function convertformat() { function convertformat() {
const filepath = "./archive/BTC_USD_Bitfinex_HistoricalData.csv" const filepath = "./archive/BTC_USD_Bitfinex_HistoricalData.csv"
let data = [] let data = []
@@ -37,20 +33,13 @@ function convertformat() {
}) })
.on('end', () => { .on('end', () => {
// console.log(data) // console.log(data)
var new_json = JSON.stringify(data) var new_history = JSON.stringify(data)
console.log(new_json) console.log(new_history)
// append this to the historical data file. fs.writeFileSync("./archive/new_history", new_history)
fs.writeFileSync("./archive/new_history", new_json) let newh = JSON.parse(new_history).reverse()
// write to regular csv file:
// csvWriter.writeRecords(data).then(() => console.log('CSV file written'))
})
}
function mergefiles() { // get original historical data file. concat new dates
// merge two files together // no logic handled here for date overlap
const newhistory = fs.readFileSync('./archive/new_history', { encoding: 'utf8' })
let newh = JSON.parse(newhistory).reverse()
//console.log(newh)
const historical = "./public/static/historical" const historical = "./public/static/historical"
const histcontent = fs.readFileSync(historical, { encoding: 'utf8' }) const histcontent = fs.readFileSync(historical, { encoding: 'utf8' })
let hist = JSON.parse(histcontent) let hist = JSON.parse(histcontent)
@@ -58,10 +47,40 @@ function mergefiles() {
const result = JSON.stringify(hist.concat(newh)) const result = JSON.stringify(hist.concat(newh))
console.log(result) console.log(result)
fs.writeFileSync("./archive/historical_merged", result) fs.writeFileSync("./archive/historical_merged", result)
// delete old historical
fs.copyFile('./archive/historical_merged', './public/static/historical', (err) => {
if (err) throw err;
console.log('./archive/historical_merged was copied to ./public/static/historical');
});
})
} }
//convertformat() function hkdrate() {
// run 1st before 2nd command const hkdusd_rate = 7.75
const historical = "./public/static/historical"
const histcontent = fs.readFileSync(historical, { encoding: 'utf8' })
let hist = JSON.parse(histcontent)
mergefiles() let hkdData = []
hist.forEach(function(entry) {
newEntry = {
"btcusd_rate": entry['btcusd_rate'],
"date": entry['date'],
"usdsat_rate": entry['usdsat_rate'],
"sathkd_rate": parseInt(entry['usdsat_rate'] / hkdusd_rate).toFixed(0),
"btchkd_rate": parseFloat(entry['btcusd_rate'] * hkdusd_rate).toFixed(2),
}
hkdData.push(newEntry)
})
console.log(hkdData)
const hkdHistorical = JSON.stringify(hkdData)
fs.writeFileSync("./public/static/hkd_historical", hkdHistorical)
}
// first run convertformat then run hkdrate
// convertformat()
hkdrate()

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long