all: use order executor extended interface to mock the risk tests

This commit is contained in:
c9s 2023-07-04 22:07:31 +08:00
parent f1828beac8
commit ff727ae495
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 126 additions and 5 deletions

View File

@ -0,0 +1,104 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/c9s/bbgo/pkg/bbgo (interfaces: OrderExecutorExtended)
// Package mocks is a generated GoMock package.
package mocks
import (
context "context"
reflect "reflect"
bbgo "github.com/c9s/bbgo/pkg/bbgo"
types "github.com/c9s/bbgo/pkg/types"
gomock "github.com/golang/mock/gomock"
)
// MockOrderExecutorExtended is a mock of OrderExecutorExtended interface.
type MockOrderExecutorExtended struct {
ctrl *gomock.Controller
recorder *MockOrderExecutorExtendedMockRecorder
}
// MockOrderExecutorExtendedMockRecorder is the mock recorder for MockOrderExecutorExtended.
type MockOrderExecutorExtendedMockRecorder struct {
mock *MockOrderExecutorExtended
}
// NewMockOrderExecutorExtended creates a new mock instance.
func NewMockOrderExecutorExtended(ctrl *gomock.Controller) *MockOrderExecutorExtended {
mock := &MockOrderExecutorExtended{ctrl: ctrl}
mock.recorder = &MockOrderExecutorExtendedMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockOrderExecutorExtended) EXPECT() *MockOrderExecutorExtendedMockRecorder {
return m.recorder
}
// CancelOrders mocks base method.
func (m *MockOrderExecutorExtended) CancelOrders(arg0 context.Context, arg1 ...types.Order) error {
m.ctrl.T.Helper()
varargs := []interface{}{arg0}
for _, a := range arg1 {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "CancelOrders", varargs...)
ret0, _ := ret[0].(error)
return ret0
}
// CancelOrders indicates an expected call of CancelOrders.
func (mr *MockOrderExecutorExtendedMockRecorder) CancelOrders(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]interface{}{arg0}, arg1...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelOrders", reflect.TypeOf((*MockOrderExecutorExtended)(nil).CancelOrders), varargs...)
}
// Position mocks base method.
func (m *MockOrderExecutorExtended) Position() *types.Position {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Position")
ret0, _ := ret[0].(*types.Position)
return ret0
}
// Position indicates an expected call of Position.
func (mr *MockOrderExecutorExtendedMockRecorder) Position() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Position", reflect.TypeOf((*MockOrderExecutorExtended)(nil).Position))
}
// SubmitOrders mocks base method.
func (m *MockOrderExecutorExtended) SubmitOrders(arg0 context.Context, arg1 ...types.SubmitOrder) (types.OrderSlice, error) {
m.ctrl.T.Helper()
varargs := []interface{}{arg0}
for _, a := range arg1 {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "SubmitOrders", varargs...)
ret0, _ := ret[0].(types.OrderSlice)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// SubmitOrders indicates an expected call of SubmitOrders.
func (mr *MockOrderExecutorExtendedMockRecorder) SubmitOrders(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]interface{}{arg0}, arg1...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitOrders", reflect.TypeOf((*MockOrderExecutorExtended)(nil).SubmitOrders), varargs...)
}
// TradeCollector mocks base method.
func (m *MockOrderExecutorExtended) TradeCollector() *bbgo.TradeCollector {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "TradeCollector")
ret0, _ := ret[0].(*bbgo.TradeCollector)
return ret0
}
// TradeCollector indicates an expected call of TradeCollector.
func (mr *MockOrderExecutorExtendedMockRecorder) TradeCollector() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TradeCollector", reflect.TypeOf((*MockOrderExecutorExtended)(nil).TradeCollector))
}

View File

@ -28,6 +28,14 @@ type OrderExecutor interface {
CancelOrders(ctx context.Context, orders ...types.Order) error
}
//go:generate mockgen -destination=mocks/mock_order_executor_extended.go -package=mocks . OrderExecutorExtended
type OrderExecutorExtended interface {
SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)
CancelOrders(ctx context.Context, orders ...types.Order) error
TradeCollector() *TradeCollector
Position() *types.Position
}
type OrderExecutionRouter interface {
// SubmitOrdersTo submit order to a specific exchange Session
SubmitOrdersTo(ctx context.Context, session string, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)

View File

@ -15,7 +15,7 @@ import (
//
//go:generate callbackgen -type PositionRiskControl
type PositionRiskControl struct {
orderExecutor *bbgo.GeneralOrderExecutor
orderExecutor bbgo.OrderExecutorExtended
// hardLimit is the maximum base position you can hold
hardLimit fixedpoint.Value
@ -27,7 +27,7 @@ type PositionRiskControl struct {
releasePositionCallbacks []func(quantity fixedpoint.Value, side types.SideType)
}
func NewPositionRiskControl(orderExecutor *bbgo.GeneralOrderExecutor, hardLimit, quantity fixedpoint.Value) *PositionRiskControl {
func NewPositionRiskControl(orderExecutor bbgo.OrderExecutorExtended, hardLimit, quantity fixedpoint.Value) *PositionRiskControl {
control := &PositionRiskControl{
orderExecutor: orderExecutor,
hardLimit: hardLimit,

View File

@ -3,9 +3,11 @@ package riskcontrol
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/bbgo/mocks"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -58,7 +60,7 @@ func TestReleasePositionCallbacks(t *testing.T) {
resultPosition fixedpoint.Value
}{
{
name: "PostivePositionWithinLimit",
name: "PositivePositionWithinLimit",
position: fixedpoint.NewFromInt(8),
resultPosition: fixedpoint.NewFromInt(8),
},
@ -68,7 +70,7 @@ func TestReleasePositionCallbacks(t *testing.T) {
resultPosition: fixedpoint.NewFromInt(-8),
},
{
name: "PostivePositionOverLimit",
name: "PositivePositionOverLimit",
position: fixedpoint.NewFromInt(11),
resultPosition: fixedpoint.NewFromInt(10),
},
@ -91,7 +93,14 @@ func TestReleasePositionCallbacks(t *testing.T) {
},
}
orderExecutor := bbgo.NewGeneralOrderExecutor(nil, "BTCUSDT", "strategy", "strategy-1", pos)
tradeCollector := &bbgo.TradeCollector{}
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
orderExecutor := mocks.NewMockOrderExecutorExtended(mockCtrl)
orderExecutor.EXPECT().TradeCollector().Return(tradeCollector).AnyTimes()
orderExecutor.EXPECT().Position().Return(pos).AnyTimes()
orderExecutor.EXPECT().SubmitOrders(gomock.Any(), gomock.Any()).AnyTimes()
riskControl := NewPositionRiskControl(orderExecutor, fixedpoint.NewFromInt(10), fixedpoint.NewFromInt(2))
riskControl.OnReleasePosition(func(quantity fixedpoint.Value, side types.SideType) {
if side == types.SideTypeBuy {