Supertrend / Position Sizing
Mar 15, 2021

The Supertrend / Position Sizing indicator provides a classic trend-following mechanism enhanced with an integrated risk management module to calculate optimal trade sizes automatically. By utilizing the Supertrend line as a dynamic stop loss, the tool helps traders determine exactly how many units to purchase based on their account capital, risk tolerance, and transaction commissions.
Usage
The indicator can be used to identify trend direction and potential entry points while simultaneously managing risk. When the price moves above the upper band, a green "Buy" signal and line appear, indicating an uptrend. Conversely, when the price drops below the lower band, a red "Sell" signal and line appear, indicating a downtrend.
The unique value of this tool lies in the real-time label displayed on the far right of the chart. This label provides:
- Affordable Quantity: The number of units or shares you can purchase based on your defined risk percentage and available capital.
- Stop Loss Level: The exact price level of the current Supertrend line to be used for your stop loss order.
Details
This script is built upon the foundational Supertrend logic originally popularized by Kivanc Ozbilgic. It calculates the Average True Range (ATR) to create bands around the price. The core addition in this version is the suggestQuantity function. This function calculates position size by evaluating the distance between the current price and the Supertrend line (acting as the stop loss), then factoring in your total capital, the percentage of capital you are willing to lose, and the impact of entry/exit commissions.
The logic ensures that the suggested volume never exceeds your total available capital, providing a realistic "max affordable" quantity even if your risk parameters would theoretically allow for a larger position.
Settings
Supertrend Settings
- ATR Period: Sets the lookback period for the Average True Range calculation.
- Source: Determines which price data (e.g., HL2, Close) is used for the calculation.
- ATR Multiplier: Sets the sensitivity of the bands; higher values result in fewer signals.
- Change ATR Calculation Method?: Toggles between the standard TradingView ATR and a Simple Moving Average based TR.
- Show Buy/Sell Signals?: Toggles the visibility of the "Buy" and "Sell" labels on the chart.
- Highlighter On/Off?: Toggles the background shading between the price and the trend line.
- Ignore the wicks?: If enabled, the trend changes based on closing prices rather than high/low price breaches.
Risk Management Settings
- Capital: Your total account balance used for the calculation.
- Max Loss Percentage Per Trade: The maximum percentage of your total capital you are willing to lose on a single trade.
- Commission Percentage Per Transaction: The percentage fee charged by your broker for both buying and selling.
FAQ
How do I access Supertrend / Position Sizing?
You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Can I use this for crypto and stocks?
Yes, the position sizing logic is mathematical and applies to any asset class as long as you input your correct capital and commission structures in the settings.
Why is the suggested quantity lower than I expected?
The tool accounts for the "Value Loss Per Share" plus the "Total Commission To Pay Per Share." If your commissions are high or your stop loss is very wide, the affordable quantity will decrease to keep your total risk within your specified limit.
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.