Stop loss and Take Profit in $$ example
Dec 23, 2020

The Stop loss and Take Profit in $$ example tool provides a practical demonstration of how to implement currency-based exit conditions within a Pine Script strategy, allowing traders to define risk and reward in absolute monetary terms regardless of position size.
Usage
The Usage section describes how the script can be used to manage trades based on specific financial targets. This strategy uses a simple Moving Average crossover as its entry signal: a long position is opened when the 14-period SMA crosses above the 28-period SMA, and a short position is opened when it crosses below.
Once a trade is active, the script applies monetary exit logic:
- Targeting Profits: Users can input a specific dollar amount (e.g., $200) they wish to gain. The script calculates the exact price level required to reach this profit based on the current position size.
- Managing Risk: Users set a maximum dollar amount they are willing to lose (e.g., $100). The script automatically determines the price level for the stop loss.
The script dynamically adjusts the exit price levels if the position size changes, ensuring the monetary risk remains constant. Visual lines and colored fills are displayed on the chart to indicate the active Take Profit and Stop Loss zones relative to the average entry price.
Details
The script executes currency-based exits by converting a monetary value into ticks/points, which are then passed to the strategy.exit function. This is achieved through a custom function that divides the target money amount by the symbol's point value and the absolute value of the current position size.
The implementation relies on syminfo.pointvalue and syminfo.mintick to ensure compatibility across various asset classes, including stocks, forex, and futures. By calculating the exit levels in ticks rather than static price points, the strategy maintains its mathematical accuracy across different instruments. The visual components use plot.style_linebr to ensure that levels are only drawn when a position is active, preventing chart clutter during periods without active trades.
Settings
- Take Profit $$: Specifies the target profit amount in the currency of the instrument being traded.
- Stop Loss $$: Specifies the maximum loss amount in the currency of the instrument being traded.
FAQ
How does the script calculate exit levels for different position sizes?
The script utilizes the strategy.position_size variable to scale the exit points. If the position size increases, the distance to the monetary stop loss or take profit price levels will narrow to maintain the exact dollar value defined in the settings.
Does this work on all symbols?
Yes, the script is designed to be universal. It uses syminfo.pointvalue to determine the value of a single point for the specific asset on the chart, making it applicable to stocks, crypto, forex, and commodities.
How can I access the Stop loss and Take Profit in $$ example?
You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Free indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.