forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolio.go
93 lines (79 loc) · 2.03 KB
/
portfolio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package engine
import (
"errors"
"sync/atomic"
"time"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio"
)
// vars for the fund manager package
var (
PortfolioSleepDelay = time.Minute
)
type portfolioManager struct {
started int32
stopped int32
shutdown chan struct{}
}
func (p *portfolioManager) Started() bool {
return atomic.LoadInt32(&p.started) == 1
}
func (p *portfolioManager) Start() error {
if atomic.AddInt32(&p.started, 1) != 1 {
return errors.New("portfolio manager already started")
}
log.Debugln(log.PortfolioMgr, "Portfolio manager starting...")
Bot.Portfolio = &portfolio.Portfolio
Bot.Portfolio.Seed(Bot.Config.Portfolio)
p.shutdown = make(chan struct{})
portfolio.Verbose = Bot.Settings.Verbose
go p.run()
return nil
}
func (p *portfolioManager) Stop() error {
if atomic.AddInt32(&p.stopped, 1) != 1 {
return errors.New("portfolio manager is already stopped")
}
log.Debugln(log.PortfolioMgr, "Portfolio manager shutting down...")
close(p.shutdown)
return nil
}
func (p *portfolioManager) run() {
log.Debugln(log.PortfolioMgr, "Portfolio manager started.")
Bot.ServicesWG.Add(1)
tick := time.NewTicker(PortfolioSleepDelay)
defer func() {
atomic.CompareAndSwapInt32(&p.stopped, 1, 0)
atomic.CompareAndSwapInt32(&p.started, 1, 0)
tick.Stop()
Bot.ServicesWG.Done()
log.Debugf(log.PortfolioMgr, "Portfolio manager shutdown.")
}()
for {
select {
case <-p.shutdown:
return
case <-tick.C:
p.processPortfolio()
}
}
}
func (p *portfolioManager) processPortfolio() {
pf := portfolio.GetPortfolio()
data := pf.GetPortfolioGroupedCoin()
for key, value := range data {
err := pf.UpdatePortfolio(value, key)
if err != nil {
log.Errorf(log.PortfolioMgr,
"PortfolioWatcher error %s for currency %s\n",
err,
key)
continue
}
log.Debugf(log.PortfolioMgr,
"Portfolio manager: Successfully updated address balance for %s address(es) %s\n",
key,
value)
}
SeedExchangeAccountInfo(GetAllEnabledExchangeAccountInfo().Data)
}