# Pine Script ATR Stop Loss to AFL Conversion Required

study(“ATR Trailing Stop Sample”, overlay=true)

SC = input(close,“data array”,source) // data array

// Fast Trailing SL //
AP1 = input(5,“fast ATR period”,integer) // ATR Period
AF1 = input(0.5,“fast ATR multiplier”,float) // ATR Factor
SL1 = AF1*atr(AP1) // Stop Loss
Trail1 = iff(SC>nz(Trail1[1],0) and SC[1]>nz(Trail1[1],0),max(nz(Trail1[1],0),SC-SL1),iff(SC<nz(Trail1[1],0) and SC[1]<nz(Trail1[1],0),min(nz(Trail1[1],0),SC+SL1),iff(SC>nz(Trail1[1],0),SC-SL1,SC+SL1)))

// Slow Trailing SL //
AP2 = input(10,“slow ATR perod”,integer) // ATR Period
AF2 = input(2,“slow ATR multiplier”,float) // ATR Factor
SL2 = AF2*atr(AP2) // Stop Loss
Trail2 = iff(SC>nz(Trail2[1],0) and SC[1]>nz(Trail2[1],0),max(nz(Trail2[1],0),SC-SL2),iff(SC<nz(Trail2[1],0) and SC[1]<nz(Trail2[1],0),min(nz(Trail2[1],0),SC+SL2),iff(SC>nz(Trail2[1],0),SC-SL2,SC+SL2)))

// ATRCD Histogram //
// to plot these, uncomment the code in the plot section below and change indicator overlay to false, also comment out the other plots //
Hst = Trail1-Trail2
Sig = ema(Hst,9)

// Bar color for trade signal //
Blue = Hst<0 and Hst>Sig
Green = Hst>0 and Hst>Sig
Gray = Hst>0 and Hst<Sig
Red = Hst<0 and Hst<Sig

// Signals //