mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-19 15:10:05 +00:00
Package portfolio
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/portfolio"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -71,10 +72,10 @@ type Config struct {
|
||||
Name string
|
||||
EncryptConfig int
|
||||
Cryptocurrencies string
|
||||
Portfolio PortfolioConfig `json:"PortfolioAddresses"`
|
||||
SMS SMSGlobalConfig `json:"SMSGlobal"`
|
||||
Webserver WebserverConfig `json:"Webserver"`
|
||||
Exchanges []ExchangeConfig `json:"Exchanges"`
|
||||
Portfolio portfolio.PortfolioBase `json:"PortfolioAddresses"`
|
||||
SMS SMSGlobalConfig `json:"SMSGlobal"`
|
||||
Webserver WebserverConfig `json:"Webserver"`
|
||||
Exchanges []ExchangeConfig `json:"Exchanges"`
|
||||
}
|
||||
|
||||
type ExchangeConfig struct {
|
||||
@@ -92,16 +93,6 @@ type ExchangeConfig struct {
|
||||
BaseCurrencies string
|
||||
}
|
||||
|
||||
type PortfolioAddressConfig struct {
|
||||
Address string
|
||||
CoinType string
|
||||
Balance float64
|
||||
}
|
||||
|
||||
type PortfolioConfig struct {
|
||||
Addresses []PortfolioAddressConfig
|
||||
}
|
||||
|
||||
func (c *Config) GetConfigEnabledExchanges() int {
|
||||
counter := 0
|
||||
for i := range c.Exchanges {
|
||||
|
||||
7
main.go
7
main.go
@@ -30,6 +30,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/okcoin"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/poloniex"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-/gocryptotrader/portfolio"
|
||||
"github.com/thrasher-/gocryptotrader/smsglobal"
|
||||
)
|
||||
|
||||
@@ -55,6 +56,7 @@ type ExchangeMain struct {
|
||||
|
||||
type Bot struct {
|
||||
config *config.Config
|
||||
portfolio *portfolio.PortfolioBase
|
||||
exchange ExchangeMain
|
||||
exchanges []exchange.IBotExchange
|
||||
tickers []ticker.Ticker
|
||||
@@ -147,7 +149,9 @@ func main() {
|
||||
|
||||
log.Println("Successfully retrieved config currencies.")
|
||||
|
||||
go StartPortfolioWatcher()
|
||||
bot.portfolio = &portfolio.Portfolio
|
||||
bot.portfolio.SeedPortfolio(bot.config.Portfolio)
|
||||
go portfolio.StartPortfolioWatcher()
|
||||
|
||||
if bot.config.Webserver.Enabled {
|
||||
err := bot.config.CheckWebserverConfigValues()
|
||||
@@ -201,6 +205,7 @@ func HandleInterrupt() {
|
||||
|
||||
func Shutdown() {
|
||||
log.Println("Bot shutting down..")
|
||||
bot.config.Portfolio = portfolio.Portfolio
|
||||
err := bot.config.SaveConfig()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package portfolio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -17,15 +16,19 @@ const (
|
||||
|
||||
ETHERCHAIN_API_URL = "https://etherchain.org/api"
|
||||
ETHERCHAIN_ACCOUNT_MULTIPLE = "account/multiple"
|
||||
PORTFOLIO_ADDRESS_EXCHANGE = "Exchange"
|
||||
PORTFOLIO_ADDRESS_CUSTOM = "Custom"
|
||||
)
|
||||
|
||||
var Portfolio PortfolioBase
|
||||
|
||||
type PortfolioAddress struct {
|
||||
Address string
|
||||
CoinType string
|
||||
Balance float64
|
||||
}
|
||||
|
||||
type Portfolio struct {
|
||||
type PortfolioBase struct {
|
||||
Addresses []PortfolioAddress
|
||||
}
|
||||
|
||||
@@ -103,8 +106,8 @@ func GetBlockrAddressMulti(addresses []string, coinType string) (BlockrAddressBa
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetAddressBalance(address string) (float64, bool) {
|
||||
for _, x := range bot.config.Portfolio.Addresses {
|
||||
func (p *PortfolioBase) GetAddressBalance(address string) (float64, bool) {
|
||||
for _, x := range p.Addresses {
|
||||
if x.Address == address {
|
||||
return x.Balance, true
|
||||
}
|
||||
@@ -112,8 +115,8 @@ func GetAddressBalance(address string) (float64, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func AddressExists(address string) bool {
|
||||
for _, x := range bot.config.Portfolio.Addresses {
|
||||
func (p *PortfolioBase) AddressExists(address string) bool {
|
||||
for _, x := range p.Addresses {
|
||||
if x.Address == address {
|
||||
return true
|
||||
}
|
||||
@@ -121,15 +124,15 @@ func AddressExists(address string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func UpdateAddressBalance(address string, amount float64) {
|
||||
for x, _ := range bot.config.Portfolio.Addresses {
|
||||
if bot.config.Portfolio.Addresses[x].Address == address {
|
||||
bot.config.Portfolio.Addresses[x].Balance = amount
|
||||
func (p *PortfolioBase) UpdateAddressBalance(address string, amount float64) {
|
||||
for x, _ := range p.Addresses {
|
||||
if p.Addresses[x].Address == address {
|
||||
p.Addresses[x].Balance = amount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func UpdatePortfolio(addresses []string, coinType string) bool {
|
||||
func (p *PortfolioBase) UpdatePortfolio(addresses []string, coinType string) bool {
|
||||
if coinType == "ETH" {
|
||||
result, err := GetEthereumBalance(addresses)
|
||||
if err != nil {
|
||||
@@ -137,10 +140,10 @@ func UpdatePortfolio(addresses []string, coinType string) bool {
|
||||
}
|
||||
|
||||
for _, x := range result.Data {
|
||||
if !AddressExists(x.Address) {
|
||||
bot.config.Portfolio.Addresses = append(bot.config.Portfolio.Addresses, config.PortfolioAddressConfig{Address: x.Address, CoinType: coinType, Balance: x.Balance / common.WEI_PER_ETHER})
|
||||
if !p.AddressExists(x.Address) {
|
||||
p.Addresses = append(p.Addresses, PortfolioAddress{Address: x.Address, CoinType: coinType, Balance: x.Balance / common.WEI_PER_ETHER})
|
||||
} else {
|
||||
UpdateAddressBalance(x.Address, x.Balance)
|
||||
p.UpdateAddressBalance(x.Address, x.Balance)
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -151,10 +154,10 @@ func UpdatePortfolio(addresses []string, coinType string) bool {
|
||||
return false
|
||||
}
|
||||
for _, x := range result.Data {
|
||||
if !AddressExists(x.Address) {
|
||||
bot.config.Portfolio.Addresses = append(bot.config.Portfolio.Addresses, config.PortfolioAddressConfig{Address: x.Address, CoinType: coinType, Balance: x.Balance})
|
||||
if !p.AddressExists(x.Address) {
|
||||
p.Addresses = append(p.Addresses, PortfolioAddress{Address: x.Address, CoinType: coinType, Balance: x.Balance})
|
||||
} else {
|
||||
UpdateAddressBalance(x.Address, x.Balance)
|
||||
p.UpdateAddressBalance(x.Address, x.Balance)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -162,18 +165,18 @@ func UpdatePortfolio(addresses []string, coinType string) bool {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if !AddressExists(result.Data.Address) {
|
||||
bot.config.Portfolio.Addresses = append(bot.config.Portfolio.Addresses, config.PortfolioAddressConfig{Address: result.Data.Address, CoinType: coinType, Balance: result.Data.Balance})
|
||||
if !p.AddressExists(result.Data.Address) {
|
||||
p.Addresses = append(p.Addresses, PortfolioAddress{Address: result.Data.Address, CoinType: coinType, Balance: result.Data.Balance})
|
||||
} else {
|
||||
UpdateAddressBalance(result.Data.Address, result.Data.Balance)
|
||||
p.UpdateAddressBalance(result.Data.Address, result.Data.Balance)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func GetPortfolioSummary(coinFilter string) map[string]float64 {
|
||||
func (p *PortfolioBase) GetPortfolioSummary(coinFilter string) map[string]float64 {
|
||||
result := make(map[string]float64)
|
||||
for _, x := range bot.config.Portfolio.Addresses {
|
||||
for _, x := range p.Addresses {
|
||||
if coinFilter != "" && coinFilter != x.CoinType {
|
||||
continue
|
||||
}
|
||||
@@ -187,21 +190,25 @@ func GetPortfolioSummary(coinFilter string) map[string]float64 {
|
||||
return result
|
||||
}
|
||||
|
||||
func GetPortfolioGroupedCoin() map[string][]string {
|
||||
func (p *PortfolioBase) GetPortfolioGroupedCoin() map[string][]string {
|
||||
result := make(map[string][]string)
|
||||
for _, x := range bot.config.Portfolio.Addresses {
|
||||
for _, x := range p.Addresses {
|
||||
result[x.CoinType] = append(result[x.CoinType], x.Address)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (p *PortfolioBase) SeedPortfolio(port PortfolioBase) {
|
||||
p.Addresses = port.Addresses
|
||||
}
|
||||
|
||||
func StartPortfolioWatcher() {
|
||||
addrCount := len(bot.config.Portfolio.Addresses)
|
||||
addrCount := len(Portfolio.Addresses)
|
||||
log.Printf("PortfolioWatcher started: Have %d address(es) in portfolio.\n", addrCount)
|
||||
for {
|
||||
data := GetPortfolioGroupedCoin()
|
||||
data := Portfolio.GetPortfolioGroupedCoin()
|
||||
for key, value := range data {
|
||||
success := UpdatePortfolio(value, key)
|
||||
success := Portfolio.UpdatePortfolio(value, key)
|
||||
if success {
|
||||
log.Printf("PortfolioWatcher: Successfully updated address balance for %s address(es) %s\n", key, value)
|
||||
}
|
||||
@@ -209,3 +216,7 @@ func StartPortfolioWatcher() {
|
||||
time.Sleep(time.Minute * 10)
|
||||
}
|
||||
}
|
||||
|
||||
func GetPortfolio() *PortfolioBase {
|
||||
return &Portfolio
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/bitfinex"
|
||||
"github.com/thrasher-/gocryptotrader/portfolio"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -47,22 +48,16 @@ func main() {
|
||||
log.Fatal("File isn't in JSON format")
|
||||
}
|
||||
|
||||
result := make(map[string]float64)
|
||||
for _, x := range cfg.Portfolio.Addresses {
|
||||
balance, ok := result[x.CoinType]
|
||||
if !ok {
|
||||
result[x.CoinType] = x.Balance
|
||||
} else {
|
||||
result[x.CoinType] = x.Balance + balance
|
||||
}
|
||||
}
|
||||
port := portfolio.PortfolioBase{}
|
||||
port.SeedPortfolio(cfg.Portfolio)
|
||||
result := port.GetPortfolioSummary("")
|
||||
|
||||
type Portfolio struct {
|
||||
type PortfolioTemp struct {
|
||||
Balance float64
|
||||
Subtotal float64
|
||||
}
|
||||
|
||||
stuff := make(map[string]Portfolio)
|
||||
stuff := make(map[string]PortfolioTemp)
|
||||
total := float64(0)
|
||||
|
||||
for x, y := range result {
|
||||
@@ -70,7 +65,7 @@ func main() {
|
||||
y = y / common.WEI_PER_ETHER
|
||||
}
|
||||
|
||||
pf := Portfolio{}
|
||||
pf := PortfolioTemp{}
|
||||
pf.Balance = y
|
||||
pf.Subtotal = 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user