grid2

package
v0.0.0-...-cb4139d Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const ID = "grid2"

Variables

This section is empty.

Functions

This section is empty.

Types

type Grid

type Grid struct {
	UpperPrice fixedpoint.Value `json:"upperPrice"`
	LowerPrice fixedpoint.Value `json:"lowerPrice"`

	// Size is the number of total grids
	Size fixedpoint.Value `json:"size"`

	// TickSize is the price tick size, this is used for truncating price
	TickSize fixedpoint.Value `json:"tickSize"`

	// Spread is a immutable number
	Spread fixedpoint.Value `json:"spread"`

	// Pins are the pinned grid prices, from low to high
	Pins []Pin `json:"pins"`
	// contains filtered or unexported fields
}

func NewGrid

func NewGrid(lower, upper, size, tickSize fixedpoint.Value) *Grid

func (*Grid) Above

func (g *Grid) Above(price fixedpoint.Value) bool

func (*Grid) Below

func (g *Grid) Below(price fixedpoint.Value) bool

func (*Grid) BottomPin

func (g *Grid) BottomPin() Pin

func (*Grid) CalculateArithmeticPins

func (g *Grid) CalculateArithmeticPins()

func (*Grid) CalculateGeometricPins

func (g *Grid) CalculateGeometricPins()

func (*Grid) ExtendLowerPrice

func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin)

func (*Grid) ExtendUpperPrice

func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin)

func (*Grid) FilterOrders

func (g *Grid) FilterOrders(orders []types.Order) (ret []types.Order)

func (*Grid) HasPin

func (g *Grid) HasPin(pin Pin) (ok bool)

func (*Grid) HasPrice

func (g *Grid) HasPrice(price fixedpoint.Value) bool

func (*Grid) Height

func (g *Grid) Height() fixedpoint.Value

func (*Grid) NextHigherPin

func (g *Grid) NextHigherPin(price fixedpoint.Value) (Pin, bool)

NextHigherPin finds the next higher pin

func (*Grid) NextLowerPin

func (g *Grid) NextLowerPin(price fixedpoint.Value) (Pin, bool)

NextLowerPin finds the next lower pin

func (*Grid) OutOfRange

func (g *Grid) OutOfRange(price fixedpoint.Value) bool

func (*Grid) SearchPin

func (g *Grid) SearchPin(price fixedpoint.Value) int

func (*Grid) String

func (g *Grid) String() string

func (*Grid) TopPin

func (g *Grid) TopPin() Pin

type GridProfit

type GridProfit struct {
	Currency string           `json:"currency"`
	Profit   fixedpoint.Value `json:"profit"`
	Time     time.Time        `json:"time"`
	Order    types.Order      `json:"order"`
}

func (*GridProfit) PlainText

func (p *GridProfit) PlainText() string

func (*GridProfit) SlackAttachment

func (p *GridProfit) SlackAttachment() slack.Attachment

func (*GridProfit) String

func (p *GridProfit) String() string

type GridProfitStats

type GridProfitStats struct {
	Symbol           string                      `json:"symbol"`
	TotalBaseProfit  fixedpoint.Value            `json:"totalBaseProfit,omitempty"`
	TotalQuoteProfit fixedpoint.Value            `json:"totalQuoteProfit,omitempty"`
	FloatProfit      fixedpoint.Value            `json:"floatProfit,omitempty"`
	GridProfit       fixedpoint.Value            `json:"gridProfit,omitempty"`
	ArbitrageCount   int                         `json:"arbitrageCount,omitempty"`
	TotalFee         map[string]fixedpoint.Value `json:"totalFee,omitempty"`
	Volume           fixedpoint.Value            `json:"volume,omitempty"`
	Market           types.Market                `json:"market,omitempty"`
	ProfitEntries    []*GridProfit               `json:"profitEntries,omitempty"`
	Since            *time.Time                  `json:"since,omitempty"`
	InitialOrderID   uint64                      `json:"initialOrderID"`
}

func (*GridProfitStats) AddProfit

func (s *GridProfitStats) AddProfit(profit *GridProfit)

func (*GridProfitStats) AddTrade

func (s *GridProfitStats) AddTrade(trade types.Trade)

func (*GridProfitStats) PlainText

func (s *GridProfitStats) PlainText() string

func (*GridProfitStats) SlackAttachment

func (s *GridProfitStats) SlackAttachment() slack.Attachment

func (*GridProfitStats) String

func (s *GridProfitStats) String() string

type OrderExecutor

type OrderExecutor interface {
	SubmitOrders(ctx context.Context, submitOrders ...types.SubmitOrder) (types.OrderSlice, error)
	ClosePosition(ctx context.Context, percentage fixedpoint.Value, tags ...string) error
	GracefulCancel(ctx context.Context, orders ...types.Order) error
	ActiveMakerOrders() *bbgo.ActiveOrderBook
}

