Files
gocryptotrader/cmd/portfolio/portfolio.go
Samuael A. 3f534a15f1 cmd/exchange_template, exchanges: Update templates and propogate to exchanges (#1777)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* Exchange test template change

* A different approach

* fix conflict with gateio timeInForce

* minor exchange template update

* Minor fix to test_files template

* Update order tests

* Complete updating the order unit tests

* Updating exchange wrapper and test template files

* update kucoin and deribit wrapper to match the time in force change

* minor comment update

* fix time-in-force related test errors

* linter issue fix

* ADD_NEW_EXCHANGE documentation update

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* consistency guideline added

* fix time-in-force error in gateio

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* update exchange template

* update wrapper template file

* policy comments, and template files update

* rename all exchange types name to Exchange

* update on template files and template generation

* templates and generation code and other updates

* linter issue fix

* added subscriptions and websocket templates

* update ADD_NEW_EXCHANGE.md with recent binance functions and implementations

* rename template files and update unit tests

* minor template and unit test fix

* rename templates and fix on unit tests

* update on template files and documentation

* removed unnecessary tag fix and update templates

* fix Add_NEW_EXCHANGE.md doc file

* formatting, comments, and error checks update on template files

* rename exchange receivers to e and ex for consistency

* rename unit test exchange receiver and minor updates

* linter issues fix

* fix deribit issue and minor style update

* fix test issues caused by receiver change

* raname local variables exchange declaration variables

* update templates comments

* update templates and related comments

* renamed ex to e

* update template comments

* toggle WS to false to improve coverage

* template comments update

* added test coverage to Ws enabled and minor changes

---------

Co-authored-by: Samuel Reid <43227667+cranktakular@users.noreply.github.com>
2025-07-17 10:46:36 +10:00

192 lines
4.6 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-corp/gocryptotrader/portfolio"
)
var (
priceMap map[*currency.Item]float64
displayCurrency currency.Code
)
func printSummary(msg string, amount float64) {
log.Println()
log.Printf("%s in USD: $%.2f", msg, amount)
if !displayCurrency.Equal(currency.USD) {
conv, err := currency.ConvertFiat(amount, currency.USD, displayCurrency)
if err != nil {
log.Println(err)
} else {
var symb string
symb, err = currency.GetSymbolByCurrencyName(displayCurrency)
if err != nil {
log.Printf("%s in %s: %.2f",
msg,
displayCurrency,
conv)
} else {
log.Printf("%s in %s: %s%.2f",
msg,
displayCurrency,
symb,
conv)
}
}
}
}
func getOnlineOfflinePortfolio(coins []portfolio.Coin, online bool) {
var totals float64
for _, x := range coins {
value := priceMap[x.Coin.Item] * x.Balance
totals += value
log.Printf("\t%v %v Subtotal: $%.2f Coin percentage: %.2f%%\n", x.Coin,
x.Balance, value, x.Percentage)
}
if !online {
printSummary("\tOffline balance", totals)
} else {
printSummary("\tOnline balance", totals)
}
}
func main() {
var inFile, key string
flag.StringVar(&inFile, "config", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()
log.Println("GoCryptoTrader: portfolio tool.")
var cfg config.Config
err := cfg.LoadConfig(inFile, true)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println("Loaded config file.")
displayCurrency = cfg.Currency.FiatDisplayCurrency
port := cfg.Portfolio
result := port.GetPortfolioSummary()
log.Println("Fetched portfolio data.")
type PortfolioTemp struct {
Balance float64
Subtotal float64
}
portfolioMap := make(map[*currency.Item]PortfolioTemp)
total := float64(0)
log.Println("Fetching currency data..")
var fiatCurrencies []currency.Code
for _, y := range result.Totals {
if y.Coin.IsFiatCurrency() {
fiatCurrencies = append(fiatCurrencies, y.Coin)
}
}
err = currency.SeedForeignExchangeData(fiatCurrencies)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println("Fetched currency data.")
log.Println("Fetching ticker data and calculating totals..")
priceMap = make(map[*currency.Item]float64)
priceMap[currency.USD.Item] = 1
for _, y := range result.Totals {
pf := PortfolioTemp{}
pf.Balance = y.Balance
pf.Subtotal = 0
if y.Coin.IsFiatCurrency() {
if !y.Coin.Equal(currency.USD) {
conv, err := currency.ConvertFiat(y.Balance, y.Coin, currency.USD)
if err != nil {
log.Println(err)
} else {
priceMap[y.Coin.Item] = conv / y.Balance
pf.Subtotal = conv
}
} else {
pf.Subtotal = y.Balance
}
} else {
bf := bitfinex.Exchange{}
bf.SetDefaults()
bf.Verbose = false
pair := "t" + y.Coin.String() + currency.USD.String()
ticker, errf := bf.GetTicker(context.TODO(), pair)
if errf != nil {
log.Println(errf)
} else {
priceMap[y.Coin.Item] = ticker.Last
pf.Subtotal = ticker.Last * y.Balance
}
}
portfolioMap[y.Coin.Item] = pf
total += pf.Subtotal
}
log.Println("Done.")
log.Println()
log.Println("PORTFOLIO TOTALS:")
for x, y := range portfolioMap {
code := currency.Code{Item: x}
log.Printf("\t%s Amount: %f Subtotal: $%.2f USD (1 %s = $%.2f USD). Percentage of portfolio %.3f%%",
code,
y.Balance,
y.Subtotal,
code,
y.Subtotal/y.Balance,
y.Subtotal/total*100/1)
}
printSummary("\tTotal balance", total)
log.Println("OFFLINE COIN TOTALS:")
getOnlineOfflinePortfolio(result.Offline, false)
log.Println("ONLINE COIN TOTALS:")
getOnlineOfflinePortfolio(result.Online, true)
log.Println("OFFLINE COIN SUMMARY:")
var totals float64
for x, y := range result.OfflineSummary {
log.Printf("\t%s:", x)
totals = 0
for z := range y {
value := priceMap[x.Item] * y[z].Balance
totals += value
log.Printf("\t %s Amount: %f Subtotal: $%.2f Coin percentage: %.2f%%\n",
y[z].Address, y[z].Balance, value, y[z].Percentage)
}
printSummary(fmt.Sprintf("\t %s balance", x), totals)
}
log.Println("ONLINE COINS SUMMARY:")
for x, y := range result.OnlineSummary {
log.Printf("\t%s:", x)
totals = 0
for z, w := range y {
value := priceMap[z.Item] * w.Balance
totals += value
log.Printf("\t %s Amount: %f Subtotal $%.2f Coin percentage: %.2f%%",
z, w.Balance, value, w.Percentage)
}
printSummary("\t Exchange balance", totals)
}
}