# Looking for TOS similar code in Amibroker

Hi there,
I'm looking for TOS similar function in Amibroker. The following is the function

``````GetValue(high, InsideBars + 1, 0)
``````

It is use in this formula

``````input MinCompressionLength = 4;
input BubbleAbove = yes;
input TurnOffPlot = no;

def InsideBars =
if InsideBars == 0 then
if MAX(Open,Close) <= high and Min(Open,Close) >= low then 1
else 0
else
if MAX(Open,Close) <= GetValue(high, InsideBars + 1, 0) and
Min(Open,Close) >= GetValue(low, InsideBars + 1, 0)
then InsideBars + 1
else 0;

AddChartBubble(!TurnOffPlot and InsideBars >= MinCompressionLength and !InsideBars[-1], high,
InsideBars, Color.GRAY, BubbleAbove);
``````

This is what it does This formula look for inside bar and number them starting from the Anchor bar. I'm actually looking how to define this Anchor bar in amibroker. I always get looped when dealing with formula that go in a loop, as in this case so appreciate all help to get me out of this loop Thanks to all

several ways you can do it.

first you have the shortest:

``````nback = 4;
nr4b = HHVBars( H, ( nback + 1 ) ) == nback AND LLVBars( L, ( nback + 1 ) ) == nback;
PlotShapes( IIf( nr4b, shapeUpArrow, shapeNone ), colorOrange, 0, L, -30 );
``````

only problem here is that if between your anchor bar and the bar your "4 inside" bar there is another bar with the same High price it will ignore this bar and still use the anchor bar.

So to solve this I used this post: press link

then the code becomes:

``````// see: https://forum.amibroker.com/t/hhv-and-barssince-issue/8006/14
function qHHVBars( array, period )
{
rev_hhvb = Reverse( HHVBars( Reverse( array ), period ) );
a = period - 1;
return a - Ref( rev_hhvb, -a );
}
function qLLVBars( array, period )
{
rev_hhvb = Reverse( LLVBars( Reverse( array ), period ) );
a = period - 1;
return a - Ref( rev_hhvb, -a );
}

nback = 4;
nr4b = qHHVBars( H, ( nback + 1 ) ) == nback AND qLLVBars( L, ( nback + 1 ) ) == nback;
PlotShapes( IIf( nr4b, shapeUpArrow, shapeNone ), colorOrange, 0, L, -30 );
``````

but you can also use:

``````nback = 4;
VarSet( "nr" + 0, H < Ref( H, -nback ) AND L > Ref( L, -nback ) );

for( i = 1; i < nback; i++ )
{
VarSet( "nr" + i, Ref( H, -i ) < Ref( H, -nback ) AND Ref( L, -i ) > Ref( L, -nback ) AND VarGet( "nr" + ( i - 1 ) ) );
}

VarSet( "nr" + nback + "a", VarGet( "nr" + ( nback - 1 ) ) );
PlotShapes( IIf( nr4a, shapeUpArrow, shapeNone ), colorYellow, 0, L, -15 );
``````
3 Likes

The formula looks for max/min of Open/Close being smaller/greater than High/Low of n-bars back.

Here you go.

``````/// @link https://forum.amibroker.com/t/looking-for-tos-similar-code-in-amibroker/24560/3
/// max/min of Open/Close inside High/Low of n-bars back
n = 4;
hh = Ref(H,-n);
ll = Ref(L,-n);
max_oc = Max(O,C);
min_oc = Min(O,C);
for( i = 0, cs = 0; i < n; i++ )
cs += Ref(min_oc,-i) >= ll AND Ref(max_oc,-i) <= hh;
cond = cs == n;

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( cond * shapeUpArrow, colorGreen, 0, L );
``````

Or without loop:

``````/// @link https://forum.amibroker.com/t/looking-for-tos-similar-code-in-amibroker/24560/3
/// max/min of Open/Close inside High/Low of n-bars back
n = 4;
hh = Ref(H,-n);
ll = Ref(L,-n);
max_oc = HHV(Max(O,C),n);
min_oc = LLV(Min(O,C),n);
cond = min_oc >= ll AND max_oc <= hh;

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( cond * shapeUpArrow, colorPaleGreen, 0, L );
``````
6 Likes

Hi Guys,
Thank for helping a 'looper' getting out of the loop... To empottasch - not only did u take care of the code... U given further solution to problem I most likely to encounter later... Hope I could reach you level someday of forecasting problem before it happen...

To fxhrat,
As always, ur code is short and sweet....
Like the nice twist to show how it can be written with and without loop...

Always a great learning lesson for me to further my programming skill from this example.   1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.