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
Usestrategy.opentrades.entry_price(trade_num)to read the fill price of any currently open trade and react instantly.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Strategy Basics |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5strategy("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)Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- 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
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
strategy.opentrades | Returns 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_price | Built-in average price helper (useful fallback). |
strategy.entry() / strategy.close() | Manage trades whose entry prices you want to monitor. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Check that trades are open
Always confirmstrategy.opentrades > 0before accessing entry prices to avoidnavalues.hasTrades = strategy.opentrades > 0 -
Select the trade index you need
Use0for the oldest trade,strategy.opentrades - 1for the newest, or loop for all trades.newestEntry = strategy.opentrades.entry_price(strategy.opentrades - 1)firstEntry = strategy.opentrades.entry_price(0) -
React to the price (plot, label, exit logic)
Once you have the entry price, display it or feed it into risk checks.if hasTradesplot(newestEntry, "Newest entry", color=color.fuchsia, style=plot.style_cross)breakeven = newestEntry * 0.99strategy.exit("Protect", from_entry="Long", stop=breakeven)
Example Playbook
Section titled “Example Playbook”//@version=5strategy("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- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”strategy.opentrades.entry_price()returnsnawhen no trades are open—guard withstrategy.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_priceunless you need per-trade detail. - When pyramiding, iterate over all trades to apply tiered exits or scaling logic.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”Why do I get na values?
Section titled “Why do I get na values?”The function only returns a number when the strategy currently holds a trade. Use an if strategy.opentrades > 0 guard before accessing entry prices.
How do I handle short positions?
Section titled “How do I handle short positions?”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.
Key Takeaways
Section titled “Key Takeaways”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
naand runtime alerts.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.