Skip to content

Commit c5ba08a

Browse files
committed
Centralize account-related under the accounts in YAML
1 parent c51f260 commit c5ba08a

File tree

18 files changed

+296
-170
lines changed

18 files changed

+296
-170
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ docker run -d --name timescaledb -p 127.0.0.1:5432:5432 \
4040
#### 2. start banbot
4141
create your `/root/config.yaml`:
4242
```yaml
43-
exchange:
44-
binance:
45-
account_prods:
46-
user1: # you can change this
43+
accounts:
44+
user1: # you can change this
45+
binance:
46+
prod:
4747
api_key: your_api_key_here
4848
api_secret: your_secret_here
4949
#database:

biz/config.local.yml

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ run_policy:
55
run_timeframes: [15m]
66
# pairs: [BTC/USDT:USDT]
77
params: {atr: 15}
8+
accounts:
9+
user1:
10+
binance:
11+
prod:
12+
api_key: vvv
13+
api_secret: vvv

biz/config.yml

+5-20
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,11 @@ pairlists:
7979
exchange:
8080
name: binance
8181
binance:
82-
# account_prods:
83-
# user1:
84-
# api_key: xxx
85-
# api_secret: bbb
86-
# max_stake_amt: 1000
87-
# stake_rate: 1
88-
# leverage: 0
89-
# user2:
90-
# api_key: xxx
91-
# api_secret: bbb
92-
account_tests:
93-
default:
94-
api_key: xxx
95-
api_secret: bbb
96-
options:
97-
# proxy: http://127.0.0.1:10808
98-
fees:
99-
linear:
100-
taker: 0.0005
101-
maker: 0.0002
82+
# proxy: http://127.0.0.1:10808
83+
fees:
84+
linear:
85+
taker: 0.0005
86+
maker: 0.0002
10287
database:
10388
retention: all
10489
max_pool_size: 50

biz/odmgr.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ func (o *OrderMgr) allowOrderEnter(env *banta.BarEnv, enters []*strat.EnterReq)
149149
}
150150
openOds, lock := ormo.GetOpenODs(o.Account)
151151
lock.Lock()
152-
enters = checkOrderNum(enters, len(openOds), config.MaxOpenOrders, "max_open_orders")
152+
maxOpenNum := config.MaxOpenOrders
153+
acc, _ := config.Accounts[o.Account]
154+
if acc != nil && acc.MaxOpenOrders > 0 {
155+
maxOpenNum = acc.MaxOpenOrders
156+
}
157+
enters = checkOrderNum(enters, len(openOds), maxOpenNum, "max_open_orders")
153158
if len(enters) > 0 && config.MaxSimulOpen > 0 {
154159
enters = checkOrderNum(enters, o.simulOpen, config.MaxSimulOpen, "max_simul_open")
155160
}

config/biz.go

+51-56
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,14 @@ func ApplyConfig(args *CmdArgs, c *Config) *errs.Error {
273273
}
274274
PairMgr = c.PairMgr
275275
PairFilters = c.PairFilters
276+
Accounts = c.Accounts
276277
Exchange = c.Exchange
278+
if Exchange == nil {
279+
Exchange = &ExchangeConfig{
280+
Name: "binance",
281+
Items: make(map[string]map[string]interface{}),
282+
}
283+
}
277284
err := initExgAccs()
278285
if err != nil {
279286
return err
@@ -325,15 +332,6 @@ func initPolicies(policyList []*RunPolicyConfig) (bool, []string) {
325332
return staticPairs, polPairs
326333
}
327334

328-
func GetExgConfig() *ExgItemConfig {
329-
if Exchange != nil {
330-
if cfg, ok := Exchange.Items[Exchange.Name]; ok {
331-
return cfg
332-
}
333-
}
334-
return &ExgItemConfig{}
335-
}
336-
337335
func GetTakeOverTF(pair, defTF string) string {
338336
if pairMap, ok := core.StgPairTfs[TakeOverStrat]; ok {
339337
if tf, ok := pairMap[pair]; ok {
@@ -358,13 +356,7 @@ func initExgAccs() *errs.Error {
358356
} else {
359357
btime.LocShow = btime.UTCLocale
360358
}
361-
exgCfg := GetExgConfig()
362-
var accs map[string]*AccountConfig
363-
if core.RunEnv != core.RunEnvTest {
364-
accs = exgCfg.AccountProds
365-
} else {
366-
accs = exgCfg.AccountTests
367-
}
359+
var accs = Accounts
368360
Accounts = make(map[string]*AccountConfig)
369361
BakAccounts = make(map[string]*AccountConfig)
370362
if core.EnvReal {
@@ -432,18 +424,18 @@ func GetStakeAmount(accName string) float64 {
432424
} else {
433425
amount = StakeAmount
434426
}
427+
// Multiply by the account multiplier
428+
// 乘以账户倍率
429+
if ok && acc.StakeRate > 0 {
430+
amount *= acc.StakeRate
431+
}
435432
// Check if the maximum amount is exceeded
436433
// 检查是否超出最大金额
437434
if ok && acc.MaxStakeAmt > 0 && acc.MaxStakeAmt < amount {
438435
amount = acc.MaxStakeAmt
439436
} else if MaxStakeAmt > 0 && MaxStakeAmt < amount {
440437
amount = MaxStakeAmt
441438
}
442-
// Multiply by the account multiplier
443-
// 乘以账户倍率
444-
if ok && acc.StakeRate > 0 {
445-
amount *= acc.StakeRate
446-
}
447439
return amount
448440
}
449441

@@ -571,6 +563,8 @@ func (c *Config) Clone() *Config {
571563
PairFilters: c.PairFilters,
572564
SpiderAddr: c.SpiderAddr,
573565
Webhook: c.Webhook,
566+
Accounts: c.Accounts,
567+
Exchange: c.Exchange,
574568
}
575569
}
576570

@@ -586,6 +580,13 @@ api_server.users[*].pwd
586580
func (c *Config) Desensitize() *Config {
587581
var res = c.Clone()
588582

583+
if res.Accounts != nil {
584+
for _, acc := range res.Accounts {
585+
acc.Exchanges = nil
586+
acc.APIServer = nil
587+
}
588+
}
589+
589590
// 处理数据库配置
590591
if c.Database != nil {
591592
res.Database = &DatabaseConfig{
@@ -596,34 +597,6 @@ func (c *Config) Desensitize() *Config {
596597
}
597598
}
598599

599-
// 处理交易所配置
600-
if c.Exchange != nil {
601-
res.Exchange = &ExchangeConfig{
602-
Name: c.Exchange.Name,
603-
Items: make(map[string]*ExgItemConfig),
604-
}
605-
for exgName, exgItem := range c.Exchange.Items {
606-
resItem := &ExgItemConfig{
607-
Options: exgItem.Options,
608-
}
609-
// 处理生产账户
610-
if exgItem.AccountProds != nil {
611-
resItem.AccountProds = make(map[string]*AccountConfig)
612-
for accName, acc := range exgItem.AccountProds {
613-
resItem.AccountProds[accName] = acc.Desensitize()
614-
}
615-
}
616-
// 处理测试账户
617-
if exgItem.AccountTests != nil {
618-
resItem.AccountTests = make(map[string]*AccountConfig)
619-
for accName, acc := range exgItem.AccountTests {
620-
resItem.AccountTests[accName] = acc.Desensitize()
621-
}
622-
}
623-
res.Exchange.Items[exgName] = resItem
624-
}
625-
}
626-
627600
// 处理RPC通道配置
628601
if c.RPCChannels != nil {
629602
res.RPCChannels = make(map[string]map[string]interface{})
@@ -855,14 +828,19 @@ func (c *RunPolicyConfig) PairDup(pair string) (*RunPolicyConfig, bool) {
855828
return res, isDiff
856829
}
857830

858-
func (a *AccountConfig) Desensitize() *AccountConfig {
859-
return &AccountConfig{
860-
NoTrade: a.NoTrade,
861-
MaxStakeAmt: a.MaxStakeAmt,
862-
StakeRate: a.StakeRate,
863-
StakePctAmt: a.StakePctAmt,
864-
Leverage: a.Leverage,
831+
func (a *AccountConfig) GetApiSecret() *ApiSecretConfig {
832+
if a == nil || len(a.Exchanges) == 0 {
833+
return &ApiSecretConfig{}
865834
}
835+
cfg, _ := a.Exchanges[Exchange.Name]
836+
if cfg != nil {
837+
if core.RunEnv != core.RunEnvTest && cfg.Prod != nil {
838+
return cfg.Prod
839+
} else if core.RunEnv == core.RunEnvTest && cfg.Test != nil {
840+
return cfg.Test
841+
}
842+
}
843+
return &ApiSecretConfig{}
866844
}
867845

868846
func LoadPerfs(inDir string) {
@@ -950,3 +928,20 @@ func (tr *TimeTuple) Clone() *TimeTuple {
950928
EndMS: tr.EndMS,
951929
}
952930
}
931+
932+
func GetApiUsers() []*UserConfig {
933+
res := make([]*UserConfig, 0)
934+
for name, acc := range Accounts {
935+
if acc.APIServer == nil {
936+
continue
937+
}
938+
res = append(res, &UserConfig{
939+
Username: name,
940+
Password: acc.APIServer.Pwd,
941+
AccRoles: map[string]string{
942+
name: acc.APIServer.Role,
943+
},
944+
})
945+
}
946+
return append(res, APIServer.Users...)
947+
}

config/types.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type Config struct {
115115
Pairs []string `yaml:"pairs,omitempty,flow" mapstructure:"pairs"`
116116
PairMgr *PairMgrConfig `yaml:"pairmgr,omitempty" mapstructure:"pairmgr"`
117117
PairFilters []*CommonPairFilter `yaml:"pairlists,omitempty" mapstructure:"pairlists"`
118+
Accounts map[string]*AccountConfig `yaml:"accounts,omitempty" mapstructure:"accounts,omitempty"`
118119
Exchange *ExchangeConfig `yaml:"exchange,omitempty" mapstructure:"exchange"`
119120
Database *DatabaseConfig `yaml:"database,omitempty" mapstructure:"database"`
120121
SpiderAddr string `yaml:"spider_addr,omitempty" mapstructure:"spider_addr"`
@@ -219,26 +220,37 @@ type CommonPairFilter struct {
219220

220221
// ExchangeConfig Represents the configuration information of the exchange 表示交易所的配置信息
221222
type ExchangeConfig struct {
222-
Name string `yaml:"name" mapstructure:"name"`
223-
Items map[string]*ExgItemConfig `yaml:",inline" mapstructure:",remain"`
224-
}
225-
226-
// Configuration of specific exchanges 具体交易所的配置
227-
type ExgItemConfig struct {
228-
AccountProds map[string]*AccountConfig `yaml:"account_prods,omitempty" mapstructure:"account_prods,omitempty"`
229-
AccountTests map[string]*AccountConfig `yaml:"account_tests,omitempty" mapstructure:"account_tests,omitempty"`
230-
Options map[string]interface{} `yaml:"options,omitempty" mapstructure:"options,omitempty"`
223+
Name string `yaml:"name" mapstructure:"name"`
224+
Items map[string]map[string]interface{} `yaml:",inline" mapstructure:",remain"`
231225
}
232226

233227
// AccountConfig Configuration to store API keys and secrets 存储 API 密钥和秘密的配置
234228
type AccountConfig struct {
235-
APIKey string `yaml:"api_key,omitempty" mapstructure:"api_key"`
236-
APISecret string `yaml:"api_secret,omitempty" mapstructure:"api_secret"`
237-
NoTrade bool `yaml:"no_trade,omitempty" mapstructure:"no_trade"`
238-
MaxStakeAmt float64 `yaml:"max_stake_amt,omitempty" mapstructure:"max_stake_amt"` // Maximum amount allowed for a single transaction 允许的单笔最大金额
239-
StakeRate float64 `yaml:"stake_rate,omitempty" mapstructure:"stake_rate"` // Multiple of billing amount relative to benchmark 相对基准的开单金额倍数
240-
StakePctAmt float64 `yaml:"-"` // The amount currently allowed when billing by percentage按百分比开单时,当前允许的金额
241-
Leverage float64 `yaml:"leverage,omitempty" mapstructure:"leverage"`
229+
NoTrade bool `yaml:"no_trade,omitempty" mapstructure:"no_trade"`
230+
StakeRate float64 `yaml:"stake_rate,omitempty" mapstructure:"stake_rate"` // Multiple of billing amount relative to benchmark 相对基准的开单金额倍数
231+
StakePctAmt float64 `yaml:"-"` // The amount currently allowed when billing by percentage按百分比开单时,当前允许的金额
232+
MaxStakeAmt float64 `yaml:"max_stake_amt,omitempty" mapstructure:"max_stake_amt"` // Maximum amount allowed for a single transaction 允许的单笔最大金额
233+
Leverage float64 `yaml:"leverage,omitempty" mapstructure:"leverage"`
234+
MaxPair int `yaml:"max_pair,omitempty" mapstructure:"max_pair"`
235+
MaxOpenOrders int `yaml:"max_open_orders,omitempty" mapstructure:"max_open_orders"`
236+
RPCChannels []map[string]interface{} `yaml:"rpc_channels,omitempty" mapstructure:"rpc_channels"`
237+
APIServer *AccPwdRole `yaml:"api_server,omitempty" mapstructure:"api_server"`
238+
Exchanges map[string]*ExgApiSecrets `yaml:",inline" mapstructure:",remain"`
239+
}
240+
241+
type ExgApiSecrets struct {
242+
Prod *ApiSecretConfig `yaml:"prod,omitempty" mapstructure:"prod"`
243+
Test *ApiSecretConfig `yaml:"test,omitempty" mapstructure:"test"`
244+
}
245+
246+
type ApiSecretConfig struct {
247+
APIKey string `yaml:"api_key,omitempty" mapstructure:"api_key"`
248+
APISecret string `yaml:"api_secret,omitempty" mapstructure:"api_secret"`
249+
}
250+
251+
type AccPwdRole struct {
252+
Pwd string `yaml:"pwd,omitempty" mapstructure:"pwd"`
253+
Role string `yaml:"role,omitempty" mapstructure:"role"`
242254
}
243255

244256
type TimeTuple struct {

core/data.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ var (
3939
ExitCalls []func() // CALLBACK TO STOP EXECUTION 停止执行的回调
4040

4141
ConcurNum = 2 // The maximum number of K-line tasks to be downloaded at the same time. If it is too high, a 429 current limit will occur. 最大同时下载K线任务数,过大会出现429限流
42-
Version = "0.2.0"
43-
UIVersion = "0.2.0"
42+
Version = "0.2.1"
43+
UIVersion = "0.2.1"
4444
LogFile string
4545
DevDbPath string
4646
)

doc/config.yml

+27-21
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,35 @@ pairlists: # 交易对过滤器,按从上到下的顺序逐个过滤应用。
108108
limit: 30 # 最多取30个
109109
- name: ShuffleFilter # 随机打乱
110110
seed: 42 # 随机数种子,可选
111+
accounts:
112+
user1: # 这里是账户名字,可任意,会在rpc发消息时使用
113+
no_trade: false # 禁止此账户交易
114+
stake_rate: 1 # 相对默认金额开单倍率
115+
leverage: 0
116+
max_stake_amt: 0
117+
max_pair: 0
118+
max_open_orders: 0
119+
binance:
120+
prod:
121+
api_key: vvv
122+
api_secret: vvv
123+
test:
124+
api_key: vvv
125+
api_secret: vvv
126+
rpc_channels: # 发送通知或通过社交app控制
127+
- name: wx_bot
128+
to_user: ChannelUserID
129+
api_server: # 通过Dashboard访问的密码和角色
130+
pwd: abc
131+
role: admin
111132
exchange: # 交易所配置
112133
name: binance # 当前使用的交易所
113-
binance:
114-
account_prods: # 生产网络的key和secret,指定env: prod时此项必填
115-
user1: # 这里是账户名字,可任意,会在rpc发消息时使用
116-
api_key: xxx
117-
api_secret: bbb
118-
max_stake_amt: 1000 # 允许的单笔最大金额
119-
stake_rate: 1 # 开单金额倍率,相对于默认的
120-
leverage: 0 # 期货杠杆,优先级高于默认的
121-
user2: # 这里是账户名字
122-
api_key: xxx
123-
api_secret: bbb
124-
account_tests: # 测试网络的key和secret,指定env: test时此项必填
125-
default:
126-
api_key: xxx
127-
api_secret: bbb
128-
options: # 这里传入banexg初始化交易所的参数,key会自动从蛇形转为驼峰。
129-
proxy: http://127.0.0.1:10808
130-
fees:
131-
linear: # 键可以是:linear/inverse/main(spot or margin)
132-
taker: 0.0005
133-
maker: 0.0002
134+
binance: # 这里传入banexg初始化交易所的参数,key会自动从蛇形转为驼峰。
135+
# proxy: http://127.0.0.1:10808
136+
fees:
137+
linear: # 键可以是:linear/inverse/main(spot or margin)
138+
taker: 0.0005
139+
maker: 0.0002
134140
database: # 数据库配置
135141
retention: all
136142
max_pool_size: 50 # 连接池最大大小

0 commit comments

Comments
 (0)