type Pin

type Pin fixedpoint.Value

type PinCalculator

type PinCalculator func() []Pin

type PinOrderMap

type PinOrderMap map[fixedpoint.Value]types.Order

PinOrderMap store the pin-order's relation, we will change key from string to fixedpoint.Value when FormatString fixed

func (PinOrderMap) AscendingOrders

func (m PinOrderMap) AscendingOrders() []types.Order

AscendingOrders get the orders from pin order map and sort it in asc order

func (PinOrderMap) String

func (m PinOrderMap) String() string

func (PinOrderMap) SyncOrderMap

func (m PinOrderMap) SyncOrderMap() *types.SyncOrderMap

type PrettyPins

type PrettyPins []Pin

func (PrettyPins) String

func (pp PrettyPins) String() string

type PriceMap

type PriceMap map[string]fixedpoint.Value

type ProfitFixer

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

func (*ProfitFixer) Fix

func (f *ProfitFixer) Fix(parent context.Context, since, until time.Time, initialOrderID uint64, profitStats *GridProfitStats) error

Fix fixes the total quote profit of the given grid

func (*ProfitFixer) SetLogger

func (f *ProfitFixer) SetLogger(logger logrus.FieldLogger)

type Strategy

type Strategy struct {
	Environment *bbgo.Environment

	// Market stores the configuration of the market, for example, VolumePrecision, PricePrecision, MinLotSize... etc
	// This field will be injected automatically since we defined the Symbol field.
	types.Market `json:"-"`

	// These fields will be filled from the config file (it translates YAML to JSON)
	Symbol string `json:"symbol"`

	// ProfitSpread is the fixed profit spread you want to submit the sell order
	// When ProfitSpread is enabled, the grid will shift up, e.g.,
	// If you opened a grid with the price range 10_000 to 20_000
	// With profit spread set to 3_000
	// The sell orders will be placed in the range 13_000 to 23_000
	// And the buy orders will be placed in the original price range 10_000 to 20_000
	ProfitSpread fixedpoint.Value `json:"profitSpread"`

	// GridNum is the grid number, how many orders you want to post on the orderbook.
	GridNum int64 `json:"gridNumber"`

	// BaseGridNum is an optional field used for base investment sell orders
	BaseGridNum int `json:"baseGridNumber,omitempty"`

	AutoRange *types.SimpleDuration `json:"autoRange"`

	UpperPrice fixedpoint.Value `json:"upperPrice"`

	LowerPrice fixedpoint.Value `json:"lowerPrice"`

	// Compound option is used for buying more inventory when
	// the profit is made by the filled sell order.
	Compound bool `json:"compound"`

	// EarnBase option is used for earning profit in base currency.
	// e.g. earn BTC in BTCUSDT and earn ETH in ETHUSDT
	// instead of earn USDT in BTCUSD
	EarnBase bool `json:"earnBase"`

	// QuantityOrAmount embeds the Quantity field and the Amount field
	// If you set up the Quantity field or the Amount field, you don't need to set the QuoteInvestment and BaseInvestment
	bbgo.QuantityOrAmount

	// If Quantity and Amount is not set, we can use the quote investment to calculate our quantity.
	QuoteInvestment fixedpoint.Value `json:"quoteInvestment"`

	// BaseInvestment is the total base quantity you want to place as the sell order.
	BaseInvestment fixedpoint.Value `json:"baseInvestment"`

	TriggerPrice fixedpoint.Value `json:"triggerPrice"`

	StopLossPrice   fixedpoint.Value `json:"stopLossPrice"`
	TakeProfitPrice fixedpoint.Value `json:"takeProfitPrice"`

	// CloseWhenCancelOrder option is used to close the grid if any of the order is canceled.
	// This option let you simply remote control the grid from the crypto exchange mobile app.
	CloseWhenCancelOrder bool `json:"closeWhenCancelOrder"`

	// KeepOrdersWhenShutdown option is used for keeping the grid orders when shutting down bbgo
	KeepOrdersWhenShutdown bool `json:"keepOrdersWhenShutdown"`

	// RecoverOrdersWhenStart option is used for recovering grid orders
	RecoverOrdersWhenStart bool `json:"recoverOrdersWhenStart"`

	// ClearOpenOrdersWhenStart
	// If this is set, when bbgo started, it will clear the open orders in the same market (by symbol)
	ClearOpenOrdersWhenStart bool `json:"clearOpenOrdersWhenStart"`

	ClearOpenOrdersIfMismatch bool `json:"clearOpenOrdersIfMismatch"`

	ClearDuplicatedPriceOpenOrders bool `json:"clearDuplicatedPriceOpenOrders"`

	// UseCancelAllOrdersApiWhenClose uses a different API to cancel all the orders on the market when closing a grid
	UseCancelAllOrdersApiWhenClose bool `json:"useCancelAllOrdersApiWhenClose"`

	// ResetPositionWhenStart resets the position when the strategy is started
	ResetPositionWhenStart bool `json:"resetPositionWhenStart"`

	// StopIfLessThanMinimalQuoteInvestment stops the strategy if the quote investment does not match
	StopIfLessThanMinimalQuoteInvestment bool `json:"stopIfLessThanMinimalQuoteInvestment"`

	OrderFillDelay types.Duration `json:"orderFillDelay"`

	// PrometheusLabels will be used as the base prometheus labels
	PrometheusLabels prometheus.Labels `json:"prometheusLabels"`

	// OrderGroupID is the group ID used for the strategy instance for canceling orders
	OrderGroupID uint32 `json:"orderGroupID"`

	LogFields logrus.Fields `json:"logFields"`

	// FeeRate is used for calculating the minimal profit spread.
	// it makes sure that your grid configuration is profitable.
	FeeRate fixedpoint.Value `json:"feeRate"`

	SkipSpreadCheck             bool          `json:"skipSpreadCheck"`
	RecoverGridByScanningTrades bool          `json:"recoverGridByScanningTrades"`
	RecoverGridWithin           time.Duration `json:"recoverGridWithin"`

	EnableProfitFixer bool        `json:"enableProfitFixer"`
	FixProfitSince    *types.Time `json:"fixProfitSince"`

	// Debug enables the debug mode
	Debug bool `json:"debug"`

	GridProfitStats *GridProfitStats `persistence:"grid_profit_stats"`
	Position        *types.Position  `persistence:"position"`

	// ExchangeSession is an injection field
	ExchangeSession *bbgo.ExchangeSession

	// this ensures that bbgo.Sync to lock the object
	sync.Mutex
	// contains filtered or unexported fields
}

