SuperTrended Moving Averages from Tradingview

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #216653 quote
    AndrewtUK
    Participant
    New

    Hi

    Please see below to code.

    //@version=5

    indicator(‘SuperTrended Moving Averages’, ‘ST MA’, overlay=true, format=format.price, precision=2, timeframe=”, timeframe_gaps=false)
    src = input(close, title=’Source’)
    mav = input.string(title=’Moving Average Type’, defval=’EMA’, options=[‘SMA’, ‘EMA’, ‘WMA’, ‘DEMA’, ‘TMA’, ‘VAR’, ‘WWMA’, ‘ZLEMA’, ‘TSF’, ‘HULL’, ‘TILL’])
    length = input.int(100, ‘Moving Average Length’, minval=1)
    Periods = input(title=’ATR Period’, defval=10)
    Multiplier = input.float(title=’ATR Multiplier’, step=0.1, defval=0.5)
    changeATR = input(title=’Change ATR Calculation Method ?’, defval=true)
    showsignals = input(title=’Show Buy/Sell Signals ?’, defval=false)
    highlighting = input(title=’Highlighter On/Off ?’, defval=true)

     

    T3a1 = input.float(0.7, ‘TILLSON T3 Volume Factor’, step=0.1)

    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 = Var_Func(src, length)
    DEMA = 2 * ta.ema(src, length) – ta.ema(ta.ema(src, length), length)
    Wwma_Func(src, length) =>
    wwalpha = 1 / length
    WWMA = 0.0
    WWMA := wwalpha * src + (1 – wwalpha) * nz(WWMA[1])
    WWMA
    WWMA = Wwma_Func(src, length)
    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 = Zlema_Func(src, length)
    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 = Tsf_Func(src, length)
    HMA = ta.wma(2 * ta.wma(src, length / 2) – ta.wma(src, length), math.round(math.sqrt(length)))
    T3e1 = ta.ema(src, length)
    T3e2 = ta.ema(T3e1, length)
    T3e3 = ta.ema(T3e2, length)
    T3e4 = ta.ema(T3e3, length)
    T3e5 = ta.ema(T3e4, length)
    T3e6 = ta.ema(T3e5, length)
    T3c1 = -T3a1 * T3a1 * T3a1
    T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1
    T3c3 = -6 * T3a1 * T3a1 – 3 * T3a1 – 3 * T3a1 * T3a1 * T3a1
    T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1
    T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3

    getMA(src, length) =>
    ma = 0.0
    if mav == ‘SMA’
    ma := ta.sma(src, length)
    ma

    if mav == ‘EMA’
    ma := ta.ema(src, length)
    ma

    if mav == ‘WMA’
    ma := ta.wma(src, length)
    ma

    if mav == ‘DEMA’
    ma := DEMA
    ma

    if mav == ‘TMA’
    ma := ta.sma(ta.sma(src, math.ceil(length / 2)), math.floor(length / 2) + 1)
    ma

    if mav == ‘VAR’
    ma := VAR
    ma

    if mav == ‘WWMA’
    ma := WWMA
    ma

    if mav == ‘ZLEMA’
    ma := ZLEMA
    ma

    if mav == ‘TSF’
    ma := TSF
    ma

    if mav == ‘HULL’
    ma := HMA
    ma

    if mav == ‘TILL’
    ma := T3
    ma
    ma

    MA = getMA(src, length)

     

    atr2 = ta.sma(ta.tr, Periods)
    atr = changeATR ? ta.atr(Periods) : atr2
    up = MA – Multiplier * atr
    up1 = nz(up[1], up)
    up := close[1] > up1 ? math.max(up, up1) : up
    dn = MA + Multiplier * atr
    dn1 = nz(dn[1], dn)
    dn := close[1] < dn1 ? math.min(dn, dn1) : dn
    trend = 1
    trend := nz(trend[1], trend)
    trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
    upPlot = plot(trend == 1 ? up : na, title=’Up Trend’, color=color.new(color.green, 100), linewidth=0, style=plot.style_linebr)
    buySignal = trend == 1 and trend[1] == -1
    plotshape(buySignal ? up : na, title=’UpTrend Begins’, location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 100))
    plotshape(buySignal and showsignals ? up : na, title=’Buy’, text=’Buy’, location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
    dnPlot = plot(trend == 1 ? na : dn, title=’Down Trend’, style=plot.style_linebr, linewidth=0, color=color.new(color.red, 100))
    sellSignal = trend == -1 and trend[1] == 1
    plotshape(sellSignal ? dn : na, title=’DownTrend Begins’, location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 100))
    plotshape(sellSignal and showsignals ? dn : na, title=’Sell’, text=’Sell’, location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
    mPlot = plot(ohlc4, title=”, style=plot.style_circles, linewidth=0)
    colorup = input.color(defval = color.new(color.green, 60), title = “ColorU”, inline = ‘color’)
    colordown = input.color(defval = color.new(color.red, 60), title = “ColorD”, inline = ‘color’)
    longFillColor = highlighting ? trend == 1 ? colorup : color.white : color.new(color.white, 100)
    shortFillColor = highlighting ? trend == -1 ? colordown : color.white : color.new(color.white, 100)
    fill(mPlot, upPlot, title=’UpTrend Highligter’, color=longFillColor)
    fill(mPlot, dnPlot, title=’DownTrend Highligter’, color=shortFillColor)
    alertcondition(buySignal, title=’SuperTrend Buy’, message=’SuperTrend Buy!’)
    alertcondition(sellSignal, title=’SuperTrend Sell’, message=’SuperTrend Sell!’)
    changeCond = trend != trend[1]
    alertcondition(changeCond, title=’SuperTrend Direction Change’, message=’SuperTrend has changed direction!’)

     

    Cheers

    #216663 quote
    Nicolas
    Keymaster
    Master

    Yes I see the code above thank you.

    #216704 quote
    JS
    Participant
    Senior

    Hi @AndrewtUK

    Hereby the conversion of “Super Trended Moving Averages” from TradingView…

    The color fill is not in the code (does not work optimally) but is set in the graph under “color zones”…

    Next time, please provide some more information than just pasting the code on the forum… 🙂  

    src = Close //Data type
    mav = 1 //Moving Average  Value: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TMA, 5=VAR, 6=WWMA, 7=ZLEMA, 8=TSF, 9=HMA, 10=T3
    length = 100 //Moving Average Length
    Periods = 10 //ATR period
    Multiplier = 0.5 //ATR Multiplier
    
    T3a1 = 0.7 //Tillson T3 Volume Factor
    
    valpha = 2 / (length+1)
    
    If src > src[1] then //Green candle
    vud1 = src - src[1] //Green profit
    else
    vud1 = 0
    EndIf
    
    If src < src[1] then //Red candle
    vdd1 = src[1] - src //Red Loss
    else
    vdd1=0
    EndIf
    
    vUD = Summation[9](vud1) //Summation [9] Green profit
    vDD = Summation[9](vdd1) //Summation [9] Red Loss
    
    vCMO = ((vUD - vDD) / vUD + vDD) //Cumulative Market Oscillator
    Once VAR = 0
    VAR = (valpha * Abs(vCMO) * src) + (1 - valpha * Abs(vCMO)) * VAR[1]
    
    xDEMA = 2 * ExponentialAverage[length](src) - ExponentialAverage[length](ExponentialAverage[length](src))
    
    wwalpha = 1 //Length
    Once WWMA = 0
    WWMA = wwalpha * src + (1 - wwalpha) * WWMA[1]
    
    If (length / 2) = Round(length / 2) then //Check even/odd length for "zero lag"
    zxLag = (length / 2)
    Else
    zxLag = ((length-1) / 2)
    EndIf
    
    zxEMAData = src + src - src[zxLag]
    xZLEMA = ExponentialAverage[length](zxEMAData) //Zero Lag Exponential Average
    
    lrc = LinearRegression[length](src)
    lrc1 = lrc[1]
    lrs = lrc - lrc1
    TSF = LinearRegression[length](src) + lrs //Time Series Forecast
    
    HMA = WeightedAverage[Round(SQRT(length))](2 * WeightedAverage[length/2](src) - WeightedAverage[length](src)) //Hull Moving Average
    
    T3e1 = ExponentialAverage[length](src)
    T3e2 = ExponentialAverage[length](T3e1)
    T3e3 = ExponentialAverage[length](T3e2)
    T3e4 = ExponentialAverage[length](T3e3)
    T3e5 = ExponentialAverage[length](T3e4)
    T3e6 = ExponentialAverage[length](T3e5)
    
    T3c1 = -T3a1 * T3a1 * T3a1
    T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1
    T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1
    T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1
    T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3 //T3 Moving Average
    
    Once MA = 0
    If mav = 0 then
    MA = Average[length,0](src) //SMA
    ElsIf mav = 1 then
    MA = Average[length,1](src) //EMA
    ElsIf mav = 2 then
    MA = Average[length,2](src) //WEMA
    ElsIf mav = 3 then
    MA = xDEMA //DEMA
    ElsIf mav = 4 then
    MA = Average[Floor(length / 2) + 1](Average[Ceil(length / 2)](src)) //TMA
    ElsIf mav = 5 then
    MA = VAR //VAR a.k.a. VIDYA Variable Index Dynamic Moving Average
    ElsIf mav = 6 then
    MA = WWMA //WWMA Welles Wilder's Moving Average
    ElsIf mav = 7 then
    MA = xZLEMA //ZLEMA Zero Lag Exponential Moving Average
    ElsIf mav = 8 then
    MA = TSF //TSF Time Series Forecast
    ElsIf mav = 9 then
    MA = HMA //HMA Hull Moving Average
    ElsIf mav = 10 then
    MA = T3 //T3 Tillson Moving Average
    EndIf
    
    atr = AverageTrueRange[Periods](src)
    up = MA - Multiplier * atr
    up1 = up[1]
    If src[1] > up1 then
    up = Max(up, up1)
    Else
    up = up
    EndIf
    dn = MA + Multiplier * atr
    dn1 = dn[1]
    If src[1] < dn1 then
    dn = Min(dn, dn1)
    else
    dn = dn
    EndIf
    
    Once trend = 1
    If trend = -1 and Close > dn1 then
    trend = 1
    ElsIf trend = 1 and Close < up1 then
    trend = -1
    EndIf
    
    If trend = 1 then
    upR=0
    upG=255
    upB=0
    upAlpha=0
    Else
    upR=0
    upG=0
    upB=0
    upAlpha=0
    EndIf
    
    //If Close > up then
    //ColorBetween(Close,up,0,255,0)
    //EndIf
    
    If trend = -1 then
    dnR=255
    dnG=0
    dnB=0
    dnAlpha=0
    else
    dnR=0
    dnG=0
    dnB=0
    dnAlpha=0
    EndIf
    
    //If Close < dn then
    //ColorBetween(Close,dn,255,0,0)
    //EndIf
    
    Return up as "Up Trend" coloured(upR,upG,upB,upAlpha), dn as "Down Trend" coloured(dnR,dnG,dnB, dnAlpha)
    

    #216710 quote
    JS
    Participant
    Senior
    Hi,

    Hereby the trading system based on the “Super Trended MA” indicator…

    When the trend changes from 1 to -1: SHORT…

    When the trend changes from -1 to 1: LONG…

    Number of contracts can be adjusted in “PositionSize” now standard 1 contract…

     
    src = Close //Data type
    mav = 1 //Moving Average  Value: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TMA, 5=VAR, 6=WWMA, 7=ZLEMA, 8=TSF, 9=HMA, 10=T3
    length = 100 //Moving Average Length
    Periods = 10 //ATR period
    Multiplier = 0.5 //ATR Multiplier
    
    T3a1 = 0.7 //Tillson T3 Volume Factor
    
    valpha = 2 / (length+1)
    
    PositionSize = 1 //Default PositionSize for trading
    
    If src > src[1] then //Green candle
    vud1 = src - src[1] //Green profit
    else
    vud1 = 0
    EndIf
    
    If src < src[1] then //Red candle
    vdd1 = src[1] - src //Red Loss
    else
    vdd1=0
    EndIf
    
    vUD = Summation[9](vud1) //Summation [9] Green profit
    vDD = Summation[9](vdd1) //Summation [9] Red Loss
    
    vCMO = ((vUD - vDD) / vUD + vDD) //Cumulative Market Oscillator
    Once VAR = 0
    VAR = (valpha * Abs(vCMO) * src) + (1 - valpha * Abs(vCMO)) * VAR[1]
    
    xDEMA = 2 * ExponentialAverage[length](src) - ExponentialAverage[length](ExponentialAverage[length](src))
    
    wwalpha = 1 //Length
    Once WWMA = 0
    WWMA = wwalpha * src + (1 - wwalpha) * WWMA[1]
    
    If (length / 2) = Round(length / 2) then //Check even/odd length for "zero lag"
    zxLag = (length / 2)
    Else
    zxLag = ((length-1) / 2)
    EndIf
    
    zxEMAData = src + src - src[zxLag]
    xZLEMA = ExponentialAverage[length](zxEMAData) //Zero Lag Exponential Average
    
    lrc = LinearRegression[length](src)
    lrc1 = lrc[1]
    lrs = lrc - lrc1
    TSF = LinearRegression[length](src) + lrs //Time Series Forecast
    
    HMA = WeightedAverage[Round(SQRT(length))](2 * WeightedAverage[length/2](src) - WeightedAverage[length](src)) //Hull Moving Average
    
    T3e1 = ExponentialAverage[length](src)
    T3e2 = ExponentialAverage[length](T3e1)
    T3e3 = ExponentialAverage[length](T3e2)
    T3e4 = ExponentialAverage[length](T3e3)
    T3e5 = ExponentialAverage[length](T3e4)
    T3e6 = ExponentialAverage[length](T3e5)
    
    T3c1 = -T3a1 * T3a1 * T3a1
    T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1
    T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1
    T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1
    T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3 //T3 Moving Average
    
    Once MA = 0
    If mav = 0 then
    MA = Average[length,0](src) //SMA
    ElsIf mav = 1 then
    MA = Average[length,1](src) //EMA
    ElsIf mav = 2 then
    MA = Average[length,2](src) //WEMA
    ElsIf mav = 3 then
    MA = xDEMA //DEMA
    ElsIf mav = 4 then
    MA = Average[Floor(length / 2) + 1](Average[Ceil(length / 2)](src)) //TMA
    ElsIf mav = 5 then
    MA = VAR //VAR a.k.a. VIDYA Variable Index Dynamic Moving Average
    ElsIf mav = 6 then
    MA = WWMA //WWMA Welles Wilder's Moving Average
    ElsIf mav = 7 then
    MA = xZLEMA //ZLEMA Zero Lag Exponential Moving Average
    ElsIf mav = 8 then
    MA = TSF //TSF Time Series Forecast
    ElsIf mav = 9 then
    MA = HMA //HMA Hull Moving Average
    ElsIf mav = 10 then
    MA = T3 //T3 Tillson Moving Average
    EndIf
    
    atr = AverageTrueRange[Periods](src)
    up = MA - Multiplier * atr
    up1 = up[1]
    If src[1] > up1 then
    up = Max(up, up1)
    Else
    up = up
    EndIf
    dn = MA + Multiplier * atr
    dn1 = dn[1]
    If src[1] < dn1 then
    dn = Min(dn, dn1)
    else
    dn = dn
    EndIf
    
    Once trend = 1
    If trend = -1 and Close > dn1 then
    trend = 1
    ElsIf trend = 1 and Close < up1 then
    trend = -1
    EndIf
    
    If Trend = 1 and Trend[1] = -1 then
    Buy PositionSize contract at Market
    EndIf
    
    If Trend = -1 and Trend[1] = 1 then
    SellShort PositionSize contract at Market
    EndIf
    
    GraHal, Nicolas and PLermite thanked this post
    #217647 quote
    GraHal
    Participant
    Master
    Thank You JS for converting the above code. Attached results on DJI M10 over 1K bars spread = 5.  .itf attached also. Only trouble is the System gets Rejected (insufficient history) at the 1st bar when code tries to execute. It is the System which spawned the Topic below
    Long Time to run System Code Causes Rejection?
    I sent in a Technical Report to PRT and got amended code back as below, only thing is the PRT amended code does not take any trades. I found this also (prior to Tech Report submission) … if the variable values used do not take trades then the System does not get Rejected.
    length = 30 //Moving Average Length
    Periods = 19 //ATR period
    Multiplier = 1.1 //ATR Multiplier
    So the values above are my values, but gets Rejected. Code below is PRT amended code after Tech Report.  The PRT version code is copied from an email. Would have been easier / more useful had PRT sent the .itf as the code below needs sorting due to line lengths etc. I have tried everything I can think of and got depressed (temporary! 🙂 ) yesterday over it, so I’m submitting here if anybody has any solution to the Rejection please? Who knows … if we find why Rejected with this System it may have knock-ons for other Systems which have been Rejected lately for insufficient history??
    DEFPARAM CUMULATEORDERS = False
    
    DEFPARAM PRELOADBARS = 10000
    
    src = Close //Data type
    
    mav = 1 //Moving Average 
    Value: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TMA, 5=VAR, 6=WWMA, 7=ZLEMA,
    8=TSF, 9=HMA, 10=T3 length = 100 //Moving Average Length Periods = 10 //ATR
    period Multiplier = 0.5 //ATR Multiplier
    
     
    
    T3a1 = 0.7 //Tillson T3 Volume Factor
    
     
    
    valpha = 2 / (length+1)
    
     
    
    PositionSize = 1 //Default PositionSize for trading
    
     
    
    If src > src[1] then //Green candle
    
    vud1 = src - src[1] //Green profit
    
    else
    
    vud1 = 0
    
    EndIf
    
     
    
    If src < src[1] then //Red candle
    
    vdd1 = src[1] - src //Red Loss
    
    else
    
    vdd1=0
    
    EndIf
    
     
    
    vUD = Summation[9](vud1) //Summation [9] Green profit vDD
    = Summation[9](vdd1) //Summation [9] Red Loss
    
     
    
    once vCMO=0
    
    if vud <>0 then
    
    vCMO = ((vUD - vDD) / vUD + vDD) //Cumulative Market
    Oscillator endif
    
     
    
    once VAR=0
    
    if barindex > 10 then
    
    VAR = (valpha * Abs(vCMO) * src) + (1 - valpha *
    Abs(vCMO)) *VAR[1] endif
    
     
    
    xDEMA = 2 * ExponentialAverage[length](src) -
    
    ExponentialAverage[length](ExponentialAverage[length](src))
    
     
    
    wwalpha = 1 //Length
    
     
    
    Once WWMA = 0
    
    if barindex >1 then
    
    WWMA = wwalpha * src + (1 - wwalpha) * WWMA[1] endif
    
     
    
    If (length / 2) = Round(length / 2) then //Check even/odd
    length for "zero lag"
    
    zxLag = (length / 2)
    
    Else
    
    zxLag = ((length-1) / 2)
    
    EndIf
    
     
    
    zxEMAData = src + src - src[zxLag]
    
    xZLEMA = ExponentialAverage[length](zxEMAData) //Zero Lag
    Exponential Average
    
     
    
    lrc = LinearRegression[length](src)
    
    lrc1 = lrc[1]
    
    lrs = lrc - lrc1
    
    TSF = LinearRegression[length](src) + lrs //Time Series
    Forecast
    
     
    
    HMA = WeightedAverage[Round(SQRT(length))](2 *
    WeightedAverage[length/2](src) -
    
    WeightedAverage[length](src)) //Hull Moving Average
    
     
    
    T3e1 = ExponentialAverage[length](src)
    
    T3e2 = ExponentialAverage[length](T3e1)
    
    T3e3 = ExponentialAverage[length](T3e2)
    
    T3e4 = ExponentialAverage[length](T3e3)
    
    T3e5 = ExponentialAverage[length](T3e4)
    
    T3e6 = ExponentialAverage[length](T3e5)
    
     
    
    T3c1 = -T3a1 * T3a1 * T3a1
    
    T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1
    
    T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 *
    T3a1
    
    T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 *
    T3a1
    
    T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 *
    T3e3 //T3 Moving Average
    
     
    
    Once MA = 0
    
    If mav = 0 then
    
    MA = Average[length,0](src) //SMA
    
    ElsIf mav = 1 then
    
    MA = Average[length,1](src) //EMA
    
    ElsIf mav = 2 then
    
    MA = Average[length,2](src) //WEMA
    
    ElsIf mav = 3 then
    
    MA = xDEMA //DEMA
    
    ElsIf mav = 4 then
    
    MA = Average[Floor(length / 2) + 1](Average[Ceil(length /
    2)](src)) //TMA ElsIf mav = 5 then MA = VAR //VAR a.k.a. VIDYA Variable Index
    Dynamic Moving Average ElsIf mav = 6 then MA = WWMA //WWMA Welles Wilder's
    Moving Average ElsIf mav = 7 then MA = xZLEMA //ZLEMA Zero Lag Exponential
    Moving Average ElsIf mav = 8 then MA = TSF //TSF Time Series Forecast ElsIf mav
    = 9 then MA = HMA //HMA Hull Moving Average ElsIf mav = 10 then MA = T3 //T3
    Tillson Moving Average EndIf
    
     
    
    atr = AverageTrueRange[Periods](src)
    
    up = MA - Multiplier * atr
    
    up1 = up[1]
    
    If src[1] > up1 then
    
    up = Max(up, up1)
    
    Else
    
    up = up
    
    EndIf
    
    dn = MA + Multiplier * atr
    
    dn1 = dn[1]
    
    If src[1] < dn1 then
    
    dn = Min(dn, dn1)
    
    else
    
    dn = dn
    
    EndIf
    
     
    
    Once trend = 1
    
    If trend = -1 and Close > dn1 then
    
    trend = 1
    
    ElsIf trend = 1 and Close < up1 then
    
    trend = -1
    
    EndIf
    
     
    
    If Trend = 1 and Trend[1] = -1 then
    
    Buy PositionSize contract at Market
    
    EndIf
    
     
    
    If Trend = -1 and Trend[1] = 1 then
    
    SellShort PositionSize contract at Market EndIf
    #217655 quote
    JS
    Participant
    Senior

    Hi GraHal,

    I only translated the code and didn’t participate in anything else, not “Live/Demo”…

    When I start the code in demo I get the same error message (insufficient historical data)…

    What strikes me in the statistics of the positions is that the average time between positions is a negative number (e.g. -8 sec / -30 bars)… I don’t know what to make of that but it’s not right…

    I think I’ve read something about it on the forum…

    GraHal thanked this post
    #217664 quote
    GraHal
    Participant
    Master
    average time between positions is a negative number (e.g. -8 sec / -30 bars)
    Are you getting above from attached in some way that I can’t see?? 🙂 0 bars is odd though as the Chart is on M10 TF so 30 mins should be 3 bars? The Algo is always onmarket. The 30 mins between trades shown on attached is at the start before it takes any trades. The really odd thing is that the Algo always gets Rejected at the end of the 1st bar when the Algo likely not even opening a trade (as in attached).
    #217666 quote
    JS
    Participant
    Senior

    Hi,

    Below is from a trading system that is “Live”…

    As you can see, the average time between positions is zero seconds…

    This system is also always on the market and alternates between Long and Short…

    #217668 quote
    GraHal
    Participant
    Master
    Algo always gets Rejected at the end of the 1st bar when the Algo likely not even opening a trade
    Hahaha after typing above, I thought … sooo what if the System did open a trade at the end of the 1st bar? I added below at top of the code and the System did not get Rejected either on Demo or on Live!! Success at last!!!!!  I’ll sleep better tonight!!! Thank You JS for cross-fertilising thoughts and ideas! Onwards & Upwards!!!!
    If  not onmarket Then
    Buy at Market
    Endif
    JS thanked this post
    #217670 quote
    JS
    Participant
    Senior

    Hi,

    Nice that it’s been fixed…

    I think the problems arise when in a trading system the positions go from long to short at once and vice versa… instead of closing the current position and then reversing…

    I have noticed in other systems that if a position is turns directly then the code is not read during the “Open”… So the orders are executed first and only then the code is read…

    You can check this when the size of your position is linked to the “StrategyProfit”,

    When the position turns directly (i.e. immediately from Long to Short or vice versa) then the “StrategyProfit” is not recalculated so the code is not read when the position is rotated directly…

    GraHal thanked this post
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

SuperTrended Moving Averages from Tradingview


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
AndrewtUK @andrewtuk Participant
Summary

This topic contains 9 replies,
has 4 voices, and was last updated by JS
2 years, 6 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 06/23/2023
Status: Active
Attachments: 7 files
Logo Logo
Loading...