Help on PlotText 'for' loop entries

#1
Hi all, I need help on fetching right data in the 'for' loop (code is done, pls help correct if wrong somewhere).

Requirement is ...
If Close > EMA(C,20)
then Low of the candle - EMA(C,20)
Else
then EMA(C,20) - High of the candle

And the values need to get plotted over the candle (above/below).

I plotted but unable to retrieve right value. Additionally last recent 3 candles alone need to get plotted.

Can somebody help for the below code.

HTML:
Plot(EMA(C,20), "20 (e)MA", colorWhite, styleThick ); 

Buy = C > EMA(C,20); 
Sell = EMA(C,20) > C; 

for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] )  PlotText("Up \n@"   + NumToStr((L[i] - EMA(C[i],20)),1.2),  i, L[ i ],colorYellow ); 
if( Sell[i] ) PlotText("Down \n@" + NumToStr((EMA(C[i],20) - H[i]),1.2), i, H[ i ], colorWhite ); 
}
 
Last edited:

casoni

Well-Known Member
#2
Hello leslieprabakar

check this

Buy = Cross(C , EMA(C,20));
Sell = Cross(EMA(C,20) , C);
//PlotShapes(shapeSmallCircle*Buy,colorGreen,0,L,-10);
//PlotShapes(shapeSmallCircle*Sell,colorRed,0,H,10);
Plot(C,"",IIf(Buy,5,IIf(Sell,4,31)),128);
Plot(EMA(C,20), "20(e)MA", colorWhite, styleLine );

bv=IIf(Buy,(L-EMA(C,20)),Null);
sv=IIf(sell,(EMA(C,20)-H),Null);
bv=Prec(bv,2.2);
sv=Prec(sv,2.2);
dist = 1.5*ATR(10);

for( i = 0; i < BarCount; i++ )
{
if( Buy ) PlotText( "up" , i, L-dist, colorGreen);
if( Sell ) PlotText("Dn", i, H+dist, colorRed );
if( Bv ) PlotText("\n" + bv[ i ],i, L-dist, colorGreen);
if( Sv ) PlotText("\n" + sv[ i ],i, H+dist, colorRed );
}
 

trash

Well-Known Member
#3
Hi all, I need help on fetching right data in the 'for' loop (code is done, pls help correct if wrong somewhere).

Requirement is ...
If Close > EMA(C,20)
then Low of the candle - EMA(C,20)
Else
then EMA(C,20) - High of the candle

And the values need to get plotted over the candle (above/below).

I plotted but unable to retrieve right value. Additionally last recent 3 candles alone need to get plotted.

Can somebody help for the below code.

HTML:
Plot(EMA(C,20), "20 (e)MA", colorWhite, styleThick ); 

Buy = C > EMA(C,20); 
Sell = EMA(C,20) > C; 

for ( i = 0; i < BarCount; i++ )
{ 
if( Buy[i] )  PlotText("Up \n@"   + NumToStr((L[i] - EMA(C[i],20)),1.2),  i, L[ i ],colorYellow ); 
if( Sell[i] ) PlotText("Down \n@" + NumToStr((EMA(C[i],20) - H[i]),1.2), i, H[ i ], colorWhite ); 
}

Don't use Array functions within Barcount loop!
EMA function is an array function. In addition avoid multiple calls of same function having same parameter inputs.
Store functions to variables!

Code:
ema1 = EMA( C, 20 );

Plot( C, "Price", colorDefault, styleCandle );  
Plot( ema1, "20 (e)MA", colorWhite, styleThick );

Buy = C > ema1;
Sell = ema1 > C;

// remove excessive signals
//Buy = ExRem( Buy, Sell );
//Sell = ExRem( Sell, Buy );

offset = 20;
bi = BarIndex();
for( i = FirstVisibleValue(bi); i <= LastVisibleValue(bi); i++ ) 
{
    if ( Buy[i] ) {
        PlotText( StrFormat("Up \n@%1.2f", L[i] - ema1[i] ),  
                  x = i, 
                  y = L[ i ], 
                  colorYellow, 
                  colorDefault,
                  -offset );
    }
    
    if ( Sell[i] ) {
        PlotText( StrFormat("Down \n@%1.2f", ema1[i] - H[i] ), 
                  x = i, 
                  y = H[ i ], 
                  colorWhite, 
                  colorDefault, 
                  offset );
    }              
}
 
Last edited:

trash

Well-Known Member
#4
BTW, it is even better practice to move code belonging to different execution areas within AB to their own section using Status( "action" ) or Status( "actionex" ). See help file.

That way it ensures that i.e. indicator code does not influence analysis code speed wise, instead then unnecessary code gets deactivated. Also it makes entire code much better readable having an overall structure.

Code:
ema1 = EMA( C, 20 );

Buy = C > ema1;
Sell = ema1 > C;

// only operates in indicator mode 
// otherwise gets deactivated i.e. if working in analysis
if( Status( "action" ) == actionIndicator )
{
	// remove excessive signals
	//Buy = ExRem( Buy, Sell );
	//Sell = ExRem( Sell, Buy );

	Plot( C, "Price", colorDefault, styleCandle );  
	Plot( ema1, "20 (e)MA", colorWhite, styleThick );

	offset = 20;
	bi = BarIndex();
	for( i = FirstVisibleValue(bi); i <= LastVisibleValue(bi); i++ ) 
	{
		if ( Buy[i] ) {
			PlotText( StrFormat("Up \n@%1.2f", L[i] - ema1[i] ),  
					  x = i, 
					  y = L[ i ], 
					  colorYellow, 
					  colorDefault,
					  -offset );
		}
		
		if ( Sell[i] ) {
			PlotText( StrFormat("Down \n@%1.2f", ema1[i] - H[i] ), 
					  x = i, 
					  y = H[ i ], 
					  colorWhite, 
					  colorDefault, 
					  offset );
		}              
	}
}
 

Similar threads