Home » Strategy Functions » Understanding the strategy.opentrades.entry_price() Function in Pine Script

Understanding the strategy.opentrades.entry_price() Function in Pine Script

Photo of author
Published on

In this article, we delve into the functionality and application of the strategy.opentrades.entry_price() function in Pine Script, an essential tool for developing and backtesting trading strategies on the TradingView platform. 


The strategy.opentrades.entry_price() function is structured as follows:

strategy.opentrades.entry_price(trade_num) → series float
  • trade_num (series int): Identifies the open trade number, where the first trade is numbered zero. This parameter allows you to specify which open trade’s entry price you’re interested in.

Example Use Cases

To illustrate the strategy.opentrades.entry_price() function’s versatility, we present two examples, each modified for uniqueness and clarity.

Example 1: Triggering Exit Based on Entry Price
strategy("My Entry Price Strategy Example", overlay = true)

// Strategy to enter long positions on a moving average crossover and exit based on a price increase.
if ta.crossover(close, ta.sma(close, 13))
    strategy.entry("MyLongPosition", strategy.long)

// Calculate the entry price for the latest open trade and define the exit price as a 5% increase.
latestEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
plannedExitPrice = latestEntryPrice * 1.05

// Close the position when the high price reaches or exceeds the planned exit price.
if high >= plannedExitPrice

plot(latestEntryPrice, "Entry Price", style = plot.style_linebr)
plot(plannedExitPrice, "Exit Price", color.green, style = plot.style_linebr)
Example 2: Calculating the Average Entry Price for Open Positions
strategy("Average Entry Price Strategy", pyramiding = 2)

// Enter long positions periodically and close them after a certain number of bars.
if bar_index % 14 == 0
    strategy.entry("PositionEntry", strategy.long)
if bar_index % 19 == 0

// Define a function to calculate the average entry price of all open positions.
averageEntryPrice() =>
    totalEntryPrice = 0.0
    for posNum = 0 to strategy.opentrades - 1
        totalEntryPrice += strategy.opentrades.entry_price(posNum) * strategy.opentrades.size(posNum) / strategy.position_size
    averagePrice = nz(totalEntryPrice / strategy.opentrades)



  • Example 1 demonstrates how to use the entry price of the most recent open trade to determine a strategy’s exit point. It employs a simple moving average crossover as a condition to enter long positions. The exit strategy is set to trigger once the price reaches 5% above the entry price.
  • Example 2 showcases a method to calculate the average entry price of all open positions within a strategy that allows pyramiding. This approach is particularly useful for strategies involving multiple concurrent trades.

Key Features and Takeaways

  • Functionality: The strategy.opentrades.entry_price() function is pivotal for accessing the entry price of open trades, enabling dynamic strategy adjustments based on entry price data.
  • Flexibility: It supports strategies with single or multiple open trades by iterating over open trades to calculate metrics like the average entry price.
  • Usability: This function is essential for creating more sophisticated trading strategies that adapt to changing market conditions based on the price at which positions were opened.

Understanding and implementing the strategy.opentrades.entry_price() function can significantly enhance the complexity and effectiveness of your trading strategies on TradingView.

Leave a Comment