PlotText result is overlapped in amibroker

1.4k views Asked by At

I use below code

![priceatbuy=0; 
for( i = 0; i < BarCount; i++ ) 
{ 
if( priceatbuy == 0 && Buy\[ i \] ) 
priceatbuy = BuyPrice\[ i \]; 

PlotText("P" + priceatbuy, SelectedValue(BarIndex()), L\[SelectedValue(BarIndex())\] - ist\[SelectedValue(BarIndex())\], colorWhite);
if( priceatbuy > 0 && SellPrice\[ i \] < (priceatbuy - (0.027 * priceatbuy/100)) ) 
{ 
Sell\[ i \] = 1; 
SellPrice\[ i \] = (priceatbuy - (0.027 * priceatbuy/100)) ;
plotShapes(shapeDownArrow*sell, colorpink );
priceatbuy = 0; 
} 
else 
Sell\[ i \] = 0;
}][1] 

With above plotText, when i print the variable "priceatBuy", i see the text is overlapped and I am not able to see the value correctly. Is it because that SellPrice[i] and BuyPrice[i] returning some other value that Plottext doesnt understand? If I print Sell[i] or Buy[i], it prints correctly. May I know how to get this value correctly.

PFA image

1

There are 1 answers

0
fxshrat On

Your code is wrong.

Below is a correct code sample (The loop contains SL exit only! So you need to add some profit exit condition too otherwise you always exit at loss.)

percent = 2.0; // percent SL
delay = 1; // buy entry bar delay

Buycond = Cross( MACD(), Signal() ); // buy when macd crosses above of signal line
Buy = Ref( Buycond, -delay );    
BuyPrice = Open;

Sell = 0;
SLlong = Null;
SLLineLong = Null; // array
for( i = delay; i < BarCount; i++ ) {
    // setting long SL line
    if( IsNull( SLlong ) && Buy[ i ] )
        SLlong = BuyPrice[ i ] * ( 1 - percent / 100 );  
    else { 
        Buy[ i ] = 0;
    }
    // no shape buy signal if SL not hit
    if( NOT IsNull( SLlong ) && BuyCond[ i ] )
        Buycond[ i ] = 0; 
    // if SL is reached exit and reset
    Sellsignal = L[ i ] < SLlong;
    if( NOT IsNull( SLlong ) && Sellsignal ) {
        Sell[ i ] = 1;
        SellPrice[ i ] = Min( O[ i ], SLlong );       
        SLlong = Null;
    } else
        Sell[ i ] = 0;
    //
    // for plotting SL line array is required
    SLLineLong[i] = SLlong;
}

// Chart pane section
if( Status( "action" ) == actionIndicator ) {
    SetBarsRequired( 10000, 10000 );
    SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );
    // Plot price
    Plot( C, "", colorDefault, GetPriceStyle() );
    // Plot SL line
    Plot( SLLineLong, "\nSL long", colorRed, styleLine | styleNoRescale );
    //
    // Plot Shapes
    dist = -12;
    PlotShapes( shapeUpArrow * Buycond, colorGreen, 0, L, dist );
    PlotShapes( shapeSmallCircle * Buy, colorGreen, 0, BuyPrice, 0 );
    PlotShapes( shapeDownArrow * Sell, colorPink, 0, H, dist );
    PlotShapes( shapeSmallCircle * Sell, colorPink, 0, SellPrice, 0 );
    //
    // Plot signal text
    fnt = "Arial";
    fntsize = 8;
    txtdist = 25 - dist;
    bi = Barindex();
    fvb = FirstVisiblevalue( bi );
    lvb = LastVisiblevalue( bi );

    PlotTextSetFont( "", fnt, fntsize, 0, 0, -1 ); 

    for( i = fvb; i <= lvb; i++ ) { // iterate through visible chart area only
        if( Buy[i] )  // plot text at signal occurences only
            PlotText( StrFormat( "BP@\n%g", BuyPrice[ i ] ), i, L[ i ], colorGreen, colorDefault, -txtdist + fntsize );
        if( Sell[i] )
            PlotText( StrFormat( "SP@\n%g", SellPrice[ i ] ), i, H[ i ], colorPink, colorDefault, txtdist );
    }
    //
    _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 ) );    
}