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

Understanding the strategy.default_entry_qty() Function in Pine Script

Photo of author
Published on

In this article, we delve into the strategy.default_entry_qty() function. This function is pivotal for traders looking to automate their strategies, particularly in calculating the default order quantity based on the strategy’s settings and the specified fill price. Let’s break down its syntax, arguments, and practical application through an example.

Syntax

The syntax for strategy.default_entry_qty() is straightforward:

strategy.default_entry_qty(fill_price) → series float

It accepts a fill_price argument, which is the price at which the order is expected to be filled, and returns a float that represents the default quantity of the entry order.

Arguments

  • fill_price (series int/float): The anticipated fill price for the order. It is the price level at which you expect the entry order to be executed.

Example

Consider a trading strategy named “Enhanced Supertrend Strategy” that dynamically calculates the quantity of an entry order based on the current market conditions. This strategy utilizes the Average True Range (ATR) to determine stop levels and dynamically calculates entry quantities.

//@version=5
strategy("Enhanced Supertrend Strategy", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 15)

//@variable The period for ATR calculation.
atrDuration = input(10, "ATR Duration")
//@variable The multiplier for ATR.
multiplier = input.float(3.0, "Multiplier", step = 0.01)
//@variable The tick offset for the stop order.
stopTickOffset = input.int(100, "Tick offset for stop entry")

// Calculate the SuperTrend direction.
[_, trendDirection] = ta.supertrend(multiplier, atrDuration)

if ta.change(trendDirection) < 0
    //@variable The stop price for the entry order.
    entryStopPrice = close + syminfo.mintick * stopTickOffset
    //@variable The calculated default fill quantity at `entryStopPrice`. This might differ from the actual filled order quantity.
    defaultQty = strategy.default_entry_qty(entryStopPrice)
    strategy.entry("Long Position ID", strategy.long, stop = entryStopPrice)
    label.new(bar_index, entryStopPrice, str.format("Stop set at {0}\nDefault qty at {0}: {1}", math.round_to_mintick(entryStopPrice), defaultQty))

if ta.change(trendDirection) > 0
    strategy.close_all()
Example

Line-by-Line Explanation

  • strategy settings: The strategy is named “Enhanced Supertrend Strategy” with a percentage of equity as the default quantity type and 15% of equity as the default quantity value.
  • atrDuration, multiplier, stopTickOffset: These lines define user inputs for the ATR period, ATR multiplier, and the tick offset for stop orders.
  • Calculating SuperTrend: Utilizes the ta.supertrend() function to determine the market’s trend direction.
  • Entry Order Calculation: When the trend direction changes negatively, it calculates the stop price for a long position and uses strategy.default_entry_qty() to compute the expected default order quantity.
  • Strategy Entry and Label: Places a long entry with a dynamic stop price and annotates the chart with the stop price and expected order quantity.

Key Features and Takeaways

  • Function Usefulness: The strategy.default_entry_qty() function is invaluable for dynamically adjusting order quantities based on strategy settings and market price, ensuring a more tailored and efficient trade execution.
  • Syntax and Application: Simple in syntax, this function requires only the fill price to calculate the default entry quantity, making it easy to integrate into various trading strategies.
  • Flexibility: While it provides a default quantity, traders can override this in specific orders by specifying a different quantity, offering flexibility in order management.

This function is a critical component of Pine Script for traders aiming to automate and optimize their strategies on TradingView. By understanding and applying the strategy.default_entry_qty() function, traders can more effectively manage their entry sizes relative to their strategy’s parameters and market conditions, leading to potentially improved trading outcomes.

Leave a Comment