Sidechair

strategy.entry()

Browse the reference

Places an order to open or add to a position; an opposite entry reverses the position.

Try it in the editor

strategy.entry(id, direction, qty) is the usual way to open a trade. The order is created when the line runs, and it fills on the next bar: a market order at that bar's open (plus slippage), a limit or stop order when the price reaches its level. With process_orders_on_close = true in the strategy() header, a market order fills at the close of the bar that created it.

An entry in the opposite direction of the open position reverses it: the emulator closes the existing position and opens the new one with a single order, so a long of 1 followed by a short entry of 1 ends as a short of 1 and the trade list shows both the exit and the new entry. Entries in the same direction pile up only up to the pyramiding limit in the header (0 allows one open entry per direction), and further orders are ignored.

Calling strategy.entry again with the id of a pending order replaces that order, which is how a script moves a limit price from bar to bar. Pending orders stay in force until they fill or are cancelled with strategy.cancel().

Syntax

Pine Script
strategy.entry(id: series string, direction: series strategy_direction, qty: series int/float (optional), limit: series int/float (optional), stop: series int/float (optional), oca_name: series string (optional), oca_type: input string (optional), comment: series string (optional), alert_message: series string (optional), disable_alert: series bool (optional)) -> void

Arguments

NameTypeDefaultDescription
idseries stringrequiredA name for the order, used by strategy.exit(), strategy.close() and strategy.cancel() to find it and shown on the chart and in the trade list.
directionseries strategy_directionrequiredThe side of the order: strategy.long to buy or strategy.short to sell.
qtyseries int/floatthe strategy's default quantityHow many contracts, shares or units to trade. When na or left out, default_qty_type and default_qty_value from the strategy() header decide.
limitseries int/floatnaA limit price: a buy fills at this price or lower, a sell at this price or higher.
stopseries int/floatnaA stop price: a buy triggers when the price rises to it, a sell when the price falls to it.
oca_nameseries stringan empty stringThe name of an OCA (one cancels all) group. Orders sharing a name are linked as oca_type says.
oca_typeinput stringstrategy.oca.noneHow orders in the group interact: strategy.oca.cancel cancels the others when one fills, strategy.oca.reduce shrinks the others by the filled size, strategy.oca.none leaves them independent.
commentseries stringan empty stringText shown next to the order on the chart and in the list of trades.
alert_messageseries stringan empty stringText sent as {{strategy.order.alert_message}} in an alert created on the strategy when this order fills.
disable_alertseries boolfalseWhen true, an alert on the strategy does not fire for this order's fill.

Returns

Nothing.

Remarks

  • Pass both limit and stop for a stop-limit order: the stop triggers first, then the limit order is placed.
  • Limit and stop orders are tested along the bar's price path: open, then the extreme closer to the open, then the other extreme, then close. A gap that jumps past the level fills at the open instead of at the level.
  • Commission and slippage from the strategy() header are applied to every fill. Slippage worsens the fill price by the given number of ticks.
  • Only available in strategy scripts. Used in an indicator it is an error.
  • On a live bar, orders are recalculated at bar close unless calc_on_every_tick is true in the strategy() header, so a script's orders do not repaint within a bar by default.

Examples

Pine ScriptMoving average crossover, long and shortOpen in editor
//@version=6
strategy("SMA cross", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
plot(fast, "Fast", color.green)
plot(slow, "Slow", color.red)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)
Pine ScriptLimit entry that follows the priceOpen in editor
//@version=6
strategy("Pullback limit", overlay = true)
trend = ta.sma(close, 50)
limitPrice = low[1]
plot(trend, "Trend", color.blue)
if close > trend
    strategy.entry("Pullback", strategy.long, limit = limitPrice, comment = "buy the dip")
else
    strategy.cancel("Pullback")

See also

Runs on Sidechair