strategy.entry() - Pine Script Function
strategy.entry()
Section titled “strategy.entry()”Overview
Section titled “Overview”Creates a new order to open or add to a position. If an unfilled order with the same id exists, a call to this command modifies that order.
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 | The identifier of the order |
| which | — | corresponds to an entry ID in the strategy’s trades after the order fills. If the strategy opens a new position after filling the order |
| the | — | order’s ID becomes the strategy.position_entry_name value. Strategy commands can reference the order ID to cancel or modify pending orders and generate exit orders for specific open trades. The Strategy Tester and the chart display the order ID unless the command specifies a comment value. |
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6strategy("Limit order strategy", overlay=true, margin_long=100, margin_short=100)//@variable The distance from the `close` pricefor each limit order.float limitOffsetInput =input.int(100, "Limit offset, in ticks", 1) * syminfo.mintick//@function Draws a label and line at the specified `price` to visualize a limit order's level.drawLimit(float price, bool isLong) => color col = isLong ? color.blue : color.red label.new( bar_index, price, (isLong ? "Long" : "Short") + " limit order created", style = label.style_label_right, color = col, textcolor = color.white )line.new(bar_index, price, bar_index + 1, price, extend = extend.right, style = line.style_dashed, color = col)//@function Stops the `l` line from extending further.method stopExtend(line l) => l.set_x2(bar_index)l.set_extend(extend.none)// Initialize two `line` variables to reference limit line IDs.var line longLimit = navar line shortLimit = na// Calculate a 14-bar and 28-bar moving average of `close` prices.float sma14 = ta.sma(close, 14)float sma28 = ta.sma(close, 28)if ta.crossover(sma14, sma28) // Cancel any unfilled sell orders with the specified ID.strategy.cancel("My Short Entry ID") //@variable The limit price level. Its value is `limitOffsetInput` ticks below the current `close`. float limitLevel = close - limitOffsetInput // Place a long limit order to close the short trade and enter a long position at the `limitLevel`.strategy.entry("My Long Entry ID", strategy.long, limit = limitLevel) // Make new drawingsfor the long limit and stop extending the `shortLimit` line. longLimit :=drawLimit(limitLevel, isLong = true)shortLimit.stopExtend()if ta.crossunder(sma14, sma28) // Cancel any unfilled buy orders with the specified ID.strategy.cancel("My Long Entry ID") //@variable The limit price level. Its value is `limitOffsetInput` ticks above the current `close`. float limitLevel = close + limitOffsetInput // Place a short limit order to close the long trade and enter a short position at the `limitLevel`.strategy.entry("My Short Entry ID", strategy.short, limit = limitLevel) // Make new drawingsfor the short limit and stop extending the `shortLimit` line. shortLimit :=drawLimit(limitLevel, isLong = false)longLimit.stopExtend()