-
-
Notifications
You must be signed in to change notification settings - Fork 299
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 #1730 from c9s/c9s/xmaker/market-trade-signal
FEATURE: [xmaker] add market trade signal
- Loading branch information
Showing
6 changed files
with
211 additions
and
54 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
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,111 @@ | ||
package xmaker | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"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 tradeVolumeWindowSignalMetrics = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "xmaker_trade_volume_window_signal", | ||
Help: "", | ||
}, []string{"symbol"}) | ||
|
||
func init() { | ||
prometheus.MustRegister(tradeVolumeWindowSignalMetrics) | ||
} | ||
|
||
type TradeVolumeWindowSignal struct { | ||
Threshold fixedpoint.Value `json:"threshold"` | ||
Window types.Duration `json:"window"` | ||
|
||
trades []types.Trade | ||
symbol string | ||
|
||
mu sync.Mutex | ||
} | ||
|
||
func (s *TradeVolumeWindowSignal) handleTrade(trade types.Trade) { | ||
s.mu.Lock() | ||
s.trades = append(s.trades, trade) | ||
s.mu.Unlock() | ||
} | ||
|
||
func (s *TradeVolumeWindowSignal) Bind(ctx context.Context, session *bbgo.ExchangeSession, symbol string) error { | ||
s.symbol = symbol | ||
|
||
if s.Window == 0 { | ||
s.Window = types.Duration(time.Minute) | ||
} | ||
|
||
if s.Threshold.IsZero() { | ||
s.Threshold = fixedpoint.NewFromFloat(0.7) | ||
} | ||
|
||
session.MarketDataStream.OnMarketTrade(s.handleTrade) | ||
return nil | ||
} | ||
|
||
func (s *TradeVolumeWindowSignal) filterTrades(now time.Time) []types.Trade { | ||
startTime := now.Add(-time.Duration(s.Window)) | ||
startIdx := 0 | ||
|
||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
for idx, td := range s.trades { | ||
// skip trades before the start time | ||
if td.Time.Before(startTime) { | ||
continue | ||
} | ||
|
||
startIdx = idx | ||
break | ||
} | ||
|
||
trades := s.trades[startIdx:] | ||
s.trades = trades | ||
return trades | ||
} | ||
|
||
func (s *TradeVolumeWindowSignal) aggTradeVolume(trades []types.Trade) (buyVolume, sellVolume float64) { | ||
for _, td := range trades { | ||
if td.IsBuyer { | ||
buyVolume += td.Quantity.Float64() | ||
} else { | ||
sellVolume += td.Quantity.Float64() | ||
} | ||
} | ||
|
||
return buyVolume, sellVolume | ||
} | ||
|
||
func (s *TradeVolumeWindowSignal) CalculateSignal(_ context.Context) (float64, error) { | ||
now := time.Now() | ||
trades := s.filterTrades(now) | ||
buyVolume, sellVolume := s.aggTradeVolume(trades) | ||
totalVolume := buyVolume + sellVolume | ||
|
||
threshold := s.Threshold.Float64() | ||
buyRatio := buyVolume / totalVolume | ||
sellRatio := sellVolume / totalVolume | ||
|
||
sig := 0.0 | ||
if buyRatio > threshold { | ||
sig = (buyRatio - threshold) / 2.0 | ||
} else if sellRatio > threshold { | ||
sig = -(sellRatio - threshold) / 2.0 | ||
} | ||
|
||
log.Infof("[TradeVolumeWindowSignal] %f buy/sell = %f/%f", sig, buyVolume, sellVolume) | ||
|
||
tradeVolumeWindowSignalMetrics.WithLabelValues(s.symbol).Set(sig) | ||
return sig, 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,55 @@ | ||
package xmaker | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
|
||
. "github.com/c9s/bbgo/pkg/testing/testhelper" | ||
) | ||
|
||
var tradeId = 0 | ||
|
||
func Trade(symbol string, side types.SideType, price, quantity fixedpoint.Value, t time.Time) types.Trade { | ||
tradeId++ | ||
return types.Trade{ | ||
ID: uint64(tradeId), | ||
Symbol: symbol, | ||
Side: side, | ||
Price: price, | ||
IsBuyer: side == types.SideTypeBuy, | ||
Quantity: quantity, | ||
Time: types.Time(t), | ||
} | ||
} | ||
|
||
func TestMarketTradeWindowSignal(t *testing.T) { | ||
now := time.Now() | ||
symbol := "BTCUSDT" | ||
sig := &TradeVolumeWindowSignal{ | ||
symbol: symbol, | ||
Threshold: fixedpoint.NewFromFloat(0.65), | ||
Window: types.Duration(time.Minute), | ||
} | ||
|
||
sig.trades = []types.Trade{ | ||
Trade(symbol, types.SideTypeBuy, Number(18000.0), Number(1.0), now.Add(-2*time.Minute)), | ||
Trade(symbol, types.SideTypeSell, Number(18000.0), Number(0.5), now.Add(-2*time.Second)), | ||
Trade(symbol, types.SideTypeBuy, Number(18000.0), Number(1.0), now.Add(-1*time.Second)), | ||
} | ||
|
||
ctx := context.Background() | ||
sigNum, err := sig.CalculateSignal(ctx) | ||
if assert.NoError(t, err) { | ||
// buy ratio: 1/1.5 = 0.6666666666666666 | ||
// sell ratio: 0.5/1.5 = 0.3333333333333333 | ||
assert.InDelta(t, 0.0083333, sigNum, 0.0001) | ||
} | ||
|
||
assert.Len(t, sig.trades, 2) | ||
} |
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