Trend-Following Combo - SuperTrend, EMA Crossover, Aroon Indicator, Aroon Oscillator, DMI (DI +/-), Laguerre RSI

jadamcraig · study · 130 行 · 点赞 3,459 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// https://mozilla.org/MPL/2.0/
//
// Much of this tool is made possible thanks to indicator code made available by
// the following authors:
//  * SuperTrend by Rajandran R
//  * Aroon w/ crossovers highlighted by seiglerj
//  * Aroon Oscillator by jcrewolinsky
//  * Directional Movement Index by TradingView
//  * Laguerre RSA (Self Adjusting Alpha with Fractals Energy) by everget
//
//@version=4
 
study("Trend-Following Combo - SuperTrend, EMA Crossover, Aroon Indicator, Aroon Oscillator, DMI (DI +/-), Laguerre RSI", shorttitle="TFC", overlay = true)
 
// Variables
St1Factor = input(title = "SuperTrend 1: Factor", defval = 1.5, minval = 1.00, maxval = 100, step = 0.01)
St1Period = input(title = "SuperTrend 1: Period", defval = 7, minval = 1, maxval = 100, step = 1)
St2Factor = input(title = "SuperTrend 2: Factor", defval = 1.65, minval = 1.00, maxval = 100, step = 0.01)
St2Period = input(title = "SuperTrend 2: Period", defval = 100, minval = 1, maxval = 100, step = 1)
EmaFast = input(title = "EMA Cross: Fast", defval = 8, step = 1)
EmaSlow = input(title = "EMA Cross: Slow", defval = 15, step = 1)
AroonLength = input(title = "Aroon: Length", defval = 8, step = 1)
DmiLength = input(title = "DMI: Length", defval = 8, step = 1)
LrsiAlpha = input(title = "LRSI: Alpha", defval = 0.7, minval = 0, step = 0.1)
LrsiFeLength = input(title = "LRSI: Fractals Energy Length", type = input.integer, defval = 13)
LrsiApplyFractalsEnergy = input(title="LRSI: Apply Fractals Energy?", defval=true)
LrsiApplyNormalization = input(title="LRSI: Apply Normalization to [0, 100]?", defval=false)
Threshold = input(title = "Indicator Threshold", defval = 3, step = 1)
 
// Define SuperTrend 1 and Determine Values
St1Up = hl2 - (St1Factor * atr(St1Period))
St1Dn = hl2 + (St1Factor * atr(St1Period))
St1TrendUp = 0.0
St1TrendUp := close[1] > St1TrendUp[1] ? max(St1Up,St1TrendUp[1]) : St1Up
St1TrendDown = 0.0
St1TrendDown := close[1] < St1TrendDown[1] ? min(St1Dn,St1TrendDown[1]) : St1Dn
St1Trend = 0.0
St1Trend := close > St1TrendDown[1] ? 1 : close < St1TrendUp[1] ? -1 : nz(St1Trend[1],1)
 
// Define SuperTrend 2 and Determine Values
St2Up = hl2 - (St2Factor * atr(St2Period))
St2Dn = hl2 + (St2Factor * atr(St2Period))
St2TrendUp = 0.0
St2TrendUp := close[1] > St2TrendUp[1] ? max(St2Up,St2TrendUp[1]) : St2Up
St2TrendDown = 0.0
St2TrendDown := close[1] < St2TrendDown[1] ? min(St2Dn,St2TrendDown[1]) : St2Dn
St2Trend = 0.0
St2Trend := close > St2TrendDown[1] ? 1 : close < St2TrendUp[1] ? -1 : nz(St2Trend[1],1)
 
// Combine the two SuperTrends into one, determine values, and plot
StComboTrend = 0.0
StComboTrend := St1Trend == St2Trend ? St1Trend : na
StComboTrendUp = St1TrendUp < St2TrendUp ? St1TrendUp : St2TrendUp
StComboTrendDown = St1TrendDown > St2TrendDown ? St1TrendDown : St2TrendDown
StComboTsl = StComboTrend == 1 ? StComboTrendUp : StComboTrend == -1 ? StComboTrendDown : na
StComboLinecolor = StComboTrend == 1 ? #00ff00 : #ff0000
plot(StComboTsl, color = StComboLinecolor, style = plot.style_linebr, linewidth = 2, title = "SuperTrend Combo")
 
// Define EMA Cross and Determine Status
Ma1 = ema(close, EmaFast)
Ma2 = ema(close, EmaSlow)
MaTrend = Ma1 < Ma2 ? -1 : 1
 
// Define Aroon Indicator and Determine Status
AroonIndicatorUpper = 100 * (highestbars(high, AroonLength + 1) + AroonLength) / AroonLength
AroonIndicatorLower = 100 * (lowestbars(low, AroonLength + 1) + AroonLength) / AroonLength
AroonIndictorTrend = 0
AroonIndictorTrend := crossover(AroonIndicatorUpper, AroonIndicatorLower) ? 1 : crossover(AroonIndicatorLower, AroonIndicatorUpper) ? -1 : AroonIndictorTrend[1]
 
// Define Aroon Oscillator and Determine Status
AroonOscillatorMidpoint = 0
AroonOscillator = AroonIndicatorUpper - AroonIndicatorLower
AroonOscillatorSignal = 0
AroonOscillatorSignal := crossover(AroonOscillator, -80) ? 1 : crossunder(AroonOscillator, 80) ? -1 : AroonOscillatorSignal[1]
 
