Skip to main content

Crate pine_broker

Crate pine_broker 

Source
Expand description

A simulated broker: the emulator a Pine strategy trades against.

There is no order book and no exchange. A backtest replays historical bars and asks, bar by bar, what would have happened — so filling an order is a modelling assumption (FillModel), not a match against a real resting order. Everything else — position, average price, commission, the trade log, equity — is plain accounting that does not depend on the venue, so there is one BarBroker, not one per exchange.

Placing real orders is deliberately out of scope: in Pine that happens outside the strategy, when an alert is delivered to an external system. This crate only simulates.

Structs§

BarBroker
BrokerConfig
The account settings a strategy() declaration configures its broker with, so a custom BrokerFactory can honour the script’s parameters rather than inventing its own.
DefaultBrokerFactory
The built-in factory: a BarBroker with PineFills, reproducing Pine’s default fill model.
Exit
A stop-loss / take-profit bracket attached to a position, from strategy.exit. Its legs are evaluated each bar once the position exists; whichever fills first closes it and cancels the other (one-cancels-all).
Order
A submitted order, before it fills. Replaces any pending order with the same id, as Pine’s order commands do.
PineFills
TradingView’s default assumptions:
Position
The current net position: signed size and the average price it was opened at.
Trade
One trade: an entry, and its exit once closed. size is signed — positive is long, negative short — matching strategy.*trades.size.

Enums§

Commission
How a strategy declaration charges commission.
Direction
Long or short.
EntryFilter
Which entry directions strategy.risk.allow_entry_in permits.
OcaType
What happens to the other orders in a One-Cancels-All group when one of them fills, from strategy.oca.*.
OrderKind
The price condition that decides when an order fills.
RiskRule
A risk-management rule set by a strategy.risk.* call, applied to the broker.
RiskType
How a risk threshold’s value is measured (strategy.risk.max_drawdown and strategy.risk.max_intraday_loss).
Sizing
How an order without an explicit qty is sized, from the strategy declaration’s default_qty_type/default_qty_value.

Traits§

Broker
The simulated broker a strategy trades against.
BrokerFactory
Builds the Broker a strategy trades against. The default, DefaultBrokerFactory, produces the built-in bar-fill broker; a host can supply its own to simulate against a different engine while still honouring the script’s BrokerConfig.
FillModel
Decides whether order fills against bar, returning the fill price.