All indicators

Risk Management and Positionsize - MACD example

Aug 31, 2023

Static chart image
SignalsOscillatorsMoney ManagementMoving Averages

The Risk Management and Positionsize - MACD example indicator is a comprehensive strategy tool designed to automate position sizing based on equity risk while providing market timing through high-timeframe trend filters. It aims to help traders manage capital exposure more effectively by integrating risk parameters directly into the entry and exit logic.

Usage

The Usage section focuses on how the script manages trade size and market entries. Users can choose between two main modes for position sizing: a percentage of equity or a fixed number of contracts. When using the percentage-based approach, the Margin Factor allows for precise control over capital deployment. For instance, a Margin Factor of -0.5 utilizes 50% of total equity, effectively reducing the risk per trade relative to the stop-loss distance.

The indicator also functions as a trend-following system. It uses a Higher Time Frame (HTF) MACD for signals and an HTF Moving Average as a filter. A long entry is triggered when the smoothed HTF MACD crosses above its signal line while below the zero line, provided the HTF trend is bullish. Conversely, short entries occur when the smoothed HTF MACD crosses below its signal line while above the zero line during a bearish trend.

Details

The script implements a "Dynamic Smoothing" mechanism to handle Higher Time Frame data. Since HTF data often appears "stepped" or jagged when plotted on lower timeframes, the script calculates a smoothing factor by comparing the timeframe minutes. It then applies a Simple Moving Average (SMA) to the MACD and Trend filter values based on this ratio. This ensures visual consistency and smoother signal transitions without manual adjustments when switching chart intervals.

Settings

Risk Management

  • Initial Balance: Sets the starting capital used for backtesting calculations.
  • qty based on equity %: Toggles between using a percentage of equity (True) or a fixed number of contracts (False).
  • Margin Factor: Adjusts the portion of equity used. Values below 0 reduce exposure (e.g., -0.5 = 50%), while values above 0 allow for leveraged positions.
  • Quantity Contracts: Defines a fixed number of units for the trade if equity-based sizing is disabled.

Higher Time Frame MACD Settings

  • MA type: Selects the moving average type for MACD calculation (EMA, DEMA, TEMA, SMA, WMA, HMA).
  • Higher Time Frame: Sets the timeframe interval for the MACD signals.
  • Fast/Slow/Signal Length: Standard parameters for the MACD calculation.

Higher Time Frame Trend

  • Higher Time Frame: Sets the timeframe for the trend filter (e.g., Daily or Weekly).
  • Length MA: The lookback period for the trend filter.
  • MA type for HTF trend: Selects the smoothing method for the trend filter, including McGinley Dynamic.

FAQ

How do I use the Margin Factor to control my risk? The Margin Factor calibrates your position size relative to your equity. If you want to use only half of your available capital, set the factor to -0.5. This helps align your total risk per trade with standard risk management rules (e.g., 1-2% of total capital).

What is the benefit of using Dynamic Smoothing? Dynamic Smoothing automatically adjusts the indicator's internal averages based on the relationship between your chart timeframe and the selected Higher Time Frame. This prevents the "staircase" effect and provides a more accurate representation of HTF trends on lower intervals.

How can I access this script? 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.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.