Skip to content

Instantly share code, notes, and snippets.

@alfonmga
Last active October 25, 2023 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonmga/50864730c3ebce7491542d146ed394cd to your computer and use it in GitHub Desktop.
Save alfonmga/50864730c3ebce7491542d146ed394cd to your computer and use it in GitHub Desktop.
A Sane and Simple bitcoin Savings plan <https://bitcointalk.org/index.php?topic=345065.0>
package main
import (
"fmt"
"log"
"os"
"strconv"
krakenapi "github.com/beldur/kraken-go-api-client"
"github.com/jedib0t/go-pretty/v6/table"
)
const initialBTC = 30.0
const multiplier = 2.0 // every time the price multiplies by this value, we rake
const rake = 0.20 // % rake
func main() {
// Fetch current Bitcoin price
btcPrice := FetchBitcoinPriceUSD()
mbtcPrice := float64(int64(float64(btcPrice) / 1000))
// Print initial net worth
btcStashUSD := initialBTC * float64(btcPrice)
fmt.Printf("Your initial USD net worth is: $%.2f.\n", btcStashUSD)
// Print current mBTC price
fmt.Printf("Current mBTC price: $%.0f.\n", mbtcPrice)
// Print rake %
fmt.Printf("Rake: %.0f%% every time the price x%.0f.\n", rake*100, multiplier)
CalcRakeMethod(btcStashUSD, mbtcPrice, rake, multiplier)
}
func FetchBitcoinPriceUSD() float64 {
api := krakenapi.New("", "")
ticker, err := api.Ticker(krakenapi.XXBTZUSD)
if err != nil {
log.Fatal(err)
}
btcUSD, err := strconv.ParseFloat(ticker.XXBTZUSD.Close[0], 64)
if err != nil {
log.Fatal(err)
}
return btcUSD
}
func CalcRakeMethod(netWorth, initialPriceMbtc, rake, multiplier float64) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"mBTC/USD", "BTC/USD", "mBTC holdings", "mBTC cashed out", "USD cashed out"})
t.SetStyle(table.StyleBold)
mbtcPrice := initialPriceMbtc
mbtcInvested := netWorth / initialPriceMbtc // Assuming we invest all in the beginning
totalRakedUSD := 0.0
totalRakedMBTC := 0.0
for {
mbtcPrice *= multiplier
rakedAmount := mbtcInvested * rake
totalRakedMBTC += rakedAmount
rakedAmountUSD := rakedAmount * mbtcPrice
totalRakedUSD += rakedAmountUSD
mbtcInvested -= rakedAmount
t.AppendRow(
[]interface{}{
fmt.Sprintf("$%.0f", mbtcPrice),
fmt.Sprintf("$%.0f", mbtcPrice*1000),
fmt.Sprintf("%.2fmBTC", mbtcInvested),
fmt.Sprintf("%.2fmBTC", rakedAmount),
fmt.Sprintf("$%.2f", rakedAmountUSD),
},
)
if mbtcPrice >= 1000 {
break // stop if mBTC >= $1k (the plan ends there because the world will look very much different by then)
}
}
t.Render()
// Print final results (total raked amount in USD and mBTC, and mBTC left over)
t2 := table.NewWriter()
t2.SetOutputMirror(os.Stdout)
t2.AppendHeader(table.Row{"USD Total raked", "mBTC Total raked", "mBTC Leftover"})
t2.SetStyle(table.StyleBold)
t2.AppendRow(
[]interface{}{
fmt.Sprintf("$%.2f", totalRakedUSD),
fmt.Sprintf("%.2fmBTC", totalRakedMBTC),
fmt.Sprintf("%.2fmBTC", mbtcInvested),
},
)
t2.Render()
}
Your initial USD net worth is: $1029597.00.
Current mBTC price: $34.
Rake: 20% every time the price x2.
┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ MBTC/USD ┃ BTC/USD ┃ MBTC HOLDINGS ┃ MBTC CASHED OUT ┃ USD CASHED OUT ┃
┣━━━━━━━━━━╋━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━┫
┃ $68 ┃ $68000 ┃ 24225.81mBTC ┃ 6056.45mBTC ┃ $411838.80 ┃
┃ $136 ┃ $136000 ┃ 19380.65mBTC ┃ 4845.16mBTC ┃ $658942.08 ┃
┃ $272 ┃ $272000 ┃ 15504.52mBTC ┃ 3876.13mBTC ┃ $1054307.33 ┃
┃ $544 ┃ $544000 ┃ 12403.62mBTC ┃ 3100.90mBTC ┃ $1686891.72 ┃
┃ $1088 ┃ $1088000 ┃ 9922.89mBTC ┃ 2480.72mBTC ┃ $2699026.76 ┃
┗━━━━━━━━━━┻━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ USD TOTAL RAKED ┃ MBTC TOTAL RAKED ┃ MBTC LEFTOVER ┃
┣━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━┫
┃ $6511006.69 ┃ 20359.37mBTC ┃ 9922.89mBTC ┃
┗━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┛
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment