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?
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
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.
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
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.