PlotShapes of targets without crossings

To start with I use the following code to obtain swingpoints

_SECTION_BEGIN( "SwingPoints" );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
rightstrength = Param( "Right Strength",2, 2, 50, 1 );
leftstrength = Param( "Left Strength", 4, 2, 50, 1 );
plotFractals = ParamToggle( "Plot Fractals", "Off|On", 1 );

function GetTop()
{
    Top = H == HHV( H, leftstrength ) AND Ref( HHV( H, rightstrength ), rightstrength ) < H;
    Top = Top AND LastValue( bi ) - ValueWhen( Top, bi ) > rightstrength;
    return Top;
}

function GetValley()
{
    Valley = L == LLV( L, leftstrength ) AND Ref( LLV( L, rightstrength ), rightstrength ) > L;
    Valley = Valley AND LastValue( bi ) - ValueWhen( Valley, bi ) > rightstrength;
    return Valley;
}

pk = GetTop();
tr = GetValley();

pk = IIf( pk, IIf( ValueWhen( pk, bi, 2 ) < ValueWhen( tr, bi ), pk, IIf( ValueWhen( pk, H, 2 ) > H, False, pk ) ), pk );
pk = IIf( pk AND ValueWhen( pk, bi, 0 ) > bi, IIf( ValueWhen( pk, bi, 0 ) < ValueWhen( tr, bi, 0 ), IIf( ValueWhen( pk, H, 0 ) >= H, False, pk ), pk ), pk );
tr = IIf( tr, IIf( ValueWhen( tr, bi, 2 ) < ValueWhen( pk, bi ), tr, IIf( ValueWhen( tr, L, 2 ) < L, False, tr ) ), tr );
tr = IIf( tr AND ValueWhen( tr, bi, 0 ) > bi , IIf( ValueWhen( tr, bi, 0 ) < ValueWhen( pk, bi, 0 ), IIf( ValueWhen( tr, L, 0 ) <= L, False, tr ), tr ), tr );

for( i = 0; i < 3; 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 ) );
}

ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;

if( PlotFractals )
{
    PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );
    PlotShapes( shapeSmallCircle * tr, 44, O, L, -10 );
    PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10, 1 );
    PlotShapes( shapeSmallCircle * tr, 44, O, L, -10, 1 );
    PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10, 2 );
    PlotShapes( shapeSmallCircle * tr, 44, O, L, -10, 2 );
}
_SECTION_END();

So far this works as expected and gives this result

image

Now I would like to add another smallcircle for the target which would be half of the range
of those 3 swingbars in the direction of the swing. This smallcircle should be drawn just
at this calculated targetlevel for the following 4. candle, but I just can't achieve to get
this circle drawn correctly but obtain instead lots of small arrows if I try to add a similar
code like the one used for the swingpoints with those calculated targets instead 'pk'/'tr'.

Hi @Achalendra,

I think that what you're trying to do is:

  1. determine where the swing high/low points are
  2. calculate the midpoint between the last swing high and swing low

I haven't tested your code, but this might do what you want:

// Named constants
cnsBarsForMidLevel = 4 ;

// Get the swing points
isPeak = getTop() ;
isTrough = getValley() ;
barsPeak = BarsSince(isPeak) ;
barsTrough = BarsSince(isTrough) ;

// The extreme values of the swing points
valPeak = ValueWhen(isPeak, High, 1) ;
valTrough = ValueWhen(isTrough, Low, 1) ;

// Midpoint of the swings
midLevel = (valPeak + valTrough) / 2 ;
midLevel = IIf((barsPeak <= cnsBarsForMidLevel) OR (barsTrough <= cnsBarsForMidLevel), midLevel, Null) ;


// These are to show exactly where the tops and bottoms are - doesn't always coincide with the code above
Plot(isPeak, "isPeak", ParamColor("isPeak Color", colorCycle), ParamStyle("isPeak Style", styleHistogram + styleOwnScale, maskAll)) ;
Plot(isTrough, "isTrough", ParamColor("isTrough Color", colorCycle), ParamStyle("isTrough Style", styleHistogram + styleOwnScale, maskAll)) ;

// "Drag" the value of the peak/trough going forward
Plot(valPeak, "valPeak", ParamColor("valPeak Color", colorCycle), ParamStyle("valPeak Style", styleLine, maskAll)) ;
Plot(valTrough, "valTrough", ParamColor("valTrough Color", colorCycle), ParamStyle("valTrough Style", styleLine, maskAll)) ;

// The midpoint between the last peak and trough
Plot(midLevel, "midPeak", ParamColor("midPeak Color", colorCycle), ParamStyle("midPeak Style", styleDots + styleNoLine, maskAll)) ;

Which produces this:
image

However, on looking at your code for GetPeak() and GetValley(), they might be producing unrealistic results, for the same reasons that the Zig() function does - it looks into the future, and is unsuitable for backtesting.

But, then again, it might be Ok, depending on what you want to use it for. :slight_smile:

Hi sinecospi,

thank you very much for your answer.
It was my fault that I didn't specify exactly that I don't consider the midpoint of the range
as target but the extension of the range by it's half in the direction of the swing.
Then I didn't want so many confusing lines but only a single 4. smallcircle (or dot)
drawn exactly at the place of this calculated extension.
It was less the calculation of the target than the way to draw this 4. smallcircle at
the level of the calculated target which is my problem.

I forgot to mention that I don't think that this code looks in the future.
The present time is the moment when the 3 dots are drawn which is exactly
at the close of the 3. candle. At that moment the range is known and the
extension can be calculated for the following candle.

May be someone could again just translate the following easylanguage code to afl ?

Meta: 
SubChart(False);
Inputs:    
length(3),
leftstrength(4),
rightstrength(2),
width(4,1),
avgpourc(0.3);
Variables: 
opivpriceL,opivbarL,opivpriceH,opivbarH,RL,TL1,RH,TH1,dist;

condition1 = pivot(Low,length,leftstrength,rightstrength,1,-1,opivpriceL,opivbarL);
condition2 = pivot(High,length,leftstrength,rightstrength,1,1,opivpriceH,opivbarH);
if condition1 then begin
dist = avgtruerange(25)*avgpourc;
DrawSymbol[2](opivpriceL - dist,"L1",SymbolDot,Width,green,green);
DrawSymbol[1](opivpriceL - dist,"L2",SymbolDot,Width,green,green);
DrawSymbol(opivpriceL - dist,"L3",SymbolDot,Width,green,green);
RL = Maxitems(H,H[1],H[2])-opivpriceL;
TL1 = opivpriceL + (RL*1.5);
DrawSymbol[-1](TL1,"TL1",SymbolDot,Width,colorlightblue,colorlightblue);
end;
if condition2 then begin
dist = avgtruerange(25)*avgpourc;
DrawSymbol[2](opivpriceH + dist,"H1",SymbolDot,Width,magenta,magenta);
DrawSymbol[1](opivpriceH + dist,"H2",SymbolDot,Width,magenta,magenta);
DrawSymbol(opivpriceH + dist,"H3",SymbolDot,Width,magenta,magenta);
RH = opivpriceH-Minitems(L,L[1],L[2]);
TH1 = opivpriceH - (RH*1.5);
DrawSymbol[-1](TH1,"TH1",SymbolDot,Width,colororange,colororange);
end;

which gives this result

image

one sees the 4. point appears in orange for the short targets below the 4. candle
and in light blue above the 4.candle for the long targets.