func (*Strategy) CleanUp

func (s *Strategy) CleanUp(ctx context.Context) error

func (*Strategy) CloseGrid

func (s *Strategy) CloseGrid(ctx context.Context) error

CloseGrid closes the grid orders

func (*Strategy) Defaults

func (s *Strategy) Defaults() error

func (*Strategy) EmitGridClosed

func (s *Strategy) EmitGridClosed()

func (*Strategy) EmitGridError

func (s *Strategy) EmitGridError(err error)

func (*Strategy) EmitGridProfit

func (s *Strategy) EmitGridProfit(stats *GridProfitStats, profit *GridProfit)

func (*Strategy) EmitGridReady

func (s *Strategy) EmitGridReady()

func (*Strategy) ID

func (s *Strategy) ID() string

func (*Strategy) Initialize

func (s *Strategy) Initialize() error

func (*Strategy) InstanceID

func (s *Strategy) InstanceID() string

InstanceID returns the instance identifier from the current grid configuration parameters

func (*Strategy) OnGridClosed

func (s *Strategy) OnGridClosed(cb func())

func (*Strategy) OnGridError

func (s *Strategy) OnGridError(cb func(err error))

func (*Strategy) OnGridProfit

func (s *Strategy) OnGridProfit(cb func(stats *GridProfitStats, profit *GridProfit))

func (*Strategy) OnGridReady

func (s *Strategy) OnGridReady(cb func())

func (*Strategy) OpenGrid

func (s *Strategy) OpenGrid(ctx context.Context) error

func (*Strategy) Run

func (*Strategy) Subscribe

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession)

func (*Strategy) Validate

func (s *Strategy) Validate() error

type TwinOrder

type TwinOrder struct {
	BuyOrder  types.Order
	SellOrder types.Order
}

func (*TwinOrder) Exist

func (t *TwinOrder) Exist() bool

func (*TwinOrder) GetOrder

func (t *TwinOrder) GetOrder() types.Order

func (*TwinOrder) IsValid

func (t *TwinOrder) IsValid() bool

func (*TwinOrder) SetOrder

func (t *TwinOrder) SetOrder(order types.Order)

type TwinOrderMap

type TwinOrderMap map[fixedpoint.Value]TwinOrder

func (TwinOrderMap) AscendingOrders

func (m TwinOrderMap) AscendingOrders() []types.Order

func (TwinOrderMap) String

func (m TwinOrderMap) String() string

func (TwinOrderMap) SyncOrderMap

func (m TwinOrderMap) SyncOrderMap() *types.SyncOrderMap

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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