Current Daily Bar within one third of the 250-days low

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

    Hello Pro Real Code community,

     

    I am writing an indicator to show me a Yes/No on whether the current candle is within one third of the yearly low. The year is defined as the 250 days prior to the current candle.

     

    The following code shows how far I have gotten, but for every stock it returns “0” though I know that many of those stocks are within one third of the 250-days low.

    for i = 250 to BarIndex do
    yearlyLow = low[i]
    yearlyLowDate = date[i]
    if low[i] < yearlyLow then
    yearlyLow = low[i]
    yearlyLowDate = date[i]
    endif
    next
    
    lastThirdStartDate = date[83]
    
    withinOneThird = 0
    if yearlyLowDate >= lastThirdStartDate then
    withinOneThird = 1
    endif

    Any chance we can find out wherein the problem lies?

    #126949 quote
    Vonasi
    Moderator
    Master

    For a starter

    for i = 250 to BarIndex do

    should be

    for i = barindex-250 to BarIndex do

    I would code it like this (not tested):

    yearlylow = low[250]
    for i = barindex-250 to BarIndex 
    yearlyLow = min(yearlylow,low[i])
    if yearlylow = low[i] then
    yearlyLowDate = date[i]
    endif
    next
     
    lastThirdStartDate = date[83]
     
    withinOneThird = 0
    if yearlyLowDate >= lastThirdStartDate then
    withinOneThird = 1
    endif
    
    return withinOneThird
    #126986 quote
    Roberto
    Participant
    Junior

    In response to your message Vonasi, I have put together the following:

    j = 250
    if BarIndex < 250 then
    j = BarIndex
    endif
    
    yearlylow = low[j]
    for i = BarIndex - j to BarIndex
    yearlyLow = min(yearlyLow, low[i])
    if yearlyLow = low[i] then
    yearlyLowDate = date[i]
    endif
    next
    
    lastThirdStartDate = date[83]
    
    withinOneThird = 0
    if yearlyLowDate >= lastThirdStartDate then
    withinOneThird = 1
    endif
    
    drawtext("withinOneThird = #withinOneThird#",barindex,0,Dialog,Bold,15)
    return

    Unfortunately, the indicator always returns “0”, even testing it on stocks at their 52-week lows. Any further ideas on what we may be missing here?

     

    Could it be related to this line?:

    if yearlyLowDate >= lastThirdStartDate then

    Perhaps the dates are not expressed properly.

    #126993 quote
    Vonasi
    Moderator
    Master

    Sorry – I shouldn’t try to count backwards in code after a couple of drinks!

    This works and is much faster:

    j = 250
    if BarIndex < j and barindex > 1 then
    j = BarIndex-1
    endif
    
    if barindex > 1 then
    yearlylow = lowest[j](low)
    for i = 0 to j
    if yearlyLow = low[i] then
    yearlyLowDate = date[i]
    break
    endif
    next
    
    lastThirdStartDate = date[83]
    
    withinOneThird = 0
    if yearlyLowDate >= lastThirdStartDate then
    withinOneThird = 1
    endif
    endif
    
    //drawtext("withinOneThird = #withinOneThird#",barindex,0,Dialog,Bold,15)
    return withinOneThird
    #127176 quote
    Roberto
    Participant
    Junior

    Indeed it does work as intended!

     

    Your help has been of great use Vonasi. Thank you. May you have strength in these global moments of difficulty.

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

Current Daily Bar within one third of the 250-days low


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by Roberto
5 years, 11 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/19/2020
Status: Active
Attachments: No files
Logo Logo
Loading...