-
-
Notifications
You must be signed in to change notification settings - Fork 302
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 #1690 from c9s/c9s/xdepthmaker/convert-hedge-order…
…-trades FEATURE: improve trade/order converter
- Loading branch information
Showing
6 changed files
with
241 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package core | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
type Converter interface { | ||
OrderConverter | ||
TradeConverter | ||
Initialize() error | ||
} | ||
|
||
// OrderConverter converts the order to another order | ||
type OrderConverter interface { | ||
ConvertOrder(order types.Order) (types.Order, error) | ||
} | ||
|
||
// TradeConverter converts the trade to another trade | ||
type TradeConverter interface { | ||
ConvertTrade(trade types.Trade) (types.Trade, error) | ||
} | ||
|
||
type OrderConvertFunc func(order types.Order) (types.Order, error) | ||
type TradeConvertFunc func(trade types.Trade) (types.Trade, error) | ||
|
||
type DynamicConverter struct { | ||
orderConverter OrderConvertFunc | ||
tradeConverter TradeConvertFunc | ||
} | ||
|
||
func NewDynamicConverter(orderConverter OrderConvertFunc, tradeConverter TradeConvertFunc) *DynamicConverter { | ||
return &DynamicConverter{orderConverter: orderConverter, tradeConverter: tradeConverter} | ||
} | ||
|
||
func (c *DynamicConverter) Initialize() error { | ||
return nil | ||
} | ||
|
||
func (c *DynamicConverter) ConvertOrder(order types.Order) (types.Order, error) { | ||
return c.orderConverter(order) | ||
} | ||
|
||
func (c *DynamicConverter) ConvertTrade(trade types.Trade) (types.Trade, error) { | ||
return c.tradeConverter(trade) | ||
} | ||
|
||
// SymbolConverter converts the symbol to another symbol | ||
type SymbolConverter struct { | ||
FromSymbol string `json:"from"` | ||
ToSymbol string `json:"to"` | ||
} | ||
|
||
func NewSymbolConverter(fromSymbol, toSymbol string) *SymbolConverter { | ||
return &SymbolConverter{FromSymbol: fromSymbol, ToSymbol: toSymbol} | ||
} | ||
|
||
func (c *SymbolConverter) Initialize() error { | ||
if c.ToSymbol == "" { | ||
return errors.New("toSymbol can not be empty") | ||
} | ||
|
||
if c.FromSymbol == "" { | ||
return errors.New("fromSymbol can not be empty") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error) { | ||
if order.Symbol == c.FromSymbol { | ||
order.Symbol = c.ToSymbol | ||
} | ||
|
||
return order, nil | ||
} | ||
|
||
func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error) { | ||
if trade.Symbol == c.FromSymbol { | ||
trade.Symbol = c.ToSymbol | ||
} | ||
|
||
return trade, 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,31 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
func TestSymbolConverter(t *testing.T) { | ||
converter := NewSymbolConverter("MAXEXCHANGEUSDT", "MAXUSDT") | ||
trade, err := converter.ConvertTrade(types.Trade{ | ||
Symbol: "MAXEXCHANGEUSDT", | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, "MAXUSDT", trade.Symbol) | ||
} | ||
|
||
order, err := converter.ConvertOrder(types.Order{ | ||
SubmitOrder: types.SubmitOrder{ | ||
Symbol: "MAXEXCHANGEUSDT", | ||
}, | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, "MAXUSDT", order.Symbol) | ||
} | ||
|
||
} |
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
Oops, something went wrong.