Trendline fan code

Hi, I've been trying for some time to 'invert' this code so that is plots above the price (resistance trendlines) rather than below (support). The code was kindly shared by Ed Pottasch.

I have tried several combinations of substituting px for tx, pk for tr, > for < and so on. Here's the original code; hope you can help, thanks.

// E.M.Pottasch, 1/2019

rightstrength =1;// Param( "Right Strength", 1, 2, 50, 1 );
leftstrength = 1;//Param( "Left Strength", 1, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );
lookBackPeriod = param( "Lookback Period", 2000, 2, 2000, 1 );
tolerance = param( "Tolerance (%)", 0, 0, 5, 0.1 );
nlines = param( "Max. Number of Trendlines", 100, 1, 150, 1 ); // max number of trendlines to have on display in chart
trendlinePriceCheck = ParamToggle( "Trendline Price Check", "Use Close|Use Low", 1 );
dataOnDisplay = ParamToggle( "Calculate Trendlines Using", "Visible Data|All data", 1 );


GfxSetZOrder( -5 );
GfxSetCoordsMode( 1 );

bi = BarIndex();
cid = GetChartID();

if( dataOnDisplay )
{
    SetBarsRequired( sbrAll, sbrAll );
    fvb = 0;
    lvb = BarCount - 1;
}
else
{
    fvb = FirstVisibleValue( bi );
    lvb = LastVisibleValue( bi );
}

rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;

pk = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
tr = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;

for( i = 0; i < 4; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, bi, i ) );
    VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
    VarSet( "ph" + i, ValueWhen( pk, H, i ) );
    VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {7/06/2017} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) \n{{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
//SetChartBkColor(ParamColor("Background Color",ColorRGB(100,100,100)));
SetBarFillColor( IIf( C > O, ParamColor( "Candle UP Color", colorGreen ), IIf( C <= O, ParamColor( "Candle Down Color", colorRed ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ParamColor( "Wick UP Color", colorDarkGreen ), IIf( C <= O, ParamColor( "Wick Down Color", colorDarkRed ), colorLightGrey ) ), 64, 0, 0, 0, 0 );
Title +=
    " | " + Now( 1 ) +
    " | " + "RightStrength: " + rightStrength + " Bars ";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PlotShapes( shapeSmallcircle * tr, ColorRGB( 0, 0, 255 ), 0, L, -10 );
PlotShapes( shapeSmallcircle* pk, ColorRGB( 255, 0, 0 ), 0, H, 10 );

trendlineatshort = stopvalue = shortArray = nlinesCounter = nbackshiftCounter = 0;

for( i = lvb; i > fvb; i-- )
{
    if( tr[i] )
    {
        x2 = tx1[i];
        y2 = tl1[i];
        llow = 1e12;

        x1 = y1 = x1old = y1old = 0;

        for( j = i; j > fvb; j-- )
        {
            x1 = tx2[j];

            if( ( x2 - x1 ) > lookbackPeriod ) break;

            y1 = tl2[j];
            tline = LineArray( x1, y1, x2, y2, 0 );

            if( trendlinePriceCheck )
                pp = IIf( tline, L - ( tline - tline / 100 * Tolerance ), 1e12 );
            else
                pp = IIf( tline, C - ( tline - tline / 100 * Tolerance ), 1e12 );

            if( LastValue( Lowest( pp ) ) >= 0 && y1 < llow && y1 < y2 )
            {
                llow = y1;
                x1old = x1;
                y1old = y1;
            }
            else
            {
                x1 = x1old;
                y1 = y1old;
            }

            j = tx2[j] + 1;
        }

        x1 = x1old;
        y1 = y1old;

        if( x1 != 0 && nlinesCounter < nlines )
        {
            GfxSetZOrder( -1 );
            GfxSelectPen( ColorRGB( 0, 0, 255 ), 1, 1 );
            GfxMoveTo( x1, y1 );
            GfxLineTo( x2, y2 );

            tline = LineArray( x1, y1, x2, y2, 1 );
            idx2 = Ref( Cross( tline, C ) AND bi > x2, -1 );
            rr = ExRem( idx2, pk );
            idx1 = bi == x1;
            idx = Flip( idx1, idx2 );
            tline = IIf( idx, tline, Null );

            x2extend = LastValue( ValueWhen( idx, bi ) );
            y2extend = tline[x2extend];
            GfxSelectPen( ColorRGB( 0, 50, 255 ), 1, 0 );
            GfxLineTo( x2extend, y2extend );

            //Plot( tline, "", ColorRGB( 0, 0, 255 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
          
            nlinesCounter++;
        }

        i = tx2[i] + 1;;
    }
}


I have not looked at the above code but have a question. Are you trying to produce something like speed resistance lines ?

No it's a bit different to that, and different to Gann fan. Plot it and you'll see what it does. If you can help out that would be great thanks.