Skip to content

Commit

Permalink
Look up market IDs from base+quote info (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyvega committed Jul 12, 2021
1 parent 040be8c commit 3ceb20e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
91 changes: 40 additions & 51 deletions bot/normal/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,47 @@ func (b *Bot) Start() error {
"address": b.config.Location,
}).Debug("Connected to Vega gRPC node")

marketResponse, err := b.node.MarketByID(&api.MarketByIDRequest{MarketId: b.config.MarketID})
marketsResponse, err := b.node.Markets(&api.MarketsRequest{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to get market %s", b.config.MarketID))
}
if marketResponse.Market == nil {
return fmt.Errorf("No market that matchs our ID: %s", b.config.MarketID)
return fmt.Errorf("failed to get markets: %w", err)
}
b.market = nil
for _, mkt := range marketsResponse.Markets {
instrument := mkt.TradableInstrument.GetInstrument()
if instrument != nil {
md := instrument.Metadata
base := ""
quote := ""
for _, tag := range md.Tags {
parts := strings.Split(tag, ":")
if len(parts) == 2 {
if parts[0] == "quote" {
quote = parts[1]
}
if parts[0] == "base" || parts[0] == "ticker" {
base = parts[1]
}
}
}
if base == b.config.InstrumentBase && quote == b.config.InstrumentQuote {
future := mkt.TradableInstrument.Instrument.GetFuture()
if future != nil {
b.settlementAssetID = future.SettlementAsset
b.market = mkt
break
}
}
}
}
b.market = marketResponse.Market
future := b.market.TradableInstrument.Instrument.GetFuture()
if future == nil {
return errors.New("market is not a Futures market")
if b.market == nil {
return fmt.Errorf("failed to find futures markets: base/ticker=%s, quote=%s", b.config.InstrumentBase, b.config.InstrumentQuote)
}
b.settlementAssetID = future.SettlementAsset
b.log.WithFields(log.Fields{
"marketID": b.config.MarketID,
"id": b.market.Id,
"base/ticker": b.config.InstrumentBase,
"quote": b.config.InstrumentQuote,
"settlementAssetID": b.settlementAssetID,
}).Debug("Fetched market info")
}).Info("Fetched market info")

// Use the settlementAssetID to lookup the settlement ethereum address
assetResponse, err := b.node.AssetByID(b.settlementAssetID)
Expand Down Expand Up @@ -420,8 +444,8 @@ func (b *Bot) sendOrder(
func (b *Bot) checkInitialMargin() error {
// Turn the shapes into a set of orders scaled by commitment
obligation := b.strategy.CommitmentFraction * float64(b.balanceMargin+b.balanceBond+b.balanceGeneral)
buyOrders := b.calculateOrderSizes(b.config.MarketID, b.walletPubKeyHex, obligation, b.buyShape, b.marketData.MidPrice)
sellOrders := b.calculateOrderSizes(b.config.MarketID, b.walletPubKeyHex, obligation, b.sellShape, b.marketData.MidPrice)
buyOrders := b.calculateOrderSizes(b.market.Id, b.walletPubKeyHex, obligation, b.buyShape, b.marketData.MidPrice)
sellOrders := b.calculateOrderSizes(b.market.Id, b.walletPubKeyHex, obligation, b.sellShape, b.marketData.MidPrice)

buyRisk := float64(0.01)
sellRisk := float64(0.01)
Expand Down Expand Up @@ -646,49 +670,14 @@ func (b *Bot) calculateMarginCost(risk float64, markPrice uint64, orders []*prot
return totalMargin
}

func (b *Bot) getPriceParts() (base, quote string, err error) {
// If we have been passed in the values, use those
if len(b.config.InstrumentBase) > 0 &&
len(b.config.InstrumentQuote) > 0 {
return b.config.InstrumentBase, b.config.InstrumentQuote, nil
}

// Find out the underlying assets for this market
instrument := b.market.TradableInstrument.GetInstrument()
if instrument != nil {
md := instrument.Metadata
for _, tag := range md.Tags {
parts := strings.Split(tag, ":")
if len(parts) == 2 {
if parts[0] == "quote" {
quote = parts[1]
}
if parts[0] == "base" {
base = parts[1]
}
}
}
}
if len(quote) == 0 || len(base) == 0 {
return "", "", fmt.Errorf("Unable to work out price assets from market metadata")
}
return
}

func (b *Bot) runPriceSteering() {
var externalPrice, currentPrice uint64
var err error
var externalPriceResponse ppservice.PriceResponse

base, quote, err := b.getPriceParts()
if err != nil {
b.log.WithFields(log.Fields{"error": err.Error()}).
Fatal("Unable to build instrument for external price feed")
}

ppcfg := ppconfig.PriceConfig{
Base: base,
Quote: quote,
Base: b.config.InstrumentBase,
Quote: b.config.InstrumentQuote,
Wander: true,
}

Expand Down
4 changes: 2 additions & 2 deletions bot/normal/streamingdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (b *Bot) subscribeToEvents() error {
Type: []eventspb.BusEventType{
eventspb.BusEventType_BUS_EVENT_TYPE_MARKET_DATA,
},
MarketId: b.config.MarketID,
MarketId: b.market.Id,
}
stream2, err := b.node.ObserveEventBus()
if err != nil {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (b *Bot) processEventBusData(stream api.TradingDataService_ObserveEventBusC

func (b *Bot) subscribePositions() error {
req := &api.PositionsSubscribeRequest{
MarketId: b.config.MarketID,
MarketId: b.market.Id,
PartyId: b.walletPubKeyHex,
}
stream, err := b.node.PositionsSubscribe(req)
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ type BotConfig struct {
// CallTimeout is the per-call timeout (in milliseconds) for communicating with the Vega node gRPC endpoint.
CallTimeout int `yaml:"callTimeout"`

// MarketID is the Vega Market ID.
MarketID string `yaml:"marketID"`

// InstrumentBase is the base asset of the instrument
InstrumentBase string `yaml:"instrumentBase"`

Expand Down

0 comments on commit 3ceb20e

Please sign in to comment.