OPEN-SOURCE SCRIPT

Risk Management: Position Size & Risk Reward

Updated
Here is a Risk Management Indicator that calculates stop loss and position sizing based on the volatility of the stock. Most traders use a basic 1 or 2% Risk Rule, where they will not risk more than 1 or 2% of their capital on any one trade. I went further and applied four levels of risk: 0.25%, 0.50%, 1% and 2%. How you apply these different levels of risk is what makes this indicator extremely useful. Here are some common ways to apply this script:
• If the stock is extremely volatile and has a better than 50% chance of hitting the stop loss, then risk only 0.25% of your capital on that trade.
• If a stock has low volatility and has less than 20% change of hitting the stop loss, then risk 2% of your capital on that trade.
• Risking anywhere between 0.25% and 2% is purely based on your intuition and assessment of the market.
• If you are on a losing streak and you want to cut back on your position sizing, then lowering the Risk % can help you weather the storm.
• If you are on a winning streak and your entries are experiencing a higher level of success, then gradually increase the Risk % to reap bigger profits.
• If you want to trade outside the noise of the market or take on more noise/risk, you can adjust the ATR Factor.
• … and whatever else you can imagine using it to benefit your trading.

The position size is calculated using the Capital and Risk % fields, which is the percentage of your total trading capital (a.k.a net liquidity or Capital at Risk). If you instead want to calculate the position size based on a specific amount of money, then enter the amount in the Custom Risk Amt input box. Any amount greater than 0 in the Custom Risk Amt field will override the values in the Capital and Risk % fields.

The stop loss is calculated by using the ATR. The default setting is the 14 RMA, but you can change the length and smoothing of the true range moving average to your liking. Selecting a different length and smoothing affects the stop loss and position size, so choose these values very carefully.

The ATR Factor is a multiplier of the ATR. The ATR Factor can be used to adjust the stop loss and move it outside of the market noise. For the more volatile stock, increase the factor to lower the stop loss and reduce the chance of getting stopped out. For stocks with less volatility , you can lower the factor to raise the stop loss and increase position size. Adjusting the ATR Factor can also be useful when you want the stop loss to be at or below key levels of support.

The Market Session is the hours the market is open. The Market Session only affects the Opening Range Breakout (ORB) option, so it’s important to change these values if you’re trading the ORB and you’re outside of Eastern Standard Time or you’re trading in a foreign exchange.

The ORB is a bonus to the script. When enabled, the indicator will only appear in the first green candle of the day (09:30:00 or 09:30 AM EST or the start time specified in Market Session). When using the ORB, the stop loss is based on the spread of the first candle at the Open. The spread is the difference between the High and Low of the green candle. On 1-day or higher timeframes, the indicator will be the spread of the last (or current) candle.

The output of the indicator is a label overlaying the chart:
1. ATR (14 RMA x2) – This indicated that the stop loss is determined by the ATR. The x2 is the ATR Factor. If ORB is selected, then the first line will show SPREAD, instead of ATR.
2. Capital – This is your total capital or capital at risk.
3. Risk X% of Capital – The amount you’re risking on a % of the Capital. If a Custom Risk Amt is entered, then Risk Amount will be shown in place of Capital and Risk % of Capital.
4. Entry – The current price.
5. Stop Loss – The stop loss price.
6. -1R – The stop loss price and the amount that will be lost of the stop loss is hit.
7. – These are the target prices, or levels where you will want to take profit.

This script is primarily meant for people who are new to active trading and who are looking for a sound risk management strategy based on market volatility . This script can also be used by the more experienced trader who is using a similar system, but also wants to see it applied as an indicator on TradingView. I’m looking forward to maintaining this script and making it better in future revisions. If you want to include or change anything you believe will be a good change or feature, then please contact me in TradingView.
Release Notes
You can now enter a specific stop price. The position size will be based on the spread between the close (or entry) and the Stop Price.
Release Notes
When trading based on risk/reward, you might prefer to take profit when the stock reaches a certain percentage increase. If you want to make 5% of the money you spent buying the stock (cost basis), then the stock will need to increase by 5% from your entry price. For example, a 1.00 entry will need to hit 1.05 for you to make 5%. If you bought 1000 shares of this 1.00 stock, then you will make 50.00 when the price hits 1.05.

If you prefer to use percentage of your cost basis to determine the price targets, then this update is for you! The updated script will show you price targets set at 1%, 5%, 10% and 20% of the cost basis. The script also allows you to enter a custom percentage, in case you want to know the target price for a specific percentage of the cost basis. If you select 'Show price targets of cost basis' and enter an amount greater than 0.0 in the 'Percentage Of Cost Basis' field, then the label will show the price target for that percentage.

Other changes:
- Renamed Targets to Price Targets.
- Removed the -1R line, since it was the same as the Stop Loss.
Release Notes
Major revamp of the dialog box! I've utilized the new features of Pine Script to make the dialog box more intuitive and user friendly. The update includes tooltips to explain the purpose of individual fields, headers to group input fields, and inlining ATR fields to save space. In addition to updating to the dialog box, I've made the following changes:

- Rounded positionSize variable, so that all risk and price target calculations are consistent.
- Added 'Cost Basis' line to the label, in order to show what it will cost if you were to buy using stated position size and current price.
- Changed 'Entry' to 'Current Price' in the label.

Although it wasn't my intention to write this script solely for buyers, I do realize this limitation and I plan to update the script so that it can also be used by short sellers. Keep an eye on the next update!
Release Notes
Now short sellers can use the Risk Management script to calculate position size and price targets when going short. When you select 'Are you short selling?' option in the dialog box, the label will change to the following:
  • Label color will turn red
  • Stop loss will be above the current price
  • Price targets will be below the current price


When choosing to short, the Price target can go no lower than 0.00. If that's the case, then the profit at 0.00 will be shown.

Also, the script now has an error condition to check if custom stop loss is greater than the current price for short and less than the current price when long. Price targets will not show when this error condition returns true.

This script is now in its fifth and almost final revision. Pine script doesn't provide the ability to do any unit testing, so adding more functionality will make the script unwieldy and more difficult to maintain. I'm planning to remove the Opening Range Breakout feature in the next revision, in order to improve the maintainability of the code. However, add a comment below to let me know if this is something you would like to keep in the script.
Release Notes
Detached Risk Management label from the last price bar and moved it to the bottom right of the chart screen, while will allow you to focus more on the price action and other indicators.
Release Notes
When choosing a higher Risk %, there is the possibility of having a Cost Basis higher than your Capital or real cash. This usually occurs when the ATR produces a tight stop loss, or when the Stop Price entered in settings is relatively close to the Close or entry price. For traders who use margin, there is a benefit to seeing a higher Cost Basis, since margin accounts allow traders to trade with more than the cash in the account. For traders with real cash (non-margin) accounts, having a Cost Basis greater than Capital makes no sense, so it's better to lower the Stop Price and have a wider spread between the Close and stop price.
ATRpositionsizingriskmanagementstrategystoplossVolatility Stop

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Also on:

Disclaimer