Created
July 3, 2021 00:56
-
-
Save larry0x/70d8724ccc17d853cc439d5b024b3dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const { table } = require("table"); | |
const pair = "ETH/USD"; | |
const initialAmount = 1.0; | |
const numberOfWeeks = 26; | |
const firstSellTimestamp = 1609473600; | |
const firstBuyTimestamp = 1609747200; | |
const interval = 7 * 24 * 60 * 60; // a week | |
const getPrice = async (timestamp) => { | |
let response = await axios.get( | |
`https://ftx.com/api/markets/${pair}/candles?resolution=15&start_time=${timestamp}&end_time=${timestamp}` | |
); | |
return response.data.result[0].close; | |
}; | |
const formatPrice = (price) => { | |
return "$" + price.toFixed(0); | |
}; | |
const formatPercent = (decimal) => { | |
const sign = decimal > 0 ? "+" : "-"; | |
return sign + Math.abs(100 * decimal).toFixed(1) + "%"; | |
}; | |
(async () => { | |
let results = []; | |
let amount = initialAmount; | |
for (let week = 0; week < numberOfWeeks; week++) { | |
process.stdout.write(`Fetching data for week ${week + 1}... `); | |
const sellTimestamp = firstSellTimestamp + week * interval; | |
const buyTimestamp = firstBuyTimestamp + week * interval; | |
const sellPrice = await getPrice(sellTimestamp); | |
const buyPrice = await getPrice(buyTimestamp); | |
const sellProceed = amount * sellPrice; | |
const buyProceed = sellProceed / buyPrice; | |
const weeklyRoi = buyProceed / amount - 1; | |
amount = buyProceed; | |
results.push([ | |
sellTimestamp, | |
formatPrice(sellPrice), | |
buyTimestamp, | |
formatPrice(buyPrice), | |
amount.toFixed(4), | |
formatPercent(weeklyRoi), | |
]); | |
console.log("Done!"); | |
} | |
return results; | |
})().then((results) => { | |
console.log("\n" + table(results)); | |
console.log( | |
"ROI:", | |
formatPercent(results[results.length - 1][4] / initialAmount - 1) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment