-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples_readme_test.go
More file actions
152 lines (135 loc) · 4.39 KB
/
examples_readme_test.go
File metadata and controls
152 lines (135 loc) · 4.39 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
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.
package openpit
import (
"testing"
"go.openpit.dev/openpit/model"
"go.openpit.dev/openpit/param"
"go.openpit.dev/openpit/pkg/optional"
"go.openpit.dev/openpit/pretrade/policies"
)
// TestReadmeQuickstart mirrors the Usage example in bindings/go/README.md.
// Keep both in sync.
func TestReadmeQuickstart(t *testing.T) {
usd, err := param.NewAsset("USD")
if err != nil {
t.Fatalf("NewAsset(USD) error = %v", err)
}
lowerBound, err := param.NewPnlFromString("-1000")
if err != nil {
t.Fatalf("NewPnlFromString(-1000) error = %v", err)
}
maxQty, err := param.NewQuantityFromString("500")
if err != nil {
t.Fatalf("NewQuantityFromString() error = %v", err)
}
maxNotional, err := param.NewVolumeFromString("100000")
if err != nil {
t.Fatalf("NewVolumeFromString() error = %v", err)
}
// 1. Configure policies.
pnlPolicy, err := policies.NewPnlBoundsKillSwitchPolicy(policies.PnlBoundsBarrier{
SettlementAsset: usd,
LowerBound: optional.Some(lowerBound),
InitialPnl: param.PnlZero,
})
if err != nil {
t.Fatalf("NewPnlBoundsKillSwitchPolicy() error = %v", err)
}
defer pnlPolicy.Close()
sizePolicy, err := policies.NewOrderSizeLimitPolicy(policies.OrderSizeLimit{
SettlementAsset: usd,
MaxQuantity: maxQty,
MaxNotional: maxNotional,
})
if err != nil {
t.Fatalf("NewOrderSizeLimitPolicy() error = %v", err)
}
defer sizePolicy.Close()
// 2. Build the engine (one time at the platform initialization).
builder, err := NewEngineBuilder()
if err != nil {
t.Fatalf("NewEngineBuilder() error = %v", err)
}
builder.BuiltinCheckPreTradeStartPolicy(
policies.NewOrderValidation(),
pnlPolicy,
policies.NewRateLimitPolicy(100, 1),
sizePolicy,
)
engine, err := builder.Build()
if err != nil {
t.Fatalf("Build() error = %v", err)
}
defer engine.Stop()
// 3. Check an order.
order := model.NewOrder()
op := order.EnsureOperationView()
aapl, err := param.NewAsset("AAPL")
if err != nil {
t.Fatalf("NewAsset(AAPL) error = %v", err)
}
op.SetInstrument(param.NewInstrument(aapl, usd))
op.SetAccountID(param.NewAccountIDFromInt(99224416))
op.SetSide(param.SideBuy)
price, _ := param.NewPriceFromString("185")
qty, _ := param.NewQuantityFromString("100")
op.SetTradeAmount(param.NewQuantityTradeAmount(qty))
op.SetPrice(price)
request, rejects, err := engine.StartPreTrade(order)
if err != nil {
t.Fatalf("StartPreTrade() error = %v", err)
}
if rejects != nil {
t.Fatalf("StartPreTrade() unexpected rejects: %v", rejects)
}
defer request.Close()
// 5. Real pre-trade and risk control.
reservation, rejects, err := request.Execute()
if err != nil {
t.Fatalf("Execute() error = %v", err)
}
if rejects != nil {
t.Fatalf("Execute() unexpected rejects: %v", rejects)
}
defer reservation.Close()
// Optional shortcut for the same two-stage flow:
// reservation, rejects, err := engine.ExecutePreTrade(order)
// 6. Commit the reservation.
reservation.Commit()
// 7. Apply execution report.
report := model.NewExecutionReport()
reportOp := model.NewExecutionReportOperation()
reportOp.SetInstrument(param.NewInstrument(aapl, usd))
reportOp.SetAccountID(param.NewAccountIDFromInt(99224416))
reportOp.SetSide(param.SideBuy)
report.SetOperation(reportOp)
pnl, _ := param.NewPnlFromString("-50")
fee, _ := param.NewFeeFromString("3.4")
impact := model.NewExecutionReportFinancialImpact()
impact.SetPnl(pnl)
impact.SetFee(fee)
report.SetFinancialImpact(impact)
result, err := engine.ApplyExecutionReport(report)
if err != nil {
t.Fatalf("ApplyExecutionReport() error = %v", err)
}
// 8. Kill switch must not be triggered after a small loss.
if result.KillSwitchTriggered {
t.Fatal("KillSwitchTriggered = true, want false")
}
}