-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunding_perp_spread_runtime.go
More file actions
164 lines (151 loc) · 4.37 KB
/
funding_perp_spread_runtime.go
File metadata and controls
164 lines (151 loc) · 4.37 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"fmt"
"strings"
"time"
"quantmesh/config"
"quantmesh/event"
"quantmesh/exchange"
"quantmesh/lock"
"quantmesh/logger"
"quantmesh/monitor"
"quantmesh/storage"
"quantmesh/strategy"
)
// startFundingPerpSpreadSymbolRuntime 雙永续跨所資金費差專用運行時
func startFundingPerpSpreadSymbolRuntime(
ctx context.Context,
baseCfg *config.Config,
symCfg config.SymbolConfig,
eventBus *event.EventBus,
storageService *storage.StorageService,
distributedLock lock.DistributedLock,
onRequestStop func(botID string),
) (*SymbolRuntime, error) {
fp := symCfg.FundingPerpSpread
if fp == nil {
return nil, fmt.Errorf("funding_perp_spread 缺少 funding_perp_spread 配置")
}
if err := config.ValidateFundingPerpSpread(fp); err != nil {
return nil, err
}
localCfg := *baseCfg
botID := symCfg.ID
if botID == "" {
botID = config.GenerateBotIDFundingPerpSpread(fp)
}
localCfg.Trading.BotID = botID
ctx = logger.WithBotID(ctx, botID)
localCfg.Trading.Symbol = symCfg.Symbol
localCfg.Trading.MarketType = config.MarketTypeFundingPerpSpread
mergeFundingPerpSpreadStrategyConfig(&localCfg, symCfg)
legAEx, err := exchange.NewExchange(&localCfg, strings.TrimSpace(fp.LegA.Exchange), strings.TrimSpace(fp.LegA.Symbol), "futures")
if err != nil {
return nil, fmt.Errorf("創建 leg_a 合約連線失敗: %w", err)
}
legBEx, err := exchange.NewExchange(&localCfg, strings.TrimSpace(fp.LegB.Exchange), strings.TrimSpace(fp.LegB.Symbol), "futures")
if err != nil {
return nil, fmt.Errorf("創建 leg_b 合約連線失敗: %w", err)
}
// 價格監控掛在 leg_a(主顯示)
priceMonitor := monitor.NewPriceMonitor(
legAEx,
fp.LegA.Symbol,
localCfg.Timing.PriceSendInterval,
)
if err := priceMonitor.Start(); err != nil {
return nil, fmt.Errorf("價格流: %w", err)
}
pollInterval := time.Duration(localCfg.Timing.PricePollInterval) * time.Millisecond
for i := 0; i < 10; i++ {
if priceMonitor.GetLastPrice() > 0 {
break
}
time.Sleep(pollInterval)
}
if priceMonitor.GetLastPrice() <= 0 {
return nil, fmt.Errorf("無法獲取初始價格(leg_a)")
}
totalCap := symCfg.TotalAllocatedCapital
if totalCap <= 0 {
totalCap = symCfg.OrderQuantity
}
if totalCap <= 0 {
return nil, fmt.Errorf("total_allocated_capital 必須大於 0")
}
strategyManager := strategy.NewStrategyManager(&localCfg, totalCap)
if eventBus != nil {
strategyManager.SetEventBus(eventBus)
}
var stratCfg map[string]interface{}
for _, si := range symCfg.Strategies {
if si.Type == "funding_perp_spread" {
stratCfg = si.Config
break
}
}
st := strategy.NewFundingPerpSpreadStrategy("funding_perp_spread", &localCfg, symCfg, legAEx, legBEx, fp, stratCfg)
strategyManager.RegisterStrategy("funding_perp_spread", st, 1.0, 0)
if err := strategyManager.StartAll(); err != nil {
return nil, err
}
accountID := ""
if fp != nil {
if exCfg, ok := baseCfg.Exchanges[strings.TrimSpace(fp.LegA.Exchange)]; ok && len(exCfg.APIKey) > 0 {
if len(exCfg.APIKey) > 8 {
accountID = exCfg.APIKey[:8]
} else {
accountID = exCfg.APIKey
}
}
}
rt := &SymbolRuntime{
Config: symCfg,
Exchange: legAEx,
PriceMonitor: priceMonitor,
StrategyManager: strategyManager,
EventBus: eventBus,
StorageService: storageService,
AccountID: accountID,
SuperPositionManager: nil,
ExchangeExecutor: nil,
ExecutorAdapter: nil,
ExchangeAdapter: nil,
}
rt.Stop = func() {
logger.InfoCtx(ctx, "⏹️ [%s] 停止雙永续跨所資金費運行時", botID)
if strategyManager != nil {
strategyManager.StopAll()
}
if priceMonitor != nil {
priceMonitor.Stop()
}
legAEx.StopOrderStream()
legBEx.StopOrderStream()
}
return rt, nil
}
func mergeFundingPerpSpreadStrategyConfig(localCfg *config.Config, symCfg config.SymbolConfig) {
localCfg.Strategies.Enabled = true
if localCfg.Strategies.Configs == nil {
localCfg.Strategies.Configs = make(map[string]config.StrategyConfig)
}
var cfg map[string]interface{}
weight := 1.0
for _, si := range symCfg.Strategies {
if si.Type == "funding_perp_spread" {
cfg = si.Config
if si.Weight > 0 {
weight = si.Weight
}
break
}
}
localCfg.Strategies.Configs["funding_perp_spread"] = config.StrategyConfig{
Enabled: true,
Type: "funding_perp_spread",
Weight: weight,
Config: cfg,
}
}