Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return theoretical target stake when in auction #3705

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 21 additions & 24 deletions execution/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type LiquidityMonitor interface {
type TargetStakeCalculator interface {
RecordOpenInterest(oi uint64, now time.Time) error
GetTargetStake(rf types.RiskFactor, now time.Time, markPrice *num.Uint) *num.Uint
GetTheoreticalTargetStake(rf types.RiskFactor, now time.Time, markPrice *num.Uint, trades []*types.Trade) *num.Uint
UpdateScalingFactor(sFactor num.Decimal) error
UpdateTimeWindow(tWindow time.Duration)
}
Expand Down Expand Up @@ -264,12 +265,12 @@ func NewMarket(
riskConfig,
tradableInstrument.MarginCalculator,
tradableInstrument.RiskModel,
getInitialFactors(log, mkt, asset),
book,
as,
broker,
now.UnixNano(),
mkt.GetId(),
asset,
)
settleEngine := settlement.New(
log,
Expand Down Expand Up @@ -406,6 +407,13 @@ func (m *Market) GetMarketData() types.MarketData {
staticMidPrice = staticMidPrice.Div(num.Sum(bestStaticBidPrice, bestStaticOfferPrice), two)
}

var targetStake string
if m.as.InAuction() {
targetStake = m.getTheoreticalTargetStake().String()
} else {
targetStake = m.getTargetStake().String()
}

return types.MarketData{
Market: m.GetID(),
BestBidPrice: bestBidPrice,
Expand All @@ -428,7 +436,7 @@ func (m *Market) GetMarketData() types.MarketData {
MarketTradingMode: m.as.Mode(),
Trigger: m.as.Trigger(),
ExtensionTrigger: m.as.ExtensionTrigger(),
TargetStake: m.getTargetStake().String(),
TargetStake: targetStake,
SuppliedStake: m.getSuppliedStake().String(),
PriceMonitoringBounds: m.pMonitor.GetCurrentBounds(),
MarketValueProxy: m.lastMarketValueProxy.String(),
Expand Down Expand Up @@ -2728,28 +2736,6 @@ func (m *Market) getOrderByID(orderID string) (*types.Order, bool, error) {
return nil, false, ErrOrderNotFound
}

// create an actual risk model, and calculate the risk factors
// if something goes wrong, return the hard-coded values of old
func getInitialFactors(log *logging.Logger, mkt *types.Market, asset string) *types.RiskResult {
rm, err := risk.NewModel(mkt.TradableInstrument.RiskModel, asset)
// @TODO log this error
if err != nil {
return nil
}
if ok, fact := rm.CalculateRiskFactors(nil); ok {
return fact
}
// default to hard-coded risk factors
return &types.RiskResult{
RiskFactors: map[string]*types.RiskFactor{
asset: {Long: num.DecimalFromFloat(0.15), Short: num.DecimalFromFloat(0.25)},
},
PredictedNextRiskFactors: map[string]*types.RiskFactor{
asset: {Long: num.DecimalFromFloat(0.15), Short: num.DecimalFromFloat(0.25)},
},
}
}

func (m *Market) getRiskFactors() (*types.RiskFactor, error) {
a, err := m.mkt.GetAsset()
if err != nil {
Expand All @@ -2762,6 +2748,17 @@ func (m *Market) getRiskFactors() (*types.RiskFactor, error) {
return rf, nil
}

func (m *Market) getTheoreticalTargetStake() *num.Uint {
rf, err := m.getRiskFactors()
if err != nil {
logging.Error(err)
m.log.Debug("unable to get risk factors, can't calculate target")
return num.Zero()
}
return m.tsCalc.GetTheoreticalTargetStake(
*rf, m.currentTime, m.getCurrentMarkPrice(), nil)
}

func (m *Market) getTargetStake() *num.Uint {
rf, err := m.getRiskFactors()
if err != nil {
Expand Down
21 changes: 19 additions & 2 deletions risk/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Engine struct {

currTime int64
mktID string
asset string
}

// NewEngine instantiate a new risk engine
Expand All @@ -68,12 +69,12 @@ func NewEngine(
config Config,
marginCalculator *types.MarginCalculator,
model Model,
initialFactors *types.RiskResult,
ob Orderbook,
as AuctionState,
broker Broker,
initialTime int64,
mktID string,
asset string,
) *Engine {
// setup logger
log = log.Named(namedLogger)
Expand All @@ -85,14 +86,15 @@ func NewEngine(
Config: config,
marginCalculator: marginCalculator,
scalingFactorsUint: sfUint,
factors: initialFactors,
factors: getInitialFactors(model, asset),
model: model,
waiting: false,
ob: ob,
as: as,
broker: broker,
currTime: initialTime,
mktID: mktID,
asset: asset,
}
}

Expand Down Expand Up @@ -447,3 +449,18 @@ func (m marginChange) Transfer() *types.Transfer {
func (m marginChange) MarginLevels() *types.MarginLevels {
return m.margins
}

func getInitialFactors(rm Model, asset string) *types.RiskResult {
if ok, fact := rm.CalculateRiskFactors(nil); ok {
return fact
}
// default to hard-coded risk factors
return &types.RiskResult{
RiskFactors: map[string]*types.RiskFactor{
asset: {Long: num.DecimalFromFloat(0.15), Short: num.DecimalFromFloat(0.25)},
},
PredictedNextRiskFactors: map[string]*types.RiskFactor{
asset: {Long: num.DecimalFromFloat(0.15), Short: num.DecimalFromFloat(0.25)},
},
}
}
12 changes: 9 additions & 3 deletions risk/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ func testMarginWithOrderInBook(t *testing.T) {
assert.Nil(t, err)
}

model.EXPECT().CalculateRiskFactors(gomock.Any()).Return(true, r).Times(1)
as.EXPECT().InAuction().AnyTimes().Return(false)
testE := risk.NewEngine(log, conf.Risk, mc, model, r, book, as, broker, 0, "mktid")
testE := risk.NewEngine(log, conf.Risk, mc, model, book, as, broker, 0, "mktid", "ETH")
evt := testMargin{
party: "tx",
size: 10,
Expand Down Expand Up @@ -403,6 +404,7 @@ func testMarginWithOrderInBook2(t *testing.T) {
},
},
}
_ = r
// custom scaling factor
mc := &types.MarginCalculator{
ScalingFactors: &types.ScalingFactors{
Expand Down Expand Up @@ -440,6 +442,8 @@ func testMarginWithOrderInBook2(t *testing.T) {
as := mocks.NewMockAuctionState(ctrl)
broker.EXPECT().Send(gomock.Any()).AnyTimes()

model.EXPECT().CalculateRiskFactors(gomock.Any()).Return(true, r).Times(1)

as.EXPECT().InAuction().AnyTimes().Return(false)
// instantiate the book then fill it with the orders

Expand All @@ -463,7 +467,7 @@ func testMarginWithOrderInBook2(t *testing.T) {
assert.Nil(t, err)
}

testE := risk.NewEngine(log, conf.Risk, mc, model, r, book, as, broker, 0, "mktid")
testE := risk.NewEngine(log, conf.Risk, mc, model, book, as, broker, 0, "mktid", "ETH")
evt := testMargin{
party: "tx",
size: 13,
Expand Down Expand Up @@ -507,17 +511,19 @@ func getTestEngine(t *testing.T, initialRisk *types.RiskResult) *testEngine {
broker := bmock.NewMockBroker(ctrl)
as := mocks.NewMockAuctionState(ctrl)

model.EXPECT().CalculateRiskFactors(gomock.Any()).Return(true, initialRisk).Times(1)

engine := risk.NewEngine(
logging.NewTestLogger(),
conf,
getMarginCalculator(),
model,
initialRisk,
ob,
as,
broker,
0,
"mktid",
"ETH",
)
return &testEngine{
Engine: engine,
Expand Down