-
-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1758 from c9s/c9s/xmaker/depth-signal
FEATURE: [xmaker] add depth ratio signal
- Loading branch information
Showing
5 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package xmaker | ||
|
||
import ( | ||
"context" | ||
"math" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/c9s/bbgo/pkg/bbgo" | ||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
var depthRatioSignalMetrics = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "xmaker_depth_ratio_signal", | ||
Help: "", | ||
}, []string{"symbol"}) | ||
|
||
func init() { | ||
prometheus.MustRegister(depthRatioSignalMetrics) | ||
} | ||
|
||
type DepthRatioSignal struct { | ||
// PriceRange, 2% depth ratio means 2% price range from the mid price | ||
PriceRange fixedpoint.Value `json:"priceRange"` | ||
MinRatio float64 `json:"minRatio"` | ||
|
||
symbol string | ||
book *types.StreamOrderBook | ||
} | ||
|
||
func (s *DepthRatioSignal) BindStreamBook(book *types.StreamOrderBook) { | ||
s.book = book | ||
} | ||
|
||
func (s *DepthRatioSignal) Bind(ctx context.Context, session *bbgo.ExchangeSession, symbol string) error { | ||
if s.book == nil { | ||
return errors.New("s.book can not be nil") | ||
} | ||
|
||
s.symbol = symbol | ||
orderBookSignalMetrics.WithLabelValues(s.symbol).Set(0.0) | ||
return nil | ||
} | ||
|
||
func (s *DepthRatioSignal) CalculateSignal(ctx context.Context) (float64, error) { | ||
bid, ask, ok := s.book.BestBidAndAsk() | ||
if !ok { | ||
return 0.0, nil | ||
} | ||
|
||
midPrice := bid.Price.Add(ask.Price).Div(fixedpoint.Two) | ||
|
||
asks := s.book.SideBook(types.SideTypeSell) | ||
bids := s.book.SideBook(types.SideTypeBuy) | ||
|
||
asksInRange := asks.InPriceRange(midPrice, types.SideTypeSell, s.PriceRange) | ||
bidsInRange := bids.InPriceRange(midPrice, types.SideTypeBuy, s.PriceRange) | ||
|
||
askDepthQuote := asksInRange.SumDepthInQuote() | ||
bidDepthQuote := bidsInRange.SumDepthInQuote() | ||
|
||
var signal = 0.0 | ||
|
||
depthRatio := bidDepthQuote.Div(askDepthQuote.Add(bidDepthQuote)) | ||
|
||
// convert ratio into -2.0 and 2.0 | ||
signal = depthRatio.Sub(fixedpoint.NewFromFloat(0.5)).Float64() * 4.0 | ||
|
||
// ignore noise | ||
if math.Abs(signal) < s.MinRatio { | ||
signal = 0.0 | ||
} | ||
|
||
log.Infof("[DepthRatioSignal] %f bid/ask = %f/%f", signal, bidDepthQuote.Float64(), askDepthQuote.Float64()) | ||
depthRatioSignalMetrics.WithLabelValues(s.symbol).Set(signal) | ||
return signal, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package xmaker | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
|
||
. "github.com/c9s/bbgo/pkg/testing/testhelper" | ||
) | ||
|
||
func TestDepthRatioSignal_CalculateSignal(t *testing.T) { | ||
type fields struct { | ||
PriceRange fixedpoint.Value | ||
MinRatio float64 | ||
symbol string | ||
book *types.StreamOrderBook | ||
} | ||
type args struct { | ||
ctx context.Context | ||
bids, asks types.PriceVolumeSlice | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want float64 | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "medium short", | ||
fields: fields{ | ||
PriceRange: fixedpoint.NewFromFloat(0.02), | ||
MinRatio: 0.01, | ||
symbol: "BTCUSDT", | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
asks: PriceVolumeSliceFromText(` | ||
19310,1.0 | ||
19320,0.2 | ||
19330,0.3 | ||
19340,0.4 | ||
19350,0.5 | ||
`), | ||
bids: PriceVolumeSliceFromText(` | ||
19300,0.1 | ||
19290,0.2 | ||
19280,0.3 | ||
19270,0.4 | ||
19260,0.5 | ||
`), | ||
}, | ||
want: -0.4641, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "strong short", | ||
fields: fields{ | ||
PriceRange: fixedpoint.NewFromFloat(0.02), | ||
MinRatio: 0.01, | ||
symbol: "BTCUSDT", | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
asks: PriceVolumeSliceFromText(` | ||
19310,10.0 | ||
19320,0.2 | ||
19330,0.3 | ||
19340,0.4 | ||
19350,0.5 | ||
`), | ||
bids: PriceVolumeSliceFromText(` | ||
19300,0.1 | ||
19290,0.1 | ||
19280,0.1 | ||
19270,0.1 | ||
19260,0.1 | ||
`), | ||
}, | ||
want: -1.8322, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "strong long", | ||
fields: fields{ | ||
PriceRange: fixedpoint.NewFromFloat(0.02), | ||
MinRatio: 0.01, | ||
symbol: "BTCUSDT", | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
asks: PriceVolumeSliceFromText(` | ||
19310,0.1 | ||
19320,0.1 | ||
19330,0.1 | ||
19340,0.1 | ||
19350,0.1 | ||
`), | ||
bids: PriceVolumeSliceFromText(` | ||
19300,10.0 | ||
19290,0.1 | ||
19280,0.1 | ||
19270,0.1 | ||
19260,0.1 | ||
`), | ||
}, | ||
want: 1.81623, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "normal", | ||
fields: fields{ | ||
PriceRange: fixedpoint.NewFromFloat(0.02), | ||
MinRatio: 0.01, | ||
symbol: "BTCUSDT", | ||
}, | ||
args: args{ | ||
ctx: context.Background(), | ||
asks: PriceVolumeSliceFromText(` | ||
19310,0.1 | ||
19320,0.2 | ||
19330,0.3 | ||
19340,0.4 | ||
19350,0.5 | ||
`), | ||
bids: PriceVolumeSliceFromText(` | ||
19300,0.1 | ||
19290,0.2 | ||
19280,0.3 | ||
19270,0.4 | ||
19260,0.5 | ||
`), | ||
}, | ||
want: 0, | ||
wantErr: assert.NoError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &DepthRatioSignal{ | ||
PriceRange: tt.fields.PriceRange, | ||
MinRatio: tt.fields.MinRatio, | ||
symbol: tt.fields.symbol, | ||
book: types.NewStreamBook("BTCUSDT", types.ExchangeBinance), | ||
} | ||
|
||
s.book.Load(types.SliceOrderBook{ | ||
Symbol: "BTCUSDT", | ||
Bids: tt.args.bids, | ||
Asks: tt.args.asks, | ||
}) | ||
|
||
got, err := s.CalculateSignal(tt.args.ctx) | ||
if !tt.wantErr(t, err, fmt.Sprintf("CalculateSignal(%v)", tt.args.ctx)) { | ||
return | ||
} | ||
|
||
assert.InDeltaf(t, tt.want, got, 0.001, "CalculateSignal(%v)", tt.args.ctx) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters