OPEN-SOURCE SCRIPT

Custom Trend Indicator


**Custom Trend Indicator**
This indicator identifies bullish and bearish trends using two simple moving averages (SMAs) of customizable lengths. The fast-moving average reacts more quickly to price changes, while the slow-moving average provides a smoother view of the trend. Key features include:

1. **Trend Detection:**
- A bullish trend is identified when the fast-moving average crosses above the slow-moving average.
- A bearish trend is identified when the fast-moving average crosses below the slow-moving average.

2. **Visual Aids:**
- Moving averages are plotted on the chart for clear trend visualization.
- Background colors (green for bullish, red for bearish) highlight the active trend.
- Labels are added at crossover points to mark significant trend changes.

This tool is helpful for traders looking to quickly identify and act on market trends based on simple moving average crossovers.

//version=5
indicator("Custom Trend Indicator", overlay=true)

// Input settings for moving averages
length_fast = input.int(9, minval=1, title="Fast Moving Average Length")
length_slow = input.int(21, minval=1, title="Slow Moving Average Length")

// Calculate moving averages
ma_fast = ta.sma(close, length_fast)
ma_slow = ta.sma(close, length_slow)

// Determine trend conditions
bullish_trend = ma_fast > ma_slow
bearish_trend = ma_fast < ma_slow

// Plot moving averages
plot(ma_fast, color=color.new(color.green, 0), title="Fast MA")
plot(ma_slow, color=color.new(color.red, 0), title="Slow MA")

// Highlight the background for trends
bgcolor(bullish_trend ? color.new(color.green, 90) : na, title="Bullish Trend Background")
bgcolor(bearish_trend ? color.new(color.red, 90) : na, title="Bearish Trend Background")

// Add labels for trend changes
var label bullish_label = na
var label bearish_label = na

if ta.crossover(ma_fast, ma_slow)
label.delete(bearish_label)
bullish_label := label.new(bar_index, high, "Bullish", style=label.style_label_up, color=color.new(color.green, 0))

if ta.crossunder(ma_fast, ma_slow)
label.delete(bullish_label)
bearish_label := label.new(bar_index, low, "Bearish", style=label.style_label_down, color=color.new(color.red, 0))
Trend Analysis

开源脚本

本着真正的TradingView精神,此脚本的作者已将其开源,以便交易者可以理解和验证它。向作者致敬!您可以免费使用它,但在出版物中重复使用此代码受网站规则约束。 您可以收藏它以在图表上使用。

想在图表上使用此脚本?

免责声明