Please help in buy sell exploration

In the following afl I want to see buy or sell signal in single column. If buy buy backgroundcolor=green if sell sellbackgroundclor=red.
Please help in resolving.

EMAA=EMA(Close, 5);
EMAB=EMA(Close, 8);
EMAC=EMA(Close, 13);

Buy= Cross(EMAA,EMAB) AND EMAB>EMAC;
Sell=Cross(EMAB,EMAA) AND EMAC>EMAB ;
B=WriteIf(Buy, "Buy","0");
S=WriteIf(Sell,"Sell","0");

Filter= Buy OR Sell;
AddColumn(EMAA,"EMAA",format=1.2);
AddColumn(EMAB,"EMAB",format=1.2);
AddColumn(EMAC,"EMAC",format=1.2);
AddColumn(Close,"Close Price", format=1.2);
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");



PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-15);
PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-15);
printf("\nBuy : " + Buy ); 
printf("\nSell : " + Sell ); 

Please point out my mistakes too. It is simple triple ema crossover system.

You may use AddMultiTextColumn function of AmiBroker 6.20 to achieve that.

Version( 6.20);// code  requires AB 6.20 minimum

EMAA=EMA(Close, 5);
EMAB=EMA(Close, 8);
EMAC=EMA(Close, 13);

Buy= Cross(EMAA,EMAB) AND EMAB>EMAC;
Sell=Cross(EMAB,EMAA) AND EMAC>EMAB ;
B=WriteIf(Buy, "Buy","0");
S=WriteIf(Sell,"Sell","0");

// if you want to remove excessive signals
// then uncomment below two lines
//Buy = ExRem( Buy, Sell );
//Sell = ExRem( Sell, Buy );

Filter= Buy OR Sell;
AddColumn(EMAA,"EMAA",format=1.2);
AddColumn(EMAB,"EMAB",format=1.2);
AddColumn(EMAC,"EMAC",format=1.2);
AddColumn(Close,"Close Price", format=1.2);
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");

/// edited Example from AmiBroker manual
/// @link https://www.amibroker.com/guide/afl/addmultitextcolumn.html
TextList = "No signal\nBuy\nSell\nBuy and Sell"; 
TextSelector = 1 * Buy + 2 * Sell; /* would give 0 if no signal, 1 if a buy, 2 if a sell, 3 if both buy and sell */ 
AddMultiTextColumn( TextSelector, TextList, "Which signal", 1, IIf( Buy, colorGreen, IIf( Sell, colorRed, colorBlue) ); 

22


Or for version lower than AB 6.20

EMAA=EMA(Close, 5);
EMAB=EMA(Close, 8);
EMAC=EMA(Close, 13);

Buy= Cross(EMAA,EMAB) AND EMAB>EMAC;
Sell=Cross(EMAB,EMAA) AND EMAC>EMAB ;
B=WriteIf(Buy, "Buy","0");
S=WriteIf(Sell,"Sell","0");

// if you want to remove excessive signals
// then uncomment below two lines
//Buy = ExRem( Buy, Sell );
//Sell = ExRem( Sell, Buy );

Filter= Buy OR Sell;
AddColumn(EMAA,"EMAA",format=1.2);
AddColumn(EMAB,"EMAB",format=1.2);
AddColumn(EMAC,"EMAC",format=1.2);
AddColumn(Close,"Close Price", format=1.2);
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");

AddColumn( IIf(Buy, 'B', IIf( Sell, 'S', 'N') ), "Which Signal", formatchar, IIf( Buy, colorGreen, IIf( Sell, colorRed, colorBlue)) ); 

6

3 Likes

@asdhaliwal, alternatively, try this snippet:

// Will handle also Buy/Sell in the same bar
// Shows 1 for Buy - 2 for Sell - 3 for Buy AND Sell
bkgColor = IIf( Buy, colorGreen, IIf( Sell, colorRed, IIf( Buy AND Sell, colorYellow, colorDefault ) ) );
AddColumn( Buy + ( Sell * 2 ), "Buy/Sell", 1, colorWhite, bkgColor );
2 Likes

Of course you can also output char if there is both Buy and Sell at same bar or if there is none of both at same bar.

Here I have made function (and for demonstration have used different Buy Sell signal than yours).

Used Ascii Characters and their meaning:

'B' -> Buy signal only
'S' -> Sell Signal only
'&' -> Both Buy and Sell signal at same bar
'N' -> No Buy and no Sell signal

(Why function name BuySellNum? Because e.g ASCII char 'B' is equal to 66, colorGreen is equal to 27 etc.)

EMAA=EMA(Close, 5);
EMAB=EMA(Close, 8);
EMAC=EMA(Close, 13);

i = BarIndex();

Buy= i%2 == 0;//Cross(EMAA,EMAB) AND EMAB>EMAC;
Sell= i%3 == 0 OR i%4 == 0;//Cross(EMAB,EMAA) AND EMAC>EMAB ;
B=WriteIf(Buy, "Buy","0");
S=WriteIf(Sell,"Sell","0");

// if you want to remove excessive signals
// then uncomment below two lines
//Buy = ExRem( Buy, Sell );
//Sell = ExRem( Sell, Buy );

Filter= 1;// Buy Or Sell;
AddColumn(EMAA,"EMAA",format=1.2);
AddColumn(EMAB,"EMAB",format=1.2);
AddColumn(EMAC,"EMAC",format=1.2);
AddColumn(Close,"Close Price", format=1.2);
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");

function BuySellNum( num1, num2, num3, num4 ) {
	/// by fxshrat@gmail.com
	/// @link https://forum.amibroker.com/t/please-help-in-buy-sell-exploration/8194/4
	global Buy, Sell;
	return Buy * (! Sell) * num1 + Sell * (! Buy) * num2 + Buy * Sell * num3 + (!Buy AND !Sell) * num4;
}

AddColumn( 
			BuySellNum( 'B', 'S', '&', 'N' ),	
			"Which Signal", 
			formatChar, 
			BuySellNum( colorGreen, colorRed, colorBlue, -1 )
		 ); 

204

6 Likes