core

package
v1.60.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2024 License: AGPL-3.0 Imports: 8 Imported by: 11

Documentation

Index

Constants

View Source
const CoolTradePeriod = 1 * time.Hour
View Source
const MaximumTradeStoreSize = 1_000
View Source
const TradeExpiryTime = 3 * time.Hour

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter added in v1.60.0

type Converter interface {
	OrderConverter
	TradeConverter
	Initialize() error
}

type ConverterManager added in v1.60.0

type ConverterManager struct {
	ConverterSettings []ConverterSetting `json:"converters,omitempty" yaml:"converters,omitempty"`
	// contains filtered or unexported fields
}

ConverterManager manages the converters for trade conversion It can be used to convert the trade symbol into the target symbol, or convert the price, volume into different units.

func (*ConverterManager) AddConverter added in v1.60.0

func (c *ConverterManager) AddConverter(converter Converter)

func (*ConverterManager) ConvertOrder added in v1.60.0

func (c *ConverterManager) ConvertOrder(order types.Order) types.Order

func (*ConverterManager) ConvertTrade added in v1.60.0

func (c *ConverterManager) ConvertTrade(trade types.Trade) types.Trade

func (*ConverterManager) Initialize added in v1.60.0

func (c *ConverterManager) Initialize() error

type ConverterSetting added in v1.60.0

type ConverterSetting struct {
	SymbolConverter *SymbolConverter `json:"symbolConverter" yaml:"symbolConverter"`
}

func (*ConverterSetting) InitializeConverter added in v1.60.0

func (s *ConverterSetting) InitializeConverter() (Converter, error)

type DynamicConverter added in v1.60.0

type DynamicConverter struct {
	// contains filtered or unexported fields
}

func NewDynamicConverter added in v1.60.0

func NewDynamicConverter(orderConverter OrderConvertFunc, tradeConverter TradeConvertFunc) *DynamicConverter

func (*DynamicConverter) ConvertOrder added in v1.60.0

func (c *DynamicConverter) ConvertOrder(order types.Order) (types.Order, error)

func (*DynamicConverter) ConvertTrade added in v1.60.0

func (c *DynamicConverter) ConvertTrade(trade types.Trade) (types.Trade, error)

func (*DynamicConverter) Initialize added in v1.60.0

func (c *DynamicConverter) Initialize() error

type OrderConvertFunc added in v1.60.0

type OrderConvertFunc func(order types.Order) (types.Order, error)

type OrderConverter added in v1.60.0

type OrderConverter interface {
	ConvertOrder(order types.Order) (types.Order, error)
}

OrderConverter converts the order to another order

type OrderStore

type OrderStore struct {
	Symbol string

	// RemoveCancelled removes the canceled order when receiving a cancel order update event
	// It also removes the order even if it's partially filled
	// by default, only 0 filled canceled order will be removed.
	RemoveCancelled bool

	// RemoveFilled removes the fully filled order when receiving a filled order update event
	RemoveFilled bool

	// AddOrderUpdate adds the order into the store when receiving an order update when the order does not exist in the current store.
	AddOrderUpdate bool
	C              chan types.Order
	// contains filtered or unexported fields
}

func NewOrderStore

func NewOrderStore(symbol string) *OrderStore

func (*OrderStore) Add

func (s *OrderStore) Add(orders ...types.Order)

func (*OrderStore) AllFilled

func (s *OrderStore) AllFilled() bool

func (*OrderStore) BindStream

func (s *OrderStore) BindStream(stream types.Stream)

func (*OrderStore) Exists

func (s *OrderStore) Exists(oID uint64) (ok bool)

func (*OrderStore) Get

func (s *OrderStore) Get(oID uint64) (order types.Order, ok bool)

Get a single order from the order store by order ID Should check ok to make sure the order is returned successfully

func (*OrderStore) HandleOrderUpdate

func (s *OrderStore) HandleOrderUpdate(order types.Order)

func (*OrderStore) NumOfOrders

func (s *OrderStore) NumOfOrders() (num int)

func (*OrderStore) Orders

func (s *OrderStore) Orders() (orders []types.Order)

func (*OrderStore) Remove

func (s *OrderStore) Remove(o types.Order)

func (*OrderStore) Update

func (s *OrderStore) Update(o types.Order) bool

type SymbolConverter added in v1.60.0

type SymbolConverter struct {
	FromSymbol string `json:"from"`
	ToSymbol   string `json:"to"`
}

SymbolConverter converts the symbol to another symbol

func NewSymbolConverter added in v1.60.0

func NewSymbolConverter(fromSymbol, toSymbol string) *SymbolConverter

func (*SymbolConverter) ConvertOrder added in v1.60.0

func (c *SymbolConverter) ConvertOrder(order types.Order) (types.Order, error)

func (*SymbolConverter) ConvertTrade added in v1.60.0

func (c *SymbolConverter) ConvertTrade(trade types.Trade) (types.Trade, error)

func (*SymbolConverter) Initialize added in v1.60.0

