Skip to content

Get an open order's entry price in TradingView's Pine Script

Get an open order’s entry price in TradingView’s Pine Script

Section titled “Get an open order’s entry price in TradingView’s Pine Script”

TL;DR
Use strategy.opentrades.entry_price(trade_num) to read the fill price of any currently open trade and react instantly.

DifficultyBeginner
Time to implement10-15 min
CategoryStrategy Basics
//@version=5
strategy("Entry price demo", overlay=true, pyramiding=2)
if ta.crossover(close, ta.sma(close, 20))
strategy.entry("Long", strategy.long)
if strategy.opentrades > 0
lastEntry = strategy.opentrades.entry_price(strategy.opentrades - 1)
plot(lastEntry, "Last entry", color=color.fuchsia, style=plot.style_circles)
Tip. Trade numbers are zero-indexed: use `0` for the oldest open trade and `strategy.opentrades - 1` for the most recent fill.

TradingView strategies expose aggregated metrics like average position price, but sometimes you need the exact entry price of each open order—for example to draw break-even lines, size partial exits, or emit alerts when price drifts away from the latest fill. The strategy.opentrades namespace gives you that granular access.

  • How to query entry prices for specific open trades
  • Looping over all open trades to calculate custom metrics
  • Guarding against empty portfolios (when no trades are open)
  • Visualising entry prices on the chart for debugging
CallPurpose
strategy.opentradesReturns the number of currently open trades.
strategy.opentrades.entry_price(trade_num)Provides the entry price of the selected open trade.
strategy.opentrades.entry_bar_index(trade_num)Helps detect new fills on the current bar.
strategy.position_avg_priceBuilt-in average price helper (useful fallback).
strategy.entry() / strategy.close()Manage trades whose entry prices you want to monitor.
  1. Check that trades are open
    Always confirm strategy.opentrades > 0 before accessing entry prices to avoid na values.

    hasTrades = strategy.opentrades > 0
  2. Select the trade index you need
    Use 0 for the oldest trade, strategy.opentrades - 1 for the newest, or loop for all trades.

    newestEntry = strategy.opentrades.entry_price(strategy.opentrades - 1)
    firstEntry = strategy.opentrades.entry_price(0)
  3. React to the price (plot, label, exit logic)
    Once you have the entry price, display it or feed it into risk checks.

    if hasTrades
    plot(newestEntry, "Newest entry", color=color.fuchsia, style=plot.style_cross)
    breakeven = newestEntry * 0.99
    strategy.exit("Protect", from_entry="Long", stop=breakeven)
//@version=5
strategy("Open trade entry marker", overlay=true, pyramiding=3, calc_on_order_fills=true)
fast = ta.ema(close, 20)
slow = ta.sma(close, 50)
plot(fast, "Fast", color=color.orange)
plot(slow, "Slow", color=color.blue)
if ta.crossover(fast, slow)
strategy.entry("Trend", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Trend")
var lastFillBar = na
if strategy.opentrades > 0
idx = strategy.opentrades - 1
entryPrice = strategy.opentrades.entry_price(idx)
entryBar = strategy.opentrades.entry_bar_index(idx)
plot(entryPrice, "Entry price", color=color.new(color.purple, 0), style=plot.style_circles)
// Detect a fresh fill to drop a label once per entry
if entryBar == bar_index and entryBar != lastFillBar
label.new(bar_index, entryPrice, "Filled @ " + str.tostring(entryPrice, format.mintick))
alert("New entry at " + str.tostring(entryPrice, format.mintick))
lastFillBar := entryBar
Why this works.
  • The fill callback (`calc_on_order_fills=true`) ensures entry prices update immediately after trades execute.
  • Plotting the price provides visual confirmation of the latest fill versus live action.
  • The `lastFillBar` guard prevents repeated labels/alerts on the same bar.
  • strategy.opentrades.entry_price() returns na when no trades are open—guard with strategy.opentrades > 0.
  • Pair with strategy.opentrades.entry_bar_index() to confirm whether a trade just filled.
  • For average cost across all entries, consider strategy.position_avg_price unless you need per-trade detail.
  • When pyramiding, iterate over all trades to apply tiered exits or scaling logic.

The function only returns a number when the strategy currently holds a trade. Use an if strategy.opentrades > 0 guard before accessing entry prices.

The entry price reflects the fill price regardless of direction. You can inspect strategy.opentrades.entry_direction(trade_num) if you need to treat longs and shorts differently.

  • strategy.opentrades.entry_price(trade_num) gives you per-trade fill prices for open positions.
  • Trade indices are zero-based; loop when multiple entries are active.
  • Combine entry prices with plotting, alerts, or exit logic for richer strategy feedback.
  • Guard against empty positions to avoid na and runtime alerts.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.