Places an order to open or add to a position; an opposite entry reverses the position.
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
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)) -> voidArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id | series string | required | A 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. |
| direction | series strategy_direction | required | The side of the order: strategy.long to buy or strategy.short to sell. |
| qty | series int/ | the strategy's default quantity | How many contracts, shares or units to trade. When na or left out, default_qty_type and default_qty_value from the strategy() header decide. |
| limit | series int/ | na | A limit price: a buy fills at this price or lower, a sell at this price or higher. |
| stop | series int/ | na | A stop price: a buy triggers when the price rises to it, a sell when the price falls to it. |
| oca_name | series string | an empty string | The name of an OCA (one cancels all) group. Orders sharing a name are linked as oca_type says. |
| oca_type | input string | strategy. | How 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. |
| comment | series string | an empty string | Text shown next to the order on the chart and in the list of trades. |
| alert_message | series string | an empty string | Text sent as {{strategy.order.alert_message}} in an alert created on the strategy when this order fills. |
| disable_alert | series bool | false | When 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
//@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)//@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