OPEN-SOURCE SCRIPT

Supertrend with EMA

106
Supertrend with EMA Technical Documentation

This document provides a technical overview of the "Supertrend with EMA" Pine Script indicator, designed for use on TradingView. This indicator combines the popular Supertrend indicator with an Exponential Moving Average (EMA) and an additional smoothing line, offering a comprehensive view of trend direction and potential support/resistance levels.

Indicator Overview
The "Supertrend with EMA" indicator is an overlay script, meaning it plots directly on the price chart. It is compatible with all timeframes and handles timeframe gaps.

Key Features:
Supertrend Calculation: Identifies trend direction using Average True Range (ATR) and a user-defined factor.
EMA Integration: Displays an Exponential Moving Average for additional trend confirmation and dynamic support/resistance.
Smoothing Line: Incorporates a customizable smoothing line applied to the EMA, offering further refinement of trend signals.
Visual Trend Representation: Clearly distinguishes uptrends (green) from downtrends (red) using filled areas.
Alerts: Provides alert conditions for Supertrend trend reversals.
Inputs
Users can customize the indicator's behavior through the following input parameters:

Supertrend Settings
ATR Length: input.int(10, "ATR Length", minval = 1)
Description: The lookback period used for calculating the Average True Range (ATR). A higher value results in a smoother Supertrend line, while a lower value makes it more reactive.
Default: 10
Minimum: 1
Factor: input.float(3.0, "Factor", minval = 0.01, step = 0.01)
Description: The multiplier applied to the ATR to determine the Supertrend bands. A higher factor creates wider bands and fewer signals, while a lower factor creates narrower bands and more signals.
Default: 3.0
Minimum: 0.01
Step: 0.01
EMA Settings
EMA Length: input.int(9, minval=1, title="EMA Length")
Description: The number of bars used in the Exponential Moving Average calculation.
Default: 9
Minimum: 1
Source: input(close, title="Source")
Description: The price series used for EMA calculation. By default, it uses the close price.
Default: close
Offset: input.int(title="Offset", defval=0, minval=-500, maxval=500, display = display.data_window)
Description: Shifts the EMA plot horizontally by the specified number of bars.
Default: 0
Minimum: -500
Maximum: 500
Smoothing Settings
Method: input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing", display = display.data_window)
Description: Selects the type of moving average to use for smoothing the EMA. Options include Simple Moving Average (SMA), Exponential Moving Average (EMA), Smoothed Moving Average (SMMA/RMA), Weighted Moving Average (WMA), and Volume Weighted Moving Average (VWMA).
Default: "SMA"
Length: input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing", display = display.data_window)
Description: The lookback period for the selected smoothing method.
Default: 5
Minimum: 1
Maximum: 100

Calculations
Supertrend Calculation
The Supertrend indicator is calculated using the built-in ta.supertrend function:
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

supertrend: The actual Supertrend line value.
direction: Indicates the current trend direction. A value of -1 typically signifies an uptrend, and 1 signifies a downtrend.
The supertrend line is plotted in green for uptrends and red for downtrends, with fills between the bodyMiddle (average of open and close prices) and the Supertrend line for visual clarity.

Exponential Moving Average (EMA) Calculation
The EMA is calculated using the ta.ema function:
out = ta.ema(src, len)

src: The user-defined source series (default: close).
len: The user-defined EMA length.
Smoothing Line Calculation
A custom ma function is used to calculate various types of moving averages, which is then applied to the EMA output:

out: The calculated EMA value.
smoothingLength: The user-defined length for the smoothing moving average.
typeMA: The user-selected type of moving average for smoothing.
Plots
The indicator plots the following on the chart:

Up Trend: The Supertrend line when the direction indicates an uptrend (color: green).
Down Trend: The Supertrend line when the direction indicates a downtrend (color: red).
Body Middle: The average of the open and close prices (open + close) / 2. This plot is hidden (display = display.none) but used for filling the areas between the price and Supertrend line.
EMA: The Exponential Moving Average (color: blue).
Smoothing Line: The moving average applied to the EMA (color: orange, hidden by default).
Alerts
The indicator provides three alert conditions based on Supertrend trend reversals:

Downtrend to Uptrend: Triggers when the Supertrend direction switches from downtrend (direction[1] > direction) to uptrend.
Uptrend to Downtrend: Triggers when the Supertrend direction switches from uptrend (direction[1] < direction) to downtrend.
Trend Change: Triggers on any Supertrend trend reversal (direction[1] != direction), combining both downtrend to uptrend and uptrend to downtrend changes.

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.