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

Understanding the strategy.risk.allow_entry_in() Function in Pine Script

Photo of author
Published on

In this article, we’ll delve into the details of the strategy.risk.allow_entry_in() function, showcasing its syntax, usage, and practical application through a Pine Script example.

Introduction to the Function


The syntax of the strategy.risk.allow_entry_in() function is straightforward and is outlined as follows:

strategy.risk.allow_entry_in(value) → void


  • value (simple string): This argument specifies the allowed direction for opening positions. It can take one of the following values:
  • strategy.direction.all: Allows entries in both long and short directions.
  • strategy.direction.long: Only allows long entries.
  • strategy.direction.short: Only allows short entries.


Let’s analyze a practical example to understand how the strategy.risk.allow_entry_in() function can be effectively utilized within a trading strategy:

strategy("Custom Directional Entry Strategy")


if open > close
    strategy.entry("EntryLong", strategy.long)

if open < close
    strategy.entry("EntryShort", strategy.short, qty = 10)

In this example, we’ve created a strategy titled “Custom Directional Entry Strategy” that utilizes the strategy.risk.allow_entry_in() function to permit only long entries. Here’s a breakdown of the code:

  • We declare the version of Pine Script being used with //@version=5 and give our strategy a title.
  • The strategy.risk.allow_entry_in(strategy.direction.long) command is used to restrict the strategy to only allow long positions to be opened.
  • A conditional statement checks if the opening price is greater than the closing price. If true, a long entry labeled “EntryLong” is initiated.
  • Another conditional statement checks if the opening price is less than the closing price. Despite attempting to open a short position with 10 contracts, this entry will be ignored because we have restricted the strategy to only allow long positions.

Key Features and Takeaways

  • Function Usability: The strategy.risk.allow_entry_in() function is crucial for creating strategies that are tailored to specific market conditions, enhancing both flexibility and precision.
  • Syntax and Application: The function’s syntax is simple, requiring only one argument to specify the allowed direction of trade entries, making it straightforward to apply within any strategy.
  • Practical Use Case: Our example demonstrates the function’s effectiveness in restricting the strategy to only execute long entries, illustrating how it can be applied to meet specific strategy requirements.

Through this detailed walkthrough, it’s clear that the strategy.risk.allow_entry_in() function is an invaluable tool in Pine Script, offering strategic control over the direction of trade entries. By understanding and utilizing this function, developers and traders can craft more refined and directionally focused trading strategies.

Leave a Comment