func (c *SymbolConverter) Initialize() error

type TradeCollector

type TradeCollector struct {
	Symbol string

	ConverterManager
	// contains filtered or unexported fields
}

func NewTradeCollector

func NewTradeCollector(symbol string, position *types.Position, orderStore *OrderStore) *TradeCollector

func (*TradeCollector) BindStream

func (c *TradeCollector) BindStream(stream types.Stream)

func (*TradeCollector) BindStreamForBackground

func (c *TradeCollector) BindStreamForBackground(stream types.Stream)

BindStreamForBackground bind the stream callback for background processing

func (*TradeCollector) Emit

func (c *TradeCollector) Emit()

Emit triggers the trade processing (position update) If you sent order, and the order store is updated, you can call this method so that trades will be processed in the next round of the goroutine loop

func (*TradeCollector) EmitPositionUpdate

func (c *TradeCollector) EmitPositionUpdate(position *types.Position)

func (*TradeCollector) EmitProfit

func (c *TradeCollector) EmitProfit(trade types.Trade, profit *types.Profit)

func (*TradeCollector) EmitRecover

func (c *TradeCollector) EmitRecover(trade types.Trade)

func (*TradeCollector) EmitTrade

func (c *TradeCollector) EmitTrade(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value)

func (*TradeCollector) OnPositionUpdate

func (c *TradeCollector) OnPositionUpdate(cb func(position *types.Position))

func (*TradeCollector) OnProfit

func (c *TradeCollector) OnProfit(cb func(trade types.Trade, profit *types.Profit))

func (*TradeCollector) OnRecover

func (c *TradeCollector) OnRecover(cb func(trade types.Trade))

func (*TradeCollector) OnTrade

func (c *TradeCollector) OnTrade(cb func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value))

func (*TradeCollector) OrderStore

func (c *TradeCollector) OrderStore() *OrderStore

OrderStore returns the order store used by the trade collector

func (*TradeCollector) Position

func (c *TradeCollector) Position() *types.Position

Position returns the position used by the trade collector

func (*TradeCollector) Process

func (c *TradeCollector) Process() bool

Process filters the received trades and see if there are orders matching the trades if we have the order in the order store, then the trade will be considered for the position. profit will also be calculated.

func (*TradeCollector) ProcessTrade

func (c *TradeCollector) ProcessTrade(trade types.Trade) bool

return true when the given trade is added return false when the given trade is not added

func (*TradeCollector) QueueTrade

func (c *TradeCollector) QueueTrade(trade types.Trade)

QueueTrade sends the trade object to the trade channel, so that the goroutine can receive the trade and process in the background.

func (*TradeCollector) Recover

func (c *TradeCollector) Recover(
	ctx context.Context, ex types.ExchangeTradeHistoryService, symbol string, from time.Time,
) error

func (*TradeCollector) RecoverTrade

func (c *TradeCollector) RecoverTrade(td types.Trade) bool

func (*TradeCollector) Run

func (c *TradeCollector) Run(ctx context.Context)

Run is a goroutine executed in the background Do not use this function if you need back-testing

func (*TradeCollector) SetPosition

func (c *TradeCollector) SetPosition(position *types.Position)

func (*TradeCollector) TradeStore

func (c *TradeCollector) TradeStore() *TradeStore

type TradeConvertFunc added in v1.60.0

type TradeConvertFunc func(trade types.Trade) (types.Trade, error)

type TradeConverter added in v1.60.0

type TradeConverter interface {
	ConvertTrade(trade types.Trade) (types.Trade, error)
}

TradeConverter converts the trade to another trade

type TradeFilter

type TradeFilter func(trade types.Trade) bool

type TradeStore

type TradeStore struct {
	// any created trades for tracking trades
	sync.Mutex

	EnablePrune bool
	// contains filtered or unexported fields
}

func NewTradeStore

func NewTradeStore() *TradeStore

func (*TradeStore) Add

func (s *TradeStore) Add(trades ...types.Trade)

func (*TradeStore) BindStream

func (s *TradeStore) BindStream(stream types.Stream)

func (*TradeStore) Clear

func (s *TradeStore) Clear()

func (*TradeStore) Exists

func (s *TradeStore) Exists(oID uint64) (ok bool)

func (*TradeStore) Filter

func (s *TradeStore) Filter(filter TradeFilter)

Filter filters the trades by a given TradeFilter function

func (*TradeStore) GetAndClear

func (s *TradeStore) GetAndClear() (trades []types.Trade)

func (*TradeStore) GetOrderTrades

func (s *TradeStore) GetOrderTrades(o types.Order) (trades []types.Trade)

GetOrderTrades finds the trades match order id matches to the given order

func (*TradeStore) Num

func (s *TradeStore) Num() (num int)

func (*TradeStore) Prune

func (s *TradeStore) Prune(curTime time.Time)

Prune prunes trades that are older than the expiry time see TradeExpiryTime (3 hours)

func (*TradeStore) Trades

func (s *TradeStore) Trades() (trades []types.Trade)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL