Reputation: 595
I'm trying to create a screener. It starts with plotting:
(a) Two red straight horizontal lines marking the highest high and the lowest low of the lookback period.
(b) Two blue straight horizontal lines marking 25% and 75% points from this range.
Please, check the screenshot.
My issue is: it should mark (plotchar) when ALL the four condition below are met, but it's not working atm.
(1) In the first half of the period, high has been in the upper range at least once.
(2) In the first half of the period, low has been in the lower range at least once.
(3) In the second half of the period, high has been in the upper range at least once.
(4) In the second half of the period, low crosses-under lower the range line.
Unless I'm missing something, all these four conditions are met in the screenshot, therefore it should be char-plotted in candle|4| (when LOW CROSSES-UNDER LOW RANGE LINE), but nothing was plotted.
Any help will be highly appreciated!
Below, the relevant part of the code, but all code is here if you'd like to check and/or re-use.
lookBack = input(title="Lookback", type=input.integer, defval=24, minval=2)
range = input(title="Upper & Lower Range", type=input.integer, defval=4, minval=10)
// INDICATORS & VARIABLES
highestHigh = highest(high, lookBack)
lowestLow = lowest(low, lookBack)
xAxisStartsAt = bar_index[lookBack]
xAxisFinishesAt = bar_index
upperLimit = highestHigh
lowerLimit = lowestLow
upperRange = ((highestHigh - lowestLow)/range) + lowestLow
lowerRange = highestHigh - ((highestHigh - lowestLow)/range)
HighAboveUpperRange = high > upperRange
LowBelowLowerRange = low < lowerRange
occurrencesAboveTotal = sum(HighAboveUpperRange ? 1 : 0, lookBack)
occurrencesAboveSecondHalf = sum(HighAboveUpperRange ? 1 : 0, lookBack/2)
occurrencesAboveFirstHalf = occurrencesAboveTotal - occurrencesAboveSecondHalf
occurrencesBelowTotal = sum(LowBelowLowerRange ? 1 : 0, lookBack)
occurrencesBelowSecondHalf = sum(LowBelowLowerRange ? 1 : 0, lookBack/2)
occurrencesBelowFirstHalf = occurrencesBelowTotal - occurrencesBelowSecondHalf
// STRATEGY
triggerA = occurrencesAboveFirstHalf >= 1 ? true : false
triggerB = occurrencesAboveSecondHalf >= 1 ? true : false
triggerC = occurrencesBelowFirstHalf >= 1 ? true : false
triggerD = crossunder(low, lowerRange)
condition = triggerA and triggerB and triggerC and triggerD
// PAINTBRUSH
plotchar(condition)
Update
In the second and third screenshots, stars (plotchar) were plotted, but not where they were expected (they are expected when LOW crosses under the lower blue line/lower range (condition 4).
Upvotes: 0
Views: 204
Reputation: 2568
In your script, the formulas for determining the range are mixed up, you have so
upperRange = ((highestHigh - lowestLow)/range) + lowestLow
lowerRange = highestHigh - ((highestHigh - lowestLow)/range)
but it should be like this
lowerRange = ((highestHigh - lowestLow)/range) + lowestLow
upperRange = highestHigh - ((highestHigh - lowestLow)/range)
Add debug lines to your script, you will see a lot of useful information
plot(lowerRange,color=color.red)
plot(upperRange,color=color.green)
plot(low, color=color.black)
I'm not sure, but it seems to me that crossunder(low, lowerRange)
this is not the condition you would like to test.
Good luck.
Upvotes: 1