Conversion TrailingSL from TradingView

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #240233 quote
    Alera15Alera15
    Participant
    New

    Hi there!

    I was wondering if any coding expert could help convert this code from TradingView.

    Thanks in advance!

    Cheers,
    A

    //@version=5
    indicator("1.5 ATR From High and -1.5 ATR From Low", shorttitle = "TSL", overlay=true)
    
    // Input for ATR length
    atrLength = input.int(21, title="ATR Length")
    
    // Calculate the ATR
    atrValue = ta.atr(atrLength)
    
    // Variables to store the last higher low, last lower high, and their corresponding ATR lines
    var float lastHigherLow = na
    var float lastLowerHigh = na
    var float atrLineLow = na
    var float atrLineHigh = na
    
    // Detect higher low and lower high
    if (low > low[1] and (na(lastHigherLow) or low > lastHigherLow))
    lastHigherLow := low
    atrLineLow := lastHigherLow - 1.5 * atrValue
    
    if (high < high[1] and (na(lastLowerHigh) or high < lastLowerHigh)) lastLowerHigh := high atrLineHigh := lastLowerHigh + 1.5 * atrValue // Recalculate the lines if they're broken by a bar if (low < atrLineLow) lastHigherLow := low atrLineLow := lastHigherLow - 1.5 * atrValue if (high > atrLineHigh)
    lastLowerHigh := high
    atrLineHigh := lastLowerHigh + 1.5 * atrValue
    
    // Plot the -1.5 ATR from the low when a higher low occurs
    plot(na(atrLineLow) ? na : atrLineLow, color=color.red, linewidth=2, title="-1.5 ATR From Low")
    
    // Plot the 1.5 ATR from the high when a lower high occurs
    plot(na(atrLineHigh) ? na : atrLineHigh, color=color.blue, linewidth=2, title="1.5 ATR From High")

    For future posts use the Add PRT Code button.

    #240290 quote
    Iván GonzálezIván González
    Moderator
    Legend

    here you have the code

    once lastlowerhigh=high
    once lasthigherlow=low
    
    atrLength=21
    atrValue=averagetruerange[atrLength](close)
    
    if low>low[1] and low>lastHigherlow then
    lastHigherLow=low
    atrLineLow=lastHigherLow-1-5*atrValue
    endif
    
    if high<high[1] and high<lastLowerhigh then
    lastlowerhigh=high
    atrlinehigh=lastlowerhigh+1.5*atrvalue
    endif
    
    return atrlinelow as "-1.5 ATR from low"coloured("red"), atrlinehigh as "+1.5 ATR from High" coloured("blue")
    Alera15 thanked this post
    #240293 quote
    Alera15Alera15
    Participant
    New

    Thank you, but unfortunately, it doesn’t work. See the attachment for the differences between the TradingView and ProRealTime indicators.

    2024-11-12-16_07_12-CROWN-HOLDINGS-INC.-Giornaliero-9407-072-11-nov-2024.png 2024-11-12-16_07_12-CROWN-HOLDINGS-INC.-Giornaliero-9407-072-11-nov-2024.png 2024-11-12-16_07_26-CCK-9447-▲-0.43-Squeeze.png 2024-11-12-16_07_26-CCK-9447-▲-0.43-Squeeze.png
    #240305 quote
    Iván GonzálezIván González
    Moderator
    Legend

    It's strange because the code I sent you matches the one on Tradingview. Since I didn't have an image of the indicator, I installed it on the TV and the results were identical… Ok. Now with the image you attached I understand what you want to program. Here is the code:

    atrLength=21
    atrValue=averagetruerange[atrLength](close)
    //---ATR Line Low
    once atrLineLow=0
    if low < atrLineLow[1] then
    atrLineLow=low-1.5*atrValue
    elsif low-1.5*atrValue>atrLineLow[1] then
    atrLineLow=low-1.5*atrValue
    else
    atrLineLow=atrLineLow
    endif
    //---ATR Line High
    once atrLineHigh=999999
    if high>atrLineHigh[1] then
    atrLineHigh=high+1.5*atrValue
    elsif high+1.5*atrValue<atrLineHigh[1] then
    atrLineHigh=high+1.5*atrValue
    else
    atrLineHigh=atrLineHigh
    endif
    //---Plot Lines when defined
    if atrLineLow<>0 and atrLineHigh<>999999 then
    LineLow=atrLineLow
    LineHigh=atrLineHigh
    else
    LineLow=undefined
    LineHigh=undefined
    endif
    //--------------------------------------------//
    return LineLow coloured("red"), LineHigh coloured("blue")
    
    Alera15 thanked this post
    #240340 quote
    Alera15Alera15
    Participant
    New

    Now it works perfectly, thank you so much!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Conversion TrailingSL from TradingView


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Alera15 @alera15 Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by Alera15Alera15
1 year, 10 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 11/11/2024
Status: Active
Attachments: 2 files
ProRealCode ProRealCode
Loading...