I am trying to identify and group similar values that i call cluster. It looks something like above. A PF has a value PL. If previous PF is PL distance (or nearer) before it than the two PFs are same cluster. The chart is self explanatory.
Below is the code that does it. The result is perfect. But I want to remove the loop. Sample data is attached. Open it in 4 hours interval chart.
_TRACE( "!CLEAR!" );
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
PlotOHLC( O, H, L, C, "Close", colorRed, styleBar , Null, Null, 0, 1, 1 );
_SECTION_END();
SetBarsRequired( sbrAll, sbrAll );
largenumber = 1e9;
_TRACE( "BarIndex()=" + BarIndex() );
PL = 0;
PF =/*Cluster_1*/( BarIndex() == 24 || BarIndex() == 27 || BarIndex() == 30 || BarIndex() == 31 || BarIndex() == 33 || BarIndex() == 40 )
|| /*Cluster_2*/( BarIndex() == 55 || BarIndex() == 57 || BarIndex() == 58 )
|| /*Cluster_3*/( BarIndex() == 70 || BarIndex() == 71 )
|| /*Cluster_4*/( BarIndex() == 82 )
|| /*Cluster_5*/( BarIndex() == 103 || BarIndex() == 108 || BarIndex() == 115 )
|| /*Cluster_6*/( BarIndex() == 117 || BarIndex() == 119 );
//cluster_1
PL = IIf( BarIndex() == 24, 15, PL );
PL = IIf( BarIndex() == 27, 20, PL );
PL = IIf( BarIndex() == 30, 21, PL );
PL = IIf( BarIndex() == 31, 26, PL );
PL = IIf( BarIndex() == 33, 15, PL );
PL = IIf( BarIndex() == 40, 15, PL );
//cluster_2
PL = IIf( BarIndex() == 55, 10, PL );
PL = IIf( BarIndex() == 57, 20, PL );
PL = IIf( BarIndex() == 58, 23, PL );
//cluster_3
PL = IIf( BarIndex() == 70, 10, PL );
PL = IIf( BarIndex() == 71, 11, PL );
//cluster_4
PL = IIf( BarIndex() == 82, 7, PL );
//cluster_5
PL = IIf( BarIndex() == 103, 20, PL );
PL = IIf( BarIndex() == 108, 25, PL );
PL = IIf( BarIndex() == 115, 26, PL );
//cluster_6
PL = IIf( BarIndex() == 117, 1, PL );
PL = IIf( BarIndex() == 119, 3, PL );
barsSincePF = Ref( BarsSince( PF ), -1 );
ClusterID = PF * Cum( PF );
previousClusterID = ValueWhen( PF, ClusterID, 2 );
//how do i remove this loop?
for (i=1;i<50;i++)
{
clusterID = IIf( PF, IIf( clusterID != 1, IIf( barsSincePF < PL, previousClusterID, clusterID ), 1 ), Null );
previousClusterID = ValueWhen( PF, ClusterID, 2 );
clusterID = IIf( PF, IIf( clusterID != 1, IIf( clusterID != previousClusterID, IIf( ( clusterID - previousClusterID ) > 1, previousClusterID + 1, clusterID ), clusterID ), clusterID ), Null );
}
clusterID = IIf( IsNull( clusterID ), largenumber, clusterID );
barsSinceLastCluster = BarsSinceCompare( clusterID, "<", clusterID );
clusterID = IIf( clusterID == largenumber, null, clusterID );
clusterSize = IIf( clusterID == 1, Cum( PF ), Sum( PF, barsSinceLastCluster ) );
SizeOfLargestCluster = LastValue( Highest( clusterSize ) );
_TRACE( "clusterID=" + clusterID + ", PL="+PL+", clusterSize=" + clusterSize + ", barsSinceLastCluster=" + barsSinceLastCluster + ", SizeOfLargestCluster=" + SizeOfLargestCluster );
atr1 = ATR( 5 ) * 0.8;
PlotShapes( PF * shapeCircle, colorPink, 0, L - atr1, 0 );
Plot( BarIndex(), "barindex", colorwhite, styleNoLine + styleNoLabel + styleNoRescale );