Documentation ¶
Index ¶
- Constants
- type Grid
- func (g *Grid) Above(price fixedpoint.Value) bool
- func (g *Grid) Below(price fixedpoint.Value) bool
- func (g *Grid) BottomPin() Pin
- func (g *Grid) CalculateArithmeticPins()
- func (g *Grid) CalculateGeometricPins()
- func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin)
- func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin)
- func (g *Grid) FilterOrders(orders []types.Order) (ret []types.Order)
- func (g *Grid) HasPin(pin Pin) (ok bool)
- func (g *Grid) HasPrice(price fixedpoint.Value) bool
- func (g *Grid) Height() fixedpoint.Value
- func (g *Grid) NextHigherPin(price fixedpoint.Value) (Pin, bool)
- func (g *Grid) NextLowerPin(price fixedpoint.Value) (Pin, bool)
- func (g *Grid) OutOfRange(price fixedpoint.Value) bool
- func (g *Grid) SearchPin(price fixedpoint.Value) int
- func (g *Grid) String() string
- func (g *Grid) TopPin() Pin
- type GridProfit
- type GridProfitStats
- func (s *GridProfitStats) AddProfit(profit *GridProfit)
- func (s *GridProfitStats) AddTrade(trade types.Trade)
- func (s *GridProfitStats) Expiration() time.Duration
- func (s *GridProfitStats) PlainText() string
- func (s *GridProfitStats) SetTTL(ttl time.Duration)
- func (s *GridProfitStats) SlackAttachment() slack.Attachment
- func (s *GridProfitStats) String() string
- type OrderExecutor
- type Pin
- type PinCalculator
- type PinOrderMap
- type PrettyPins
- type PriceMap
- type ProfitFixer
- type Strategy
- func (s *Strategy) CleanUp(ctx context.Context) error
- func (s *Strategy) CloseGrid(ctx context.Context) error
- func (s *Strategy) Defaults() error
- func (s *Strategy) EmitGridClosed()
- func (s *Strategy) EmitGridError(err error)
- func (s *Strategy) EmitGridProfit(stats *GridProfitStats, profit *GridProfit)
- func (s *Strategy) EmitGridReady()
- func (s *Strategy) ID() string
- func (s *Strategy) Initialize() error
- func (s *Strategy) InstanceID() string
- func (s *Strategy) OnGridClosed(cb func())
- func (s *Strategy) OnGridError(cb func(err error))
- func (s *Strategy) OnGridProfit(cb func(stats *GridProfitStats, profit *GridProfit))
- func (s *Strategy) OnGridReady(cb func())
- func (s *Strategy) OpenGrid(ctx context.Context) error
- func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error
- func (s *Strategy) Subscribe(session *bbgo.ExchangeSession)
- func (s *Strategy) Validate() error
- type TwinOrder
- type TwinOrderBook
- func (b *TwinOrderBook) AddOrder(order types.Order, checkUpdateTime bool) error
- func (b *TwinOrderBook) EmptyTwinOrderSize() int
- func (b *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder
- func (b *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error)
- func (b *TwinOrderBook) Size() int
- func (b *TwinOrderBook) String() string
- func (b *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap
- type TwinOrderMap
Constants ¶
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) 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 ¶ added in v1.44.0
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
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"` Since *time.Time `json:"since,omitempty"` InitialOrderID uint64 `json:"initialOrderID"` // contains filtered or unexported fields }
func (*GridProfitStats) AddProfit ¶
func (s *GridProfitStats) AddProfit(profit *GridProfit)
func (*GridProfitStats) AddTrade ¶
func (s *GridProfitStats) AddTrade(trade types.Trade)
func (*GridProfitStats) Expiration ¶ added in v1.53.0
func (s *GridProfitStats) Expiration() time.Duration
func (*GridProfitStats) PlainText ¶ added in v1.44.0
func (s *GridProfitStats) PlainText() string
func (*GridProfitStats) SetTTL ¶ added in v1.53.0
func (s *GridProfitStats) SetTTL(ttl time.Duration)
func (*GridProfitStats) SlackAttachment ¶
func (s *GridProfitStats) SlackAttachment() slack.Attachment
func (*GridProfitStats) String ¶ added in v1.44.0
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 ¶ added in v1.44.1
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 ¶ added in v1.44.1
func (m PinOrderMap) AscendingOrders() []types.Order
AscendingOrders get the orders from pin order map and sort it in asc order
func (PinOrderMap) String ¶ added in v1.45.0
func (m PinOrderMap) String() string
func (PinOrderMap) SyncOrderMap ¶ added in v1.44.1
func (m PinOrderMap) SyncOrderMap() *types.SyncOrderMap
type PrettyPins ¶ added in v1.44.0
type PrettyPins []Pin
func (PrettyPins) String ¶ added in v1.44.0
func (pp PrettyPins) String() string
type PriceMap ¶ added in v1.43.1
type PriceMap map[string]fixedpoint.Value
type ProfitFixer ¶ added in v1.47.0
type ProfitFixer struct {
// contains filtered or unexported fields
}
func (*ProfitFixer) Fix ¶ added in v1.47.0
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 ¶ added in v1.47.0
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"` PersistenceTTL types.Duration `json:"persistenceTTL"` // 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) EmitGridClosed ¶ added in v1.44.0
func (s *Strategy) EmitGridClosed()
func (*Strategy) EmitGridError ¶ added in v1.44.0
func (*Strategy) EmitGridProfit ¶ added in v1.44.0
func (s *Strategy) EmitGridProfit(stats *GridProfitStats, profit *GridProfit)
func (*Strategy) EmitGridReady ¶ added in v1.44.0
func (s *Strategy) EmitGridReady()
func (*Strategy) Initialize ¶ added in v1.44.0
func (*Strategy) InstanceID ¶
InstanceID returns the instance identifier from the current grid configuration parameters
func (*Strategy) OnGridClosed ¶ added in v1.44.0
func (s *Strategy) OnGridClosed(cb func())
func (*Strategy) OnGridError ¶ added in v1.44.0
func (*Strategy) OnGridProfit ¶ added in v1.44.0
func (s *Strategy) OnGridProfit(cb func(stats *GridProfitStats, profit *GridProfit))
func (*Strategy) OnGridReady ¶ added in v1.44.0
func (s *Strategy) OnGridReady(cb func())
func (*Strategy) Run ¶
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error
func (*Strategy) Subscribe ¶
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession)
type TwinOrderBook ¶ added in v1.53.0
type TwinOrderBook struct {
// contains filtered or unexported fields
}
TwinOrderBook is to verify grid For grid trading, there are twin orders between a grid e.g. 100, 200, 300, 400, 500
BUY 100 and SELL 200 are a twin. BUY 200 and SELL 300 are a twin.
Because they can't be placed on orderbook at the same time. We use sell price to be the twin orderbook's key New the twin orderbook with pins, and it will sort the pins in asc order. There must be a non nil TwinOrder on the every pin (except the first one). But the TwinOrder.Exist() may be false. It means there is no twin order on this grid
func (*TwinOrderBook) AddOrder ¶ added in v1.53.0
func (b *TwinOrderBook) AddOrder(order types.Order, checkUpdateTime bool) error
func (*TwinOrderBook) EmptyTwinOrderSize ¶ added in v1.53.0
func (b *TwinOrderBook) EmptyTwinOrderSize() int
EmptyTwinOrderSize is the amount of grid there is no twin order on it.
func (*TwinOrderBook) GetTwinOrder ¶ added in v1.53.0
func (b *TwinOrderBook) GetTwinOrder(pin fixedpoint.Value) *TwinOrder
func (*TwinOrderBook) GetTwinOrderPin ¶ added in v1.53.0
func (b *TwinOrderBook) GetTwinOrderPin(order types.Order) (fixedpoint.Value, error)
func (*TwinOrderBook) Size ¶ added in v1.53.0
func (b *TwinOrderBook) Size() int
Size is the valid twin order on grid.
func (*TwinOrderBook) String ¶ added in v1.53.0
func (b *TwinOrderBook) String() string
func (*TwinOrderBook) SyncOrderMap ¶ added in v1.53.0
func (b *TwinOrderBook) SyncOrderMap() *types.SyncOrderMap
type TwinOrderMap ¶ added in v1.48.0
type TwinOrderMap map[fixedpoint.Value]TwinOrder
func (TwinOrderMap) AscendingOrders ¶ added in v1.48.0
func (m TwinOrderMap) AscendingOrders() []types.Order
func (TwinOrderMap) String ¶ added in v1.48.0
func (m TwinOrderMap) String() string
func (TwinOrderMap) SyncOrderMap ¶ added in v1.48.0
func (m TwinOrderMap) SyncOrderMap() *types.SyncOrderMap