strategy.entry() - Pine Script Function
Overview
Section titled “Overview”Creates an order that enters a position. A call creates a market order when neither
limit nor stop is supplied. It can also create limit, stop, and stop-limit
orders. Calls are affected by the strategy’s pyramiding setting and can reverse an
open position in the opposite direction.
Syntax
Section titled “Syntax”strategy.entry(id, direction, qty, limit, stop, oca_name, oca_type, comment, alert_message, disable_alert) → voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
id | series string | Order identifier. Reusing an ID can modify an unfilled order with that ID. |
direction | strategy_direction | strategy.long or strategy.short. |
qty | series int/float | Optional order size. When omitted, the strategy’s default order-size settings apply. |
limit | series int/float | Optional limit price. With no stop, creates a limit order. |
stop | series int/float | Optional stop price. With no limit, creates a stop order; with limit, creates a stop-limit order. |
oca_name | series string | Optional One-Cancels-All group name. |
oca_type | input string | Optional OCA behavior such as strategy.oca.none, strategy.oca.cancel, or strategy.oca.reduce. |
comment | series string | Optional label shown instead of the order ID in strategy output. |
alert_message | series string | Optional custom message for an order-fill alert. Use {{strategy.order.alert_message}} in TradingView’s alert Message field. |
disable_alert | series bool | When true, suppresses the order-fill alert for the order created by this call. |
Minimal example
Section titled “Minimal example”//@version=6strategy("Moving-average entries", overlay = true, pyramiding = 1)
float fast = ta.sma(close, 14)float slow = ta.sma(close, 28)
if ta.crossover(fast, slow) strategy.entry("Long", strategy.long, qty = 1)
if ta.crossunder(fast, slow) strategy.entry("Short", strategy.short, qty = 1)
plot(fast, "Fast", color.aqua)plot(slow, "Slow", color.orange)Remarks
Section titled “Remarks”- An entry in the opposite direction reverses the open position by default. The transaction size can therefore be larger than the requested resulting position.
- The strategy’s
pyramidingproperty controls how many entries created bystrategy.entry()can be open in the same direction. - A priced order can remain unfilled. Creating an order is not the same as the broker emulator filling it.
- To exit without reversing, use
strategy.exit(),strategy.close(), orstrategy.close_all()as appropriate.