// Define Directional Movement Index and Determine Values
DmiUp = change(high)
DmiDown = -change(low)
DmiPlusDm = na(DmiUp) ? na : (DmiUp > DmiDown and DmiUp > 0 ? DmiUp : 0)
DmiMinusDm = na(DmiDown) ? na : (DmiDown > DmiUp and DmiDown > 0 ? DmiDown : 0)
DmiTrur = rma(tr, DmiLength)
DmiPlus = fixnan(100 * rma(DmiPlusDm, DmiLength) / DmiTrur)
DmiMinus = fixnan(100 * rma(DmiMinusDm, DmiLength) / DmiTrur)
DmiTrend = 0
DmiTrend := crossover(DmiPlus, DmiMinus) ? 1 : crossover(DmiMinus, DmiPlus) ? -1 : DmiTrend[1]
 
// Define Laguerre RSI and Determine Values
LrsiOC = (open + nz(close[1])) / 2
LrsiHC = max(high, nz(close[1]))
LrsiLC = min(low, nz(close[1]))
LrsiFeSrc = (LrsiOC + LrsiHC + LrsiLC + close) / 4
LrsiFeAlpha = log(sum((LrsiHC - LrsiLC) / (highest(LrsiFeLength) - lowest(LrsiFeLength)), LrsiFeLength)) / log(LrsiFeLength)
LrsiAlphaCalc = LrsiApplyFractalsEnergy ? LrsiFeAlpha : LrsiAlpha
LrsiL0 = 0.0
LrsiL0 := LrsiAlphaCalc * (LrsiApplyFractalsEnergy ? LrsiFeSrc : close) + (1 - LrsiAlphaCalc) * nz(LrsiL0[1])
LrsiL1 = 0.0
LrsiL1 := -(1 - LrsiAlphaCalc) * LrsiL0 + nz(LrsiL0[1]) + (1 - LrsiAlphaCalc) * nz(LrsiL1[1])
LrsiL2 = 0.0
LrsiL2 := -(1 - LrsiAlphaCalc) * LrsiL1 + nz(LrsiL1[1]) + (1 - LrsiAlphaCalc) * nz(LrsiL2[1])
LrsiL3 = 0.0
LrsiL3 := -(1 - LrsiAlphaCalc) * LrsiL2 + nz(LrsiL2[1]) + (1 - LrsiAlphaCalc) * nz(LrsiL3[1])
LrsiCU = 0.0
LrsiCU := (LrsiL0 >= LrsiL1 ? LrsiL0 - LrsiL1 : 0) + (LrsiL1 >= LrsiL2 ? LrsiL1 - LrsiL2 : 0) + (LrsiL2 >= LrsiL3 ? LrsiL2 - LrsiL3 : 0)
LrsiCD = 0.0
LrsiCD := (LrsiL0 >= LrsiL1 ? 0 : LrsiL1 - LrsiL0) + (LrsiL1 >= LrsiL2 ? 0 : LrsiL2 - LrsiL1) + (LrsiL2 >= LrsiL3 ? 0 : LrsiL3 - LrsiL2)
Lrsi = LrsiCU + LrsiCD != 0
         ? LrsiApplyNormalization ? 100 * LrsiCU / (LrsiCU + LrsiCD) : LrsiCU / (LrsiCU + LrsiCD)
         : 0
LrsiMult = (LrsiApplyNormalization ? 100 : 1)
LrsiOverBought = 0.8 * LrsiMult
LrsiOverSold = 0.2 * LrsiMult
LrsiSignal = 0
LrsiSignal := crossover(Lrsi, LrsiOverSold) ? 1 : crossunder(Lrsi, LrsiOverBought) ? -1 : LrsiSignal[1]
 
// Determine Strength of Trend Based on Status of All Indicators
MaTrendCalc = StComboTrend == MaTrend ? StComboTrend : 0
AroonIndictorTrendCalc = StComboTrend == AroonIndictorTrend ? StComboTrend : 0
AroonOscillatorSignalCalc = StComboTrend == AroonOscillatorSignal ? StComboTrend : 0
DmiTrendCalc = StComboTrend == DmiTrend ? StComboTrend : 0
LrsiSignalCalc = StComboTrend == LrsiSignal ? StComboTrend : 0
TrendStrength = MaTrendCalc + AroonIndictorTrendCalc + AroonOscillatorSignalCalc + DmiTrendCalc + LrsiSignalCalc
 
// Plot Entry Arrows
EntryDirection = 0
EntryDirection := StComboTrend == 1 and TrendStrength >= Threshold and TrendStrength[1] < Threshold ? 1 : StComboTrend == -1 and TrendStrength <= -Threshold and TrendStrength[1] > -Threshold ? -1 : EntryDirection[1]
plotarrow(StComboTrend == 1 and TrendStrength >= Threshold and TrendStrength[1] < Threshold ? StComboTrend : na, title="Up Entry Arrow", colorup=color.yellow, maxheight=40, minheight=20, transp=20)
//plotarrow(EntryDirection == 1 and EntryDirection[1] != 1 ? StComboTrend : na, title="Up Entry Arrow", colorup=color.yellow, maxheight=40, minheight=20, transp=20)
plotarrow(StComboTrend == -1 and TrendStrength <= -Threshold and TrendStrength[1] > -Threshold ? StComboTrend : na, title="Down Entry Arrow", colordown=color.yellow, maxheight=40, minheight=20, transp=20)
//plotarrow(EntryDirection == -1 and EntryDirection[1] != -1 ? StComboTrend : na, title="Down Entry Arrow", colordown=color.yellow, maxheight=40, minheight=20, transp=20)

← 返回列表