-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03_validate_risk_before_execution.go
More file actions
61 lines (56 loc) · 1.73 KB
/
Copy path03_validate_risk_before_execution.go
File metadata and controls
61 lines (56 loc) · 1.73 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
package examples
import (
"github.com/QuantProcessing/exchanges/cache"
"github.com/QuantProcessing/exchanges/model"
"github.com/QuantProcessing/exchanges/risk"
"github.com/shopspring/decimal"
)
type RiskValidationResult struct {
Accepted model.SubmitOrder
Rejected model.SubmitOrder
RejectErr error
}
// ValidateRiskBeforeExecution demonstrates the normal execution boundary:
// put instrument metadata in cache, configure limits, then call risk.Check
// before an order is allowed to reach a venue execution client.
func ValidateRiskBeforeExecution() (RiskValidationResult, error) {
instrumentID := model.MustInstrumentID("BTC-USDT-SPOT.BINANCE")
c := cache.New()
if err := c.PutInstrument(model.Instrument{
ID: instrumentID,
RawSymbol: "BTCUSDT",
Type: model.InstrumentTypeSpot,
Base: "BTC",
Quote: "USDT",
PriceTick: decimal.RequireFromString("0.01"),
SizeTick: decimal.RequireFromString("0.001"),
Status: model.InstrumentStatusTrading,
}); err != nil {
return RiskValidationResult{}, err
}
engine := risk.NewEngine(c, risk.Config{
MaxOrderNotional: decimal.RequireFromString("100"),
ExposureCurrency: "USDT",
})
factory := model.NewOrderFactory("risk-account", model.WithClientOrderIDPrefix("risk"))
accepted := factory.Limit(
instrumentID,
model.OrderSideBuy,
decimal.RequireFromString("0.5"),
decimal.RequireFromString("100.00"),
)
rejected := factory.Limit(
instrumentID,
model.OrderSideBuy,
decimal.RequireFromString("2"),
decimal.RequireFromString("100.00"),
)
if err := engine.Check(accepted); err != nil {
return RiskValidationResult{}, err
}
return RiskValidationResult{
Accepted: accepted,
Rejected: rejected,
RejectErr: engine.Check(rejected),
}, nil
}