10. Testing 1st confirm for fixed TP SPY - idavidov13/Pine-Script GitHub Wiki

Preconditions

Testing SPY as initial ticker. The goal is to have an indicator with greater profit than buy and hold for the testing period (01/Jan/2019 - 01/Jan/2024). The baseline is 40k at the end (15 k profit) Initial capital is 25k. The risk is 2%. It is possible to create a strategy only for long positions.

Main Parameters of Testing

image

Used 1st Indi Baseline

SSL

  • 15/15 - TP = 2.5xATR image

Twiggs Money Flow

  • 14 - TP = 2.5xATR image

Vortex

  • 15 - TP = 2.5xATR image

Average Sentiment Oscillator

  • 8/2 - TP = 2.5xATR image

Doda Stochastic

  • close/7/8/9 - TP = 2.5xATR image

Prevailing Trend

  • 72/close/sma - TP = 2.5xATR image

Bulls vs Bears

  • 4/230/80 - TP = 2.5xATR image

Squeeze Momentum Indicator

image

// Input parameters
length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")

useTrueRange = input.bool(true, title="Use TrueRange (KC)")

// Calculate BB
source = close
basis = ta.sma(source, length)
dev = multKC * ta.stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = ta.sma(source, lengthKC)
_range = useTrueRange ? ta.tr : (high - low)
rangema = ta.sma(_range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz  = (sqzOn == false) and (sqzOff == false)

val = ta.linreg(source  -  math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)), ta.sma(close, lengthKC)), lengthKC, 0)

bcolor = val > 0 ? (val > nz(val[1]) ? color.lime : color.green) : (val < nz(val[1]) ? color.red : color.maroon)
scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray 
plot(val, color=bcolor, style=plot.style_histogram, linewidth=4)
plot(0, color=scolor, style=plot.style_cross, linewidth=2)
//Insert indicator conditions as Trigger
C1_L_Trigger = ta.crossover(val, 0)
C1_S_Trigger = ta.crossunder(val, 0)

Daily

  • 20/2/18/1.5 - TP = 1xATR image
  • 20/2/18/1.5 - TP = 2.5xATR image

WaveTrend

image

// Input parameters
n1 = input.int(10, "Channel Length")
n2 = input.int(21, "Average Length")
obLevel1 = input.int(60, "Over Bought Level 1")
obLevel2 = input.int(53, "Over Bought Level 2")
osLevel1 = input.int(-60, "Over Sold Level 1")
osLevel2 = input.int(-53, "Over Sold Level 2")
 
ap = hlc3 
esa = ta.ema(ap, n1)
d = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ta.ema(ci, n2)
 
wt1 = tci
wt2 = ta.sma(wt1,4)

//Insert indicator conditions as Trigger
C1_L_Trigger = ta.crossover(wt1, 0)
C1_S_Trigger = ta.crossunder(wt1, 0)

Daily

  • 14/8 - TP = 1xATR image
  • 14/8 - TP = 2.5xATR image

Supertrend

image

// Input parameters
Periods = input(10, title="ATR Period")
src = input(hl2, title="Source")
Multiplier = input.float(3.0, title="ATR Multiplier", step=0.1)
changeATR = input(true, title="Change ATR Calculation Method?")
showsignals = input(true, title="Show Buy/Sell Signals?")
highlighting = input(true, title="Highlighter On/Off?")

// ATR Calculation
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2

// Supertrend calculation
up = src - (Multiplier * atr)
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up

dn = src + (Multiplier * atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

// Trend determination
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

// Plotting
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1

dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1

//Insert indicator conditions as Trigger
C1_L_Trigger = buySignal
C1_S_Trigger = sellSignal

Daily

  • 14/8 - TP = 1xATR image

  • 10/close/2.5 - TP = 2.5xATR image

src = input(close, title="Source")
length = input.int(2, title="OTT Period", minval=1)
percent = input.float(1.4, title="OTT Percent", step=0.1, minval=0)
showsupport = input(true, title="Show Support Line?")
showsignalsk = input(true, title="Show Support Line Crossing Signals?")
showsignalsc = input(false, title="Show Price/OTT Crossing Signals?")
highlight = input(false, title="Show OTT Color Changes?")
showsignalsr = input(false, title="Show OTT Color Change Signals?")
highlighting = input(true, title="Highlighter On/Off?")
mav = input.string("VAR", title="Moving Average Type", options=["SMA", "EMA", "WMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF"])

// VAR function
var_Func(src, length) =>
    valpha = 2 / (length + 1)
    vud1 = src > src[1] ? src - src[1] : 0
    vdd1 = src < src[1] ? src[1] - src : 0
    vUD = math.sum(vud1, 9)
    vDD = math.sum(vdd1, 9)
    vCMO = nz((vUD - vDD) / (vUD + vDD))
    VAR = 0.0
    VAR := nz(valpha * math.abs(vCMO) * src) + (1 - valpha * math.abs(vCMO)) * nz(VAR[1])

VAR = var_Func(src, length)

// WWMA function
wwma_Func(src, length) =>
    wwalpha = 1 / length
    WWMA = 0.0
    WWMA := wwalpha * src + (1 - wwalpha) * nz(WWMA[1])

WWMA = wwma_Func(src, length)

// ZLEMA function
zlema_Func(src, length) =>
    zxLag = length / 2 == math.round(length / 2) ? length / 2 : (length - 1) / 2
    zxEMAData = src + (src - src[zxLag])
    ZLEMA = ta.ema(zxEMAData, length)

ZLEMA = zlema_Func(src, length)

// TSF function
tsf_Func(src, length) =>
    lrc = ta.linreg(src, length, 0)
    lrc1 = ta.linreg(src, length, 1)
    lrs = lrc - lrc1
    TSF = ta.linreg(src, length, 0) + lrs

TSF = tsf_Func(src, length)

// Moving Average selection
get_MA(src, length) =>
    ma = 0.0
    if mav == "SMA"
        ma := ta.sma(src, length)
    if mav == "EMA"
        ma := ta.ema(src, length)
    if mav == "WMA"
        ma := ta.wma(src, length)
    if mav == "TMA"
        ma := ta.sma(ta.sma(src, math.ceil(length / 2)), math.floor(length / 2) + 1)
    if mav == "VAR"
        ma := VAR
    if mav == "WWMA"
        ma := WWMA
    if mav == "ZLEMA"
        ma := ZLEMA
    if mav == "TSF"
        ma := TSF
    ma

MAvg = get_MA(src, length)
fark = MAvg * percent * 0.01
longStop = MAvg - fark
longStopPrev = nz(longStop[1], longStop)
longStop := MAvg > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = MAvg + fark
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := MAvg < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop

dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and MAvg > shortStopPrev ? 1 : dir == 1 and MAvg < longStopPrev ? -1 : dir

MT = dir == 1 ? longStop : shortStop
OTT = MAvg > MT ? MT * (200 + percent) / 200 : MT * (200 - percent) / 200

// Plotting
plot(showsupport ? MAvg : na, color=color.new(#0585E1, 0), linewidth=2, title="Support Line")

OTTC = highlight ? OTT[2] > OTT[3] ? color.green : color.red : color.new(#B800D9, 0)
pALL = plot(nz(OTT[2]), color=OTTC, linewidth=2, title="OTT", transp=0)

// Alert conditions
alertcondition(ta.cross(OTT[2], OTT[3]), title="Color ALARM", message="OTT Has Changed Color!")
alertcondition(ta.crossover(OTT[2], OTT[3]), title="GREEN ALERT", message="OTT GREEN BUY SIGNAL!")
alertcondition(ta.crossunder(OTT[2], OTT[3]), title="RED ALERT", message="OTT RED SELL SIGNAL!")
alertcondition(ta.cross(MAvg, OTT[2]), title="Cross Alert", message="OTT - Support Line Crossing!")
alertcondition(ta.crossover(MAvg, OTT[2]), title="Crossover Alarm", message="Support Line BUY SIGNAL!")
alertcondition(ta.crossunder(MAvg, OTT[2]), title="Crossunder Alarm", message="Support Line SELL SIGNAL!")
alertcondition(ta.cross(src, OTT[2]), title="Price Cross Alert", message="OTT - Price Crossing!")
alertcondition(ta.crossover(src, OTT[2]), title="Price Crossover Alarm", message="PRICE OVER OTT - BUY SIGNAL!")
alertcondition(ta.crossunder(src, OTT[2]), title="Price Crossunder Alarm", message="PRICE UNDER OTT - SELL SIGNAL!")

// Buy/Sell signal plot shapes
buySignalk = ta.crossover(MAvg, OTT[2])
plotshape(buySignalk and showsignalsk ? OTT * 0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallk = ta.crossunder(MAvg, OTT[2])
plotshape(sellSignallk and showsignalsk ? OTT * 1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

buySignalc = ta.crossover(src, OTT[2])
plotshape(buySignalc and showsignalsc ? OTT * 0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallc = ta.crossunder(src, OTT[2])
plotshape(sellSignallc and showsignalsc ? OTT * 1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

// Highlighting
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none)
longFillColor = highlighting ? (MAvg > OTT ? color.green : na) : na
shortFillColor = highlighting ? (MAvg < OTT ? color.red : na) : na
fill(mPlot, pALL, title="UpTrend Highlighter", color=longFillColor)
fill(mPlot, pALL, title="DownTrend Highlighter", color=shortFillColor)

buySignalr = ta.crossover(OTT[2], OTT[3])
plotshape(buySignalr and showsignalsr ? OTT * 0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

sellSignallr = ta.crossunder(OTT[2], OTT[3])
plotshape(sellSignallr and showsignalsr ? OTT * 1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)


//Insert indicator conditions as Trigger
C1_L_Trigger = buySignalk
C1_S_Trigger = sellSignallk

Daily

  • close/11/1.4/WMA - TP = 1xATR image

  • close/11/1.4/WMA - TP = 1.5xATR image