Risk Management and Positionsize - MACD example

Mastering Risk Management
Risk management is the cornerstone of successful trading, and it's often the difference between turning a profit and suffering a loss. In light of its importance, I share a risk management tool which you can use for your trading strategies. The script not only assists in position sizing but also comes with built-in technical features that help in market timing. Let's delve into the nitty-gritty details.

Input Parameter: MarginFactor
One of the key features of the script is the MarginFactor input parameter. This element lets you control the portion of your equity used for placing each trade. A MarginFactor of -0.5 means 50% of your total equity will be deployed in placing the position size. Although Tradingview has a built-in option to adjust position sizing in a same way, I personally prefer to have the logic in my pinecode script. The main reason is userexperience in managing and testing different settings for different charts, timeframes and instruments (with the same strategy).

Stoploss and MarginFactor
If your strategy has a 4% stop-loss, you can choose to use only 50% of your equity by setting the MarginFactor to -0.5. In this case, you are effectively risking only 2% of your total capital per trade, which aligns well with the widely-accepted rule of thumb suggesting a 1-2% risk per trade. Similar if your stoploss is only 1% you can choose to change the MarginFactor to 1, resulting in a positionsize of 200% of your equity. The total risk would be again 2% per trade if your stoploss is set to 1%.

Max Drawdown and MarginFactor
Your MarginFactor setting can also be aligned with the maximum drawdown of your strategy, seen during a backtested period of 2-3 years. For example, if the max drawdown is 15%, you could calibrate your MarginFactor accordingly to limit your risk exposure.

Option to Toggle Number of Contracts
The script offers the option to toggle between using a percentage of equity for position sizing or specifying a fixed number of contracts. Utilizing a percentage of equity might yield unrealistic backtest results, especially over longer periods. This occurs because as the capital grows, the absolute position size also increases, potentially inflating the accumulated returns generated by the backtester. On the other hand, setting a fixed number of contracts as your position size offers a more stable and realistic ROI over the backtested period, as it removes the compounding effect on position sizes.

Key Features Strategy
MACD High Time Frame Entry and Exit Logic
The strategy employs a high time frame MACD (Moving Average Convergence Divergence) to make entry and exit decisions. You can easily adjust the timeframe settings and MACD settings in the inputsection to trade on lower timeframes. For more information on the HTF MACD with dynamic smoothing see:
Moving Average High Time Frame Filter
To reduce market 'noise', the strategy incorporates a high time frame moving average filter. This ensures that the trades are aligned with the dominant market trend (trading the trend). In the inputsection traders can easily switch between different type of moving averages. For more information about this HTF filter see:
Dynamic Smoothing
The script includes a feature for dynamic smoothing. The script contains The timeframeToMinutes(tf) function to convert any given time frame into its equivalent in minutes. For example, a daily (D) time frame is converted into 1440 minutes, a weekly (W) into 10,080 minutes, and so forth. Next the smoothing factor is calculated by dividing the minutes of the higher time frame by those of the current time frame. Finally, the script applies a Simple Moving Average (SMA) over the MACD, SIGNAL, and HIST values, MA filter using the dynamically calculated smoothing factor.
  • User Convenience: One of the major benefits is that traders don't need to manually adjust the smoothing factor when switching between different time frames. The script does this dynamically.
  • Visual Consistency: Dynamic smoothing helps traders to more accurately visualize and interpret HTF indicators when trading on lower time frames.
  • Time Frame Restriction: It's crucial to note that the operational time frame should always be lower than the time frame selected in the input sections for dynamic smoothing to function as intended.
By incorporating this dynamic smoothing logic, the script offers traders a nuanced yet straightforward way to adapt High Time Frame indicators for lower time frame trading, enhancing both adaptability and user experience.

Limitations: Exit Strategy
It's crucial to note that the script comes with a simplified exit strategy, devoid of features like a stop-loss, trailing stop-loss or multiple take profits. This means that while the script focuses on entries and risk management, it might result in higher losses if market conditions unexpectedly turn unfavorable.

Effective risk management is pivotal for trading success, and this TradingView script is designed to give you a better idea how to implement positions sizing with your preferred strategy. However, it's essential to note that this tool should not be considered financial advice. Always perform your due diligence and consult with financial advisors before making any trading decisions.

Feel free to use this risk management tool as building block in your trading scripts, Happy Trading!

Open-source Skript

Ganz im Spirit von TradingView hat der Autor dieses Skripts es als Open-Source veröffentlicht, damit Trader es besser verstehen und überprüfen können. Herzlichen Glückwunsch an den Autor! Sie können es kostenlos verwenden, aber die Wiederverwendung dieses Codes in einer Veröffentlichung unterliegt den Hausregeln. Sie können es als Favoriten auswählen, um es in einem Chart zu verwenden.


Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.

Möchten Sie dieses Skript auf einem Chart verwenden?