examples

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 14 Imported by: 0

README

Examples

The examples are a progressive API cookbook. Start at 01_... and move down only when you need the next layer of the trading stack.

File Use When You Are Building
01_fetch_ticker_with_adapter.go A market-data probe, health check, quote snapshot tool, or connector smoke test.
02_build_orders_with_order_factory.go A strategy that needs normalized order commands without hand-building IDs and metadata.
03_validate_risk_before_execution.go A bot or execution service that must reject unsafe orders before venue submission.
04_run_strategy_backtest.go A research workflow that replays market events through strategy.Runtime.
05_submit_bracket_order_backtest.go A strategy using parent/child order lists, take-profit, and stop-loss semantics.
06_run_live_node_with_in_memory_venue.go A paper-trading harness, integration test, or live-node assembly prototype.
07_monitor_funding_rate_arbitrage.go A multi-venue funding-rate monitor that creates and risk-checks a hedged arbitrage order pair.

All examples are compiled and exercised by examples_test.go except the real Binance network helper in 01_fetch_ticker_with_adapter.go, which is provided as a direct adapter entry point and intentionally not called by default.

Run them with:

env GOCACHE=/private/tmp/go-build-exchanges go test -count=1 ./examples -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FetchBTCUSDTFromBinanceSpot

func FetchBTCUSDTFromBinanceSpot(ctx context.Context) (model.Ticker, error)

FetchBTCUSDTFromBinanceSpot is the same workflow using the concrete Binance spot adapter. It calls Binance public endpoints, so tests compile this helper but do not execute it by default.

func FetchTickerWithDataClient

func FetchTickerWithDataClient(ctx context.Context, client venue.DataClient, instrumentID model.InstrumentID) (model.Ticker, error)

FetchTickerWithDataClient shows the smallest normalized market-data path: connect a venue.DataClient, request one ticker, and disconnect it.

Types

type BacktestStrategyResult

type BacktestStrategyResult struct {
	EventsProcessed int
	Order           model.OrderStatusReport
	Position        model.PositionStatusReport
	Fills           []model.FillReport
	ExposureUSDT    decimal.Decimal
}

func RunThresholdStrategyBacktest

func RunThresholdStrategyBacktest(ctx context.Context) (BacktestStrategyResult, error)

RunThresholdStrategyBacktest replays two order-book events through the same strategy.Runtime surface used by live nodes. The strategy subscribes in OnStart, reacts in OnOrderBook, submits through OrderFactory, and reads final state from cache and portfolio.

type BracketBacktestResult

type BracketBacktestResult struct {
	Entry      model.OrderStatusReport
	StopLoss   model.OrderStatusReport
	TakeProfit model.OrderStatusReport
	Fills      []model.FillReport
}

func RunBracketOrderBacktest

func RunBracketOrderBacktest(ctx context.Context) (BracketBacktestResult, error)

RunBracketOrderBacktest shows the advanced order-list path: one parent entry order releases two reduce-only children, and the take-profit fill cancels the stop-loss sibling.

type FundingArbitrageConfig

type FundingArbitrageConfig struct {
	Symbol             string
	Quantity           decimal.Decimal
	MinFundingSpread   decimal.Decimal
	MinNetEdge         decimal.Decimal
	SlippageBufferRate decimal.Decimal
	MaxOrderNotional   decimal.Decimal
}

type FundingArbitrageDecision

type FundingArbitrageDecision struct {
	ShouldTrade       bool
	Reason            string
	Long              FundingVenueSnapshot
	Short             FundingVenueSnapshot
	FundingSpread     decimal.Decimal
	EstimatedCostRate decimal.Decimal
	ExpectedNetRate   decimal.Decimal
	ExpectedNetUSDT   decimal.Decimal
	Orders            []model.SubmitOrder
	Reports           []model.OrderStatusReport
}

func RunFundingRateArbitrageMonitor

func RunFundingRateArbitrageMonitor(ctx context.Context) (FundingArbitrageDecision, error)

RunFundingRateArbitrageMonitor shows a production-shaped funding-rate arbitrage flow without depending on any live credentials:

  1. collect funding snapshots from multiple venues;
  2. find the venue paying the most to shorts and the venue charging longs the least;
  3. estimate taker-fee plus slippage cost;
  4. create a delta-neutral long/short order pair with OrderFactory;
  5. run risk checks before routing orders to execution clients by AccountID.

type FundingArbitrageMonitor

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

func NewFundingArbitrageMonitor

func NewFundingArbitrageMonitor(cfg FundingArbitrageConfig, sources []FundingRateSource, riskEngine *risk.Engine, executor *fundingExecutionRouter) *FundingArbitrageMonitor

func (*FundingArbitrageMonitor) EvaluateOnce

type FundingRateSource

type FundingRateSource interface {
	Name() string
	Snapshots(context.Context) ([]FundingVenueSnapshot, error)
}

type FundingVenueSnapshot

type FundingVenueSnapshot struct {
	Venue        model.Venue
	AccountID    model.AccountID
	RawSymbol    string
	Base         model.Currency
	Quote        model.Currency
	Funding      model.FundingRate
	TakerFeeRate decimal.Decimal
}

type LiveNodeResult

type LiveNodeResult struct {
	SubmittedOrder model.OrderStatusReport
	Fills          []model.FillReport
	Position       model.PositionStatusReport
	ExposureUSDT   decimal.Decimal
	EventLog       []string
}

func RunLiveNodeWithInMemoryVenue

func RunLiveNodeWithInMemoryVenue(ctx context.Context) (LiveNodeResult, error)

RunLiveNodeWithInMemoryVenue is a full local live-node assembly. It is useful for paper trading, integration tests, and adapter development because the strategy, risk, execution, cache, portfolio, and callbacks are real; only the venue network edge is replaced by in-memory clients.

type OrderFactoryWalkthrough

type OrderFactoryWalkthrough struct {
	Market       model.SubmitOrder
	PostOnly     model.SubmitOrder
	StopMarket   model.SubmitOrder
	TrailingStop model.SubmitOrder
	Bracket      model.OrderList
}

func BuildOrdersWithOrderFactory

func BuildOrdersWithOrderFactory() OrderFactoryWalkthrough

BuildOrdersWithOrderFactory shows how strategy code should create commands: use OrderFactory for account identity, client order IDs, list IDs, and shared command metadata instead of assembling those fields by hand.

type RiskValidationResult

type RiskValidationResult struct {
	Accepted  model.SubmitOrder
	Rejected  model.SubmitOrder
	RejectErr error
}

func ValidateRiskBeforeExecution

func ValidateRiskBeforeExecution() (RiskValidationResult, error)

ValidateRiskBeforeExecution demonstrates the normal execution boundary: put instrument metadata in cache, configure limits, then call risk.Check before an order is allowed to reach a venue execution client.

Jump to

Keyboard shortcuts

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