*** This message is posted in Amibroker Community Forum ***

Hello,

Regarding to Amibroker code for Stocks & Commodities Traders’ Tips Issue 05/2015, the code seems malfunction. Enclosed please find the code regarding to my comments on modifying the code. However, the code seems running very very slow. Is it because of varget? I am using it in 1 min chart. How can I speed it up!

Please comment!

Regards,

Terence

```
SetBarsRequired( sbrAll, 0 );
function FindMiddlePIP( data, x0, x1, curdist )
{
bi = BarIndex();
y0 = data[ x0 ];
y1 = data[ x1 ];
line = y0 + ( y1 - y0 ) * ( bi - x0 ) / ( x1 - x0 );
distance = abs( data - line );
maxbars = HHVBars( distance, x1 - x0 );
pipbar = Nz( x1 - maxbars[ x1 ], -1 );
if ( pipbar != -1 )
VarSet( "curdist", distance[ pipbar ] ); // VarSet( curdist, distance[ pipbar ] );
return pipbar;
}
LogMode = ParamToggle( "Mode", "Linear|Logarithmic", 0 );
MaxLegs = Param( "MaxLegs", 20, 4, 500 );
// one leg first - from start to end
pips = 0;
pips[ 0 ] = 1;
pips[ BarCount - 1 ] = 1;
data = Close;
if ( LogMode ) data = log( Data );
for ( legs = 0; legs < maxlegs; legs++ )
{
x0 = 0;
x1 = -1;
oldpip = -1;
maxdist = 0;
for ( i = 0; i < BarCount; i++ )
{
if ( pips[ i ] )
{
x1 = i;
if ( x1 > x0 )
{
VarSet( "curdist", 0 ); //curdist = 0;
newpip = FindMiddlePIP( data, x0, x1, VarGet("curdist") ); // newpip = FindMiddlePIP( data, x0, x1, "curdist" );
if ( newpip != -1 && VarGet("curdist") > maxdist ) // if ( newpip != -1 && curdist > maxdist )
{
maxdist = VarGet("curdist"); // maxdist = curdist;
if ( oldpip != -1 )
pips[ oldpip ] = 0; // remove smaller one
pips[ newpip ] = 1;
oldpip = newpip;
}
x0 = x1; // next leg
x1 = -1; //
}
}
}
}
// draw lines connecting pip points
x0 = 0;
x1 = -1;
zzline = Null;
for ( i = 0; i < BarCount; i++ )
{
if ( pips[ i ] )
{
x1 = i;
line = LineArray( x0, data[ x0 ], x1, data[ x1 ] );
zzline = IIf( NOT IsNull( line ), line, zzline );
x0 = x1;
}
}
if ( LogMode )
zzline = exp( zzline );
Plot( zzline, "zzTop", colorRed, styleThick );
Plot( C, "Price", colorDefault );
```