gogo2/Niki/trader/test-NNFX/strategy.pine
2024-06-12 15:46:47 +03:00

99 lines
3.4 KiB
Plaintext

//@version=5
// https://www.youtube.com/watch?v=3fBLZgWSsy4
strategy("NNFX Style Strategy with ADX, EMA, ATR SL and TP", overlay=true)
// SSL Channel
period = input.int(title="SSL Period", defval=140)
smaHigh = ta.sma(high, period)
smaLow = ta.sma(low, period)
var float Hlv = na
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : nz(Hlv[1])
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp = Hlv < 0 ? smaLow : smaHigh
plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)
// T3 Indicator
length_fast = input.int(40, minval=1, title="Fast T3 Length")
length_slow = input.int(90, minval=1, title="Slow T3 Length")
b = 0.7
t3(x, length) =>
e1 = ta.ema(x, length)
e2 = ta.ema(e1, length)
e3 = ta.ema(e2, length)
e4 = ta.ema(e3, length)
e5 = ta.ema(e4, length)
e6 = ta.ema(e5, length)
c1 = -b * b * b
c2 = 3 * b * b + 3 * b * b * b
c3 = -6 * b * b - 3 * b - 3 * b * b * b
c4 = 1 + 3 * b + b * b * b + 3 * b * b
c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3
t3_fast = t3(close, length_fast)
t3_slow = t3(close, length_slow)
plot(t3_fast, color=color.blue, title="T3 Fast")
plot(t3_slow, color=color.red, title="T3 Slow")
// ADX Calculation
adxlen = input.int(100, title="ADX Smoothing")
dilen = input.int(110, title="DI Length")
dirmov(len) =>
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = ta.rma(ta.tr(true), len)
plus = nz(100 * ta.rma(plusDM, len) / truerange)
minus = nz(100 * ta.rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
adx
adx_value = adx(dilen, adxlen)
adx_ema_length = input.int(80, title="ADX EMA Length")
adx_ema = ta.ema(adx_value, adx_ema_length)
plot(adx_value, title="ADX", color=color.orange)
plot(adx_ema, title="ADX EMA", color=color.purple)
// ATR-based Stop Loss and Take Profit
atr_length = input.int(120, title="ATR Length")
atr_stop_loss_multiplier = input.float(10, title="ATR Stop Loss Multiplier")
atr_take_profit_multiplier = input.float(20, title="ATR Take Profit Multiplier")
atr = ta.atr(atr_length)
// Strategy Logic
longCondition = ta.crossover(t3_fast, t3_slow) and adx_value > adx_ema and Hlv > 0
shortCondition = ta.crossunder(t3_fast, t3_slow) and adx_value > adx_ema and Hlv < 0
exitLongCondition = ta.crossunder(t3_fast, t3_slow) or Hlv < 0
exitShortCondition = ta.crossover(t3_fast, t3_slow) or Hlv > 0
// Debug plots
plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, text="LONG")
plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, text="SHORT")
if (longCondition)
stopLoss = close - atr_stop_loss_multiplier * atr
takeProfit = close + atr_take_profit_multiplier * atr
strategy.entry("Long", strategy.long)
strategy.exit("Long TP/SL", from_entry="Long", stop=stopLoss, limit=takeProfit)
if (shortCondition)
stopLoss = close + atr_stop_loss_multiplier * atr
takeProfit = close - atr_take_profit_multiplier * atr
strategy.entry("Short", strategy.short)
strategy.exit("Short TP/SL", from_entry="Short", stop=stopLoss, limit=takeProfit)
if (exitLongCondition)
strategy.close("Long")
if (exitShortCondition)
strategy.close("Short")