0% found this document useful (0 votes)
59 views2 pages

Backtest - Tool - On Tredingview

This document is a Pine Script code for a trading indicator that combines Smoothed Heikin-Ashi candles with optional EMA and ADX filters. It allows users to customize parameters for two EMAs and an ADX filter to generate long and short trading signals based on specific conditions. The script also includes plotting functionalities for visualizing the smoothed candles, EMAs, and signals on a trading chart.

Uploaded by

lathiyayug13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views2 pages

Backtest - Tool - On Tredingview

This document is a Pine Script code for a trading indicator that combines Smoothed Heikin-Ashi candles with optional EMA and ADX filters. It allows users to customize parameters for two EMAs and an ADX filter to generate long and short trading signals based on specific conditions. The script also includes plotting functionalities for visualizing the smoothed candles, EMAs, and signals on a trading chart.

Uploaded by

lathiyayug13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//@version=5

indicator("Smoothed HA with Optional EMA & ADX Filters", overlay=true)

// === Input Parameters ===


length1 = input.int(6, "First EMA Length", minval=1)
length2 = input.int(6, "Second EMA Length", minval=1)

// EMA Filter Options: Enable/disable each EMA filter individually.


useFastEMA = input.bool(true, "Use Fast Trend EMA")
useSlowEMA = input.bool(true, "Use Slow Trend EMA")
fastTrendLength = input.int(20, "Fast Trend EMA Length", minval=1)
slowTrendLength = input.int(50, "Slow Trend EMA Length", minval=1)

// ADX Filter Option: Enable or disable ADX filter.


useADX = input.bool(true, "Use ADX Filter")
adxLength = input.int(14, "ADX Length", minval=1)
adxThreshold = input.float(25.0, "ADX Threshold", step=0.1)

//////////////////////////////////////////////////////
// Standard Smoothed Heikin-Ashi Calculation //
//////////////////////////////////////////////////////

// Standard Heikin-Ashi values.


ha_close = (open + high + low + close) / 4
var float ha_open_val = na
ha_open_val := bar_index == 0 ? ha_close : (ha_open_val[1] + ha_close[1]) / 2
ha_high = math.max(high, math.max(open, close))
ha_low = math.min(low, math.min(open, close))

// First Smoothing Stage.


smoothed_open = ta.ema(ha_open_val, length1)
smoothed_close = ta.ema(ha_close, length1)
smoothed_high = ta.ema(ha_high, length1)
smoothed_low = ta.ema(ha_low, length1)

// Second Smoothing Stage.


sha_open = ta.ema(smoothed_open, length2)
sha_close = ta.ema(smoothed_close, length2)
sha_high = ta.ema(smoothed_high, length2)
sha_low = ta.ema(smoothed_low, length2)

//////////////////////////////////////////////////////
// Dual EMA Trend Filter Calculation //
//////////////////////////////////////////////////////

// Calculate fast and slow trend EMAs on the smoothed HA close.


fastTrendEMA = ta.ema(sha_close, fastTrendLength)
slowTrendEMA = ta.ema(sha_close, slowTrendLength)

//////////////////////////////////////////////////////
// Manual ADX Calculation //
//////////////////////////////////////////////////////

// True Range and Directional Movements.


tr = math.max(high - low, math.max(math.abs(high - nz(close[1])), math.abs(low -
nz(close[1]))))
up = high - nz(high[1])
down = nz(low[1]) - low
plusDM = (up > down and up > 0) ? up : 0.0
minusDM = (down > up and down > 0) ? down : 0.0

// Wilder's smoothing for ATR and directional sums.


atr = ta.rma(tr, adxLength)
plusDI = 100 * ta.rma(plusDM, adxLength) / atr
minusDI = 100 * ta.rma(minusDM, adxLength) / atr
dx = 100 * math.abs(plusDI - minusDI) / (plusDI + minusDI)
adxValue = ta.rma(dx, adxLength)

//////////////////////////////////////////////////////
// Build Optional Filter Conditions //
//////////////////////////////////////////////////////

emaConditionLong = useFastEMA and useSlowEMA ? (fastTrendEMA > slowTrendEMA and


sha_close > fastTrendEMA and sha_close > slowTrendEMA) : useFastEMA and not
useSlowEMA ? (sha_close > fastTrendEMA) : not useFastEMA and useSlowEMA ?
(sha_close > slowTrendEMA) : true
emaConditionShort = useFastEMA and useSlowEMA ? (fastTrendEMA < slowTrendEMA and
sha_close < fastTrendEMA and sha_close < slowTrendEMA) : useFastEMA and not
useSlowEMA ? (sha_close < fastTrendEMA) : not useFastEMA and useSlowEMA ?
(sha_close < slowTrendEMA) : true
adxCondition = useADX ? (adxValue > adxThreshold) : true

//////////////////////////////////////////////////////
// Signal Generation //
//////////////////////////////////////////////////////

// Basic reversal conditions.


bullishReversal = (sha_close[1] < sha_open[1]) and (sha_close > sha_open)
bearishReversal = (sha_close[1] > sha_open[1]) and (sha_close < sha_open)

// Final Long and Short Signal conditions.


longSignal = bullishReversal and emaConditionLong and adxCondition
shortSignal = bearishReversal and emaConditionShort and adxCondition

//////////////////////////////////////////////////////
// Plotting //
//////////////////////////////////////////////////////

// Plot the smoothed Heikin-Ashi candles.


plotcandle(sha_open, sha_high, sha_low, sha_close,
title="Smoothed Heikin-Ashi",
color = sha_close >= sha_open ? color.green : color.red,
wickcolor = color.black)

// Conditionally plot the trend EMAs.


plot(useFastEMA ? fastTrendEMA : na, color=color.orange, title="Fast Trend EMA")
plot(useSlowEMA ? slowTrendEMA : na, color=color.blue, title="Slow Trend EMA")

// Conditionally plot ADX and its threshold.


plot(useADX ? adxValue : na, color=color.purple, title="ADX")
plot(useADX ? adxThreshold : na, color=color.gray, title="ADX Threshold")

// Mark long and short signals on the chart.


plotshape(longSignal, title="Long Signal", style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.small)
plotshape(shortSignal, title="Short Signal", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.small)

You might also like