Skip to content

Instantly share code, notes, and snippets.

@ivanelianto
Last active November 5, 2024 06:11
Show Gist options
  • Save ivanelianto/e70a872f5c9bf24f408f36e535d26f52 to your computer and use it in GitHub Desktop.
Save ivanelianto/e70a872f5c9bf24f408f36e535d26f52 to your computer and use it in GitHub Desktop.
/**
* Author: Ivan Elianto
* This script is work for Bahasa.
* You can replace "Beli" with "Buy" if you are using English.
**/
let transactions = document.querySelector("#my_last_trades tbody").children
let totalBuy = 0
let totalSell = 0
let totalCoins = 0
let currentTotalIDR = 0;
let currentTotalCoin = 0;
Object.values(transactions).sort((a, b) => {
let firstTransactionDate = new Date(a.children[0].innerText).getTime()
let secondTransactionDate = new Date(b.children[0].innerText).getTime()
// Sort from oldest transaction date
return firstTransactionDate - secondTransactionDate
}).forEach(transaction => {
let transactionType = transaction.children[1].innerText
let transactionValue = +transaction.children[4].innerText.replaceAll('.', '')
let coinValue = parseFloat(transaction.children[3].innerText.replaceAll('.', '').replaceAll(',', '.'))
if (transactionType.trim() == 'Beli') {
totalBuy += transactionValue
totalCoins += coinValue
currentTotalIDR += transactionValue
currentTotalCoin += coinValue
} else {
totalSell += transactionValue
totalCoins -= coinValue
if (totalCoins - coinValue <= 0) {
currentTotalIDR = 0;
currentTotalCoin = 0;
} else {
currentTotalIDR -= transactionValue
currentTotalCoin -= coinValue
}
}
})
console.log("-=-=-=-==-")
console.log('Avg. Price: ', (Math.ceil(currentTotalIDR / currentTotalCoin)).toLocaleString())
console.log('Total Current IDR Holding: ', currentTotalIDR.toLocaleString())
console.log('Total Current Coin Holding: ', currentTotalCoin.toLocaleString())
console.log("-=-=-=-==-")
console.log('Total Buy: ', totalBuy.toLocaleString())
console.log('Total Sell: ', totalSell.toLocaleString())
console.log('Realized PnL: ', (currentTotalIDR + totalSell - totalBuy).toLocaleString())
console.log('Unrealized PnL: ', (totalSell - totalBuy).toLocaleString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment