Is barindex an integer? lowest sees it differently

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #209476 quote
    Wim
    Participant
    Junior

    I am having some problems with Lowest and Highest functions in my indicator. Every now and then the indicator crashes with the error message that Lowest (or Highest) requires a positive integer as parameter. So i wrote some lines to test this further and find the culprit. And . . . the culprit seems to be “barindex”. The code passes the variable numberBars, being the difference between actual bar and a historic bar, to the function Lowest. When I supply the bar numbers of these 2 bars myself to Lowest, no problem. When I ask the indicator to calculate the numberBars as the difference of “barindex – historicbarindex”, pang, error message, indicator wiped of the chart. I tried to rescue the situation by adding Floor(substraction,0) to the code. But Lowest stubbornly interprets this numberBars also as a non-integer. What am I missing here?

    My test was on the DAX 1 minute chart this morning, at barindex 1503. With the line currentIndex=barindex as comment, it returns 1503 and 103 as barindex and 103 as numberBars (screenshot 1). When I uncomment the line, the indicator crashes (screenshot 2).

    Thanks for helping me out here.

    // WM-LowestHighest
    //
    startIndex   = 1400
    currentIndex = 1503
    //currentIndex = barindex   // un-comment for testing with barindex instead of fixed value
    
    numberBars = floor(currentIndex - startIndex,0)
    
    lowestValue = lowest[numberBars]
    highestValue = highest[numberBars]
    
    lowestValue = 15499
    highestValue = 15799
    
    return barindex as "barindex",numberBars as "numberBars", lowestValue as "lowestValue", highestValue as "highestValue"
    #209500 quote
    robertogozzi
    Moderator
    Master

    Try replacing line 7 with:

    numberBars = currentIndex - startIndex
    #209510 quote
    Wim
    Participant
    Junior

    Thanks Roberto. But that was how I started. I added the Floor-trick hoping that would definitely make numberBars an integer. To me it’s an integer, but not to Lowest and Highest. Next tip?

    #209513 quote
    JC_Bywan
    Moderator
    Master

    When you use currentIndex = barindex, for any bar in history lower than or equal to your startindex=1400, then your numberbars= currentIndex – startIndex is negative, or at best, zero (with or without floor, doesn’t matter it’s not about the decimals), and it is prevented by the error message because it makes no sense to use a negative parameter for lowest or highest, even if its absolute value is an integer, it has to be a positive number of candles. You can fix it by not calculating lowest/highest as long as numberBars is negative or 0 with:

    startIndex = 1400
    //currentIndex = 1503
    currentIndex = barindex
    
    numberBars = currentIndex - startIndex
    
    if numberbars>0 then
     lowestValue = lowest[numberBars]
     highestValue = highest[numberBars]
    endif
    Wim and robertogozzi thanked this post
    #209523 quote
    Wim
    Participant
    Junior

    Thank you for pointing out what I wasn’t seeing. It is so basic, but still missed it completely this time.

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

Is barindex an integer? lowest sees it differently


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Wim @wim Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/10/2023
Status: Active
Attachments: 2 files
Logo Logo
Loading...