I've been trying to write a script that, for a given segment of time (say BarNumber = x, to BarNumber = y), finds the price level at which the area of the price action above that level is equal to the area of the price action below that price level.
It's easy enough to write fold statements that calculate area above and below a given price level, but after that I run into problems. I'm new to thinkscript, so I'm hoping there's a way to do this I'm not familiar with. Since for this study I need to iterate through a price range to check if the area_above(p) = area_below(p), and I'm not aware of another way of iterating in thinkscript other than using fold, I wrote a script that is passed the relevant variables and calculates the areas, then tried to call that script in another fold statement (passing the index of that fold to the script to determine the price level) which would return either the price level if area_above = area_below or 0 otherwise. The problem is that the index of the final fold is not recognized as a variable for the script.
I've included the code below so you can see what I mean. I found a post from 2019 on stackOverflow asking about this problem and the answer at the time was "it's a known bug, no workaround exists," so I'm really hoping that someone's figured out a workaround since. (I get the same error as that post from 2019.)
Does anyone know of any workarounds? Or can think of another way to algorithmically find such a price level for a given chart segment? In the end I'd like to have a plot where I pass either a starting bar number X and ending bar number Y (or just an ending bar number Y, and X can be 1) and at each bar Z on that segment (X < Z < Y) it plots the price at which the price action area above that price is equal to the price action area under that price for segment Z to Y.
It's easy enough to write fold statements that calculate area above and below a given price level, but after that I run into problems. I'm new to thinkscript, so I'm hoping there's a way to do this I'm not familiar with. Since for this study I need to iterate through a price range to check if the area_above(p) = area_below(p), and I'm not aware of another way of iterating in thinkscript other than using fold, I wrote a script that is passed the relevant variables and calculates the areas, then tried to call that script in another fold statement (passing the index of that fold to the script to determine the price level) which would return either the price level if area_above = area_below or 0 otherwise. The problem is that the index of the final fold is not recognized as a variable for the script.
I've included the code below so you can see what I mean. I found a post from 2019 on stackOverflow asking about this problem and the answer at the time was "it's a known bug, no workaround exists," so I'm really hoping that someone's figured out a workaround since. (I get the same error as that post from 2019.)
Does anyone know of any workarounds? Or can think of another way to algorithmically find such a price level for a given chart segment? In the end I'd like to have a plot where I pass either a starting bar number X and ending bar number Y (or just an ending bar number Y, and X can be 1) and at each bar Z on that segment (X < Z < Y) it plots the price at which the price action area above that price is equal to the price action area under that price for segment Z to Y.
Code:
input fromBar = 1;
input toBar = 10;
input center_price = 400.00;
input price_range_low = 400.00;
input price_range_high = 410.00;
input band_increment = 0.25;
script areaDelta_hilo {
input index_map = 0;
input price_increment = 1;
input segStart = 0;
input segEnd = 10;
input range_bottom = 0;
input range_top = 10;
#### index_map is what will be passed to the script, is the index of the fold that returns either a price that meets the criteria or 0
def price = index_map * price_increment + range_bottom;
def above_price_highs = fold
i = 0 to BarNumber()
with j = 0
DO
if (
GetValue(BarNumber(), i) >= segStart
and
Getvalue(BarNumber(), i) < segEnd
and GetValue(high, i) > price,
j + GetValue(high, i) - price,
j);
def below_price_lows = fold
a = 0 to BarNumber()
with b = 0
DO
if (
GetValue(BarNumber(), a) >= segStart
and
GetValue(BarNumber(), a) < segEnd
and
GetValue(low, a) < price,
b + price - GetValue(low, a),
b);
plot area_delta = above_price_highs - below_price_lows;
}
def index_high = (1/band_increment)*(price_range_high - price_range_low);
def isMean = fold
index = 0 to index_high
with d = 0
DO
if (
areaDelta_hilo(index_map = index, price_increment = band_increment, segStart=fromBar, segEnd=toBar, range_bottom = price_range_low, range_top = price_range_high).area_delta == 0,
d + (d*band_increment) + price_range_low,
d);
### Thinkscript throws the error: No such variable: index.