Convert Loop to Array- Inside Bar Clusters

Hi there!

I have "starred myself blind" on this Loop to Array conversion problem, so I need your help to get a working Array solution:

An Inside Bar is defined as follows: InsideBar = High <= Ref( High, -1 ) AND Low >= Ref( Low, - 1 );

Let's expand that and say that every bar which has a high and a low within the initiating bar's High and Low (that gave us the first Inside Bar) is defined as an Inside Bar Cluster.

To make it clear what I mean, this picture explains it better:

image

An Inside Bar Cluster can be from 1 bar to many, many bars! As soon as a candle's high Or low crosses the Initiating bar's High Or Low, that's the end of the Inside Bar Cluster.

This is a fully working loop and I want to convert this loop to an Array solution instead:

PrcH 			= High;
PrcL				= Low;
HighPoint 		= PrcH;
LowPoint		= PrcL;
    
// locate the inside bar "clusters"
// the inside bar can be a sequence or cluster of bars
barpriorto_insidebarcluster = 0;
insidebarcluster = 0;
insidebarclusterH = 0;
insidebarclusterL = 0;
cntarray = 0;
newH = HighPoint;
newL = LowPoint;
idx = 0;
idxH = HighPoint[0];
idxL = LowPoint[0];
Hloop = 0;
Lloop = 1e12;

for( i = 0; i < BarCount; i++ )
{
    if( PrcH[i] <= idxH AND PrcL[i] >= idxL )
    {
        insidebarcluster[i] = 1;
        barpriorto_insidebarcluster[idx] = 1;
        cntarray[idx] = cntarray[idx] + 1;
        newH[i] = idxH;
        newL[i] = idxL;
        Hloop = Max( Hloop, PrcH[i] );
        Lloop = Min( Lloop, PrcL[i] );
        insidebarclusterH[idx] = Hloop;
        insidebarclusterL[idx] = Lloop;
    }
    else
    {
        idx = i;
        idxH = PrcH[i];
        idxL = PrcL[i];
        Hloop = 0;
        Lloop = 1e12;
    }
}

BarColor	= IIf( InsideBarCluster, colorWhite, colorBlack );
Plot( Close, "Close", BarColor, styleCandle, Null, Null, Null, 1, 1 );

I hope that it is clear what I am trying to achieve and if not please let me know.

Thanks and Regards,

Jorgen

Here is array solution using ApplyStop function.
Yellow lines are the lines that have to be broken to upside or to the downside.
Those are the ones marking the InsideBar cluster.
Red Up/down arrows mark the break of cluster.

/// BREAK OF INSIDE BAR CLUSTER
/// @link https://forum.amibroker.com/t/convert-loop-to-array-inside-bar-clusters/25536/2
/// by fxshrat@gmail.com
SetBacktestMode(backtestRegular);
SetOption("ActivateStopsImmediately", 0);
SetPositionSize(1, spsShares);
SetTradeDelays(0, 0, 0, 0);

TickSize = 0.01;

InsideBar = High <= Ref(High, -1) AND Low >= Ref(Low, -1);

BuyPrice = Close;
Buy = InsideBar;
Sell = 0;

amount1 = (Ref(H,-1)-BuyPrice);
amount2 = (BuyPrice-Ref(L,-1));
ApplyStop(stopTypeProfit, stopModePoint, amount1+TickSize, 1);
ApplyStop(stopTypeLoss, stopModePoint, amount2+TickSize, 1);

eq = Equity(1,0);

intrade = Flip(Buy,Sell) OR Sell>0;
hh = IIf(intrade, ValueWhen(Buy, Ref(H,-1)), Null);
ll = IIf(intrade, ValueWhen(Buy, Ref(L,-1)), Null);

Plot( C, "Price", colorDefault, styleCandle );
Plot( hh, "hh", colorGold, styleStaircase );
Plot( ll, "hh", colorGold, styleStaircase  );
PlotShapes( Buy * shapeUpArrow, colorGold, layer = 0, L );
PlotShapes( (Sell==2) * shapeDownArrow,colorRed, 0,H );
PlotShapes( (Sell==3) * shapeUpArrow, colorRed, 0, L );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );

28

2 Likes

Here is small addition to show cluster bars with different bar outline color.
So should be more clear now to see what is what.

/// BREAK OF INSIDE BAR CLUSTER
/// @link https://forum.amibroker.com/t/convert-loop-to-array-inside-bar-clusters/25536/2
/// by fxshrat@gmail.com
SetBacktestMode(backtestRegular);
SetOption("ActivateStopsImmediately", 0);
SetPositionSize(1, spsShares);
SetTradeDelays(0, 0, 0, 0);

TickSize = 0.01;

InsideBar = High <= Ref(High, -1) AND Low >= Ref(Low, -1);

BuyPrice = Close;
Buy = InsideBar;
Sell = 0;

amount1 = Ref(H,-1)-BuyPrice;
amount2 = BuyPrice-Ref(L,-1);
ApplyStop(stopTypeProfit, stopModePoint, amount1+TickSize, 1);
ApplyStop(stopTypeLoss, stopModePoint, amount2+TickSize, 1);

eq = Equity(1,0);

intrade = Flip(Buy,Sell);
hh = IIf(intrade OR Sell>0, ValueWhen(Buy, Ref(H,-1)), Null);
ll = IIf(intrade OR Sell>0, ValueWhen(Buy, Ref(L,-1)), Null);

Plot(C, "Price", IIf(intrade, colorWhite, colorGrey50), styleCandle);
Plot(hh, "hh", colorGold, styleStaircase);
Plot(ll, "hh", colorGold, styleStaircase);
PlotShapes(Buy * shapeUpArrow, colorGold, layer = 0, L);
PlotShapes((Sell==2) * shapeDownArrow,colorRed, 0,H);
PlotShapes((Sell==3) * shapeUpArrow, colorRed, 0, L);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
//   

28

11 Likes

Hi fxshrat,

That was a fast reply! Nicely done and it works great!

Thank you very much!!!

Regards,

Jorgen

2 Likes

Brilliant approach! A small oversight:

Plot(ll, "ll", colorGold, styleStaircase);

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