I can’t code same supertrend as PRT

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

    Hi everyone!

    I’m trying to code the prorealtime supertrend in python with the native tws api.

    I noticed my ATR which is essential for calculating the supertrend it sometimes very far from the PRT indicator.
    I have tried several different functions for calculating ATR but nothing helps.

    Is it possible to find the source code for ATR or supertrend?
    Perhaps have you another way of thinking ?

    Thanks everyone
    Regards,
    Ben

    #180694 quote
    robertogozzi
    Moderator
    Master

    There you go:

    // Super Trend custom
    //
    // https://stackoverflow.com/questions/44935269/supertrend-code-using-pandas-python
    //
    //Multiplier     = 3
    //Periods        = 10
    Multiplier       = Max(1,Min(999,Multiplier))
    Periods          = Max(1,Min(999,Periods))
    IF BarIndex > Max(Periods,Multiplier) THEN
       MyATR         = AverageTrueRange[Periods](close) * Multiplier
       BasicUPPER    = MedianPrice + MyATR
       BasicLOWER    = MedianPrice - MyATR
       IF (BasicUPPER < FinalUPPER[1]) OR (close[1] > FinalUPPER[1]) THEN
          FinalUPPER = BasicUPPER
       ENDIF
       IF (BasicLOWER > FinalLOWER[1]) OR (close[1] < FinalLOWER[1]) THEN
          FinalLOWER = BasicLOWER
       ENDIF
       IF (ST[1] = FinalUPPER[1]) AND (close <= FinalUPPER) THEN
          ST = FinalUPPER
       ELSE
          IF (ST[1] = FinalUPPER[1]) AND (close > FinalUPPER) THEN
             ST = FinalLOWER
          ELSE
             IF (ST[1] = FinalLOWER[1]) AND (close >= FinalLOWER) THEN
                ST = FinalLOWER
             ELSE
                IF (ST[1] = FinalLOWER[1]) AND (close < FinalLOWER) THEN
                   ST = FinalUPPER
                ENDIF
             ENDIF
          ENDIF
       ENDIF
    ENDIF
    RETURN ST AS "Super Trend"
    #180724 quote
    Ben3211
    Participant
    New

    Hi robertogozz

    Yes it’s almost what i need, I would like to have a look inside “AverageTrueRange
    I will try some ideas from your stackoverflow link

    Thanks !

    #180733 quote
    robertogozzi
    Moderator
    Master

    Atr (and TR):

    // ATR formula
    //
    // https://en.wikipedia.org/wiki/Average_true_range
    // https://www.investopedia.com/terms/a/atr.asp
    //
    // TR calculations:
    //
    //        MyTR = max(Range,max(abs⁡ (high − close[1]),abs⁡ (low − close[1])))
    //
    //        The true range is the largest of the:
    //         - Most recent period's high minus the most recent period's low
    //         - Absolute value of the most recent period's high minus the
    //           previous close
    //         - Absolute value of the most recent period's low minus the
    //           previous close
    //
    // Calculation of ATR (of P periods):
    //p    = 10
    p      = max(2,min(999,p))               //range 2 - 999
    MyTR   = max(Range,max(abs(high - close[1]),abs(low - close[1])))
    IF BarIndex < p THEN
       MyATR = MyTR
    ELSE
       MyATR = ((MyATR[1] * (p - 1)) + MyTR) / p
    ENDIF
    RETURN MyATR
    Ben3211 thanked this post
    #180743 quote
    Ben3211
    Participant
    New

    Thanks robertogozz

    i’ll studied that !

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

I can’t code same supertrend as PRT


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Ben3211 @ben3211 Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by Ben3211
4 years, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/31/2021
Status: Active
Attachments: No files
Logo Logo
Loading...