TradingView策略设置overlay=true时无法叠加绘制MACD线的问题求助
Solution: Normalize MACD Lines to Main Chart Scale
The issue occurs because MACD values are centered around zero (they represent the difference between two exponential moving averages), while the main chart's price levels are typically orders of magnitude higher. When overlay=true, the plot uses the main chart's price scale, so the MACD lines get squashed near the bottom and become invisible.
To fix this, normalize the MACD and signal lines to fit the main chart's price range. Here's the modified code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=5 strategy('MACD Overlay', overlay=true, process_orders_on_close=true, default_qty_type=strategy.percent_of_equity, initial_capital=1000, default_qty_value=100) [macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) // Define lookback period to calculate historical ranges (adjust as needed) lookback = 200 // Get price range from the main chart price_high = ta.highest(high, lookback) price_low = ta.lowest(low, lookback) // Get MACD and Signal line ranges macd_range_high = ta.highest(macdLine, lookback) macd_range_low = ta.lowest(macdLine, lookback) signal_range_high = ta.highest(signalLine, lookback) signal_range_low = ta.lowest(signalLine, lookback) // Normalize lines to fit the price scale normalized_macd = ta.normalize(macdLine, macd_range_low, macd_range_high, price_low, price_high) normalized_signal = ta.normalize(signalLine, signal_range_low, signal_range_high, price_low, price_high) // Plot normalized lines on main chart plot(normalized_macd, color=color.blue, title="MACD (Normalized)") plot(normalized_signal, color=color.orange, title="Signal (Normalized)")
Key Details:
- Normalization: The
ta.normalize()function scales MACD/signal values to match the price range of the main chart. This preserves the relative relationship between the MACD and signal lines (like crossovers) while making them visible alongside price action. - Lookback Period: The
lookbackvariable (set to 200 here) determines how much historical data is used to calculate ranges. Adjust this based on your chart's timeframe (e.g., use 100 for shorter timeframes, 300 for longer ones). - Consistency: Normalization ensures the lines stay visible across different assets and price levels, unlike fixed offsets which may fail if prices move significantly.
内容的提问来源于stack exchange,提问作者Chalkin
相关产品推荐
相关产品推荐

