Announcement

Collapse
No announcement yet.

True Range

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • True Range

    Hello,

    I use this function to calculate the True Range
    Code:
    function TrueRange(){
        var TRH = TRL = 0;
        //compute True High
        if (high(0)>close(-1)) {TRH = high(0);}
        else if(high(0)<close(-1)) {TRH = close(-1);}
        //compute True Low
        if (low(0)<close(-1)) {TRL = low(0);}
        else if(low(0)>close(-1)) {TRL = close(-1);}
        //compute True Range
        var TR  = TRH-TRL;    
        return (TR);
    }
    I call this function with efsInternal function and it works fine but sometimes I get some big spikes in the true range.
    Why does this happen and how can I make this better also from the point of view of efficiency?
    Also I am not sure but I think TRH and TRL should be local variables but I think that in my function TRL is global and I cannot understand why?

    Thank you in advance
    Raffaele

  • #2
    Raffaele
    The reason you are getting some spikes is because the conditions to calculate True High or True Low do not take into account that the current High or the Low could also be equal to the prior bar's Close. When that happens TRH or TRL will have a value of 0 (which you assigned to them when you declared them). To verify this add the following line of code just before the one in which you calculate TR
    PHP Code:
    debugPrintln(getCurrentBarIndex()+"  "+TRH+"  "+TRL); 
    You will see that on the bar where those spikes occur either TRH or TRL will have a value of 0
    As to making this more efficient there are various solutions.
    The simplest one by far is to just use the built-in atr() function and set the length to 1. You can do this directly in main without having to use the efsInternal() function to create the series (I am assuming this is the reason why you are using efsInternal())
    If instead you have some other reason to compute the function separately then you could simplify it in the following way
    PHP Code:
    function TrueRange(){
        return 
    Math.max((high(0)-low(0)),Math.abs(close(-1)-high(0)),Math.abs(close(-1)-low(0)));

    Either solution will resolve your problem and is likely more efficient than what you are currently using
    As to the scope of the TRL variable you are not explicitly declaring it within the function using a var statement [in that line you are only assigning it to TRH and assigning to it the value 0] hence it is [implicitly] declared as a global variable.
    You should write that line of code either as
    var TRH = 0;
    var TRL = 0;

    or as
    var TRH=0, TRL=0;
    If you search the net you will find a number of articles related to the importance of properly declaring variables in JavaScript to avoid issues such as this one or hoisting, etc.
    Hope this helps
    Alex

    Comment


    • #3
      Hello Alexis,

      Thank you very much as always for your responce and for the solution of using the built in atr study.
      I agree that it is much easier and better to use.
      Also, thank you for the explanation about declaring variables.
      Raffaele

      Comment


      • #4
        Raffaele
        As always you are most welcome and thank you for the feedback
        Alex

        Comment

        Working...
        X