Kindly help completing the code

Dear Friends,

I am very new on Amibroker and AFL. No software background too... I have searched on the internet and managed to write the below codes. This code is giving me the BUY/SELL & SHORT/COVER signals, but the execution is happening only at "CLOSE" price, not on the value where the signal gets generated. Also, I wish to display that signal value on the screen, but I don't know how to write code for the same. It is either giving me "C" or "O". How to get the exact value where the signal is generated.

Kindly help..

Regards,
Mahesh


_SECTION_BEGIN("HA");
SetChartOptions(0,chartShowArrows | chartShowDates);

HaClose = (O + H + L + C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
	
barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);

Plot( C, "Price", colorDefault, styleCandle );

Buy = ExRem(Haopen < Haclose , Haopen > Haclose) ;
Sell = ExRem(Haopen > Haclose , Haopen < Haclose) ;

Cover = ExRem(Haopen < Haclose , Haopen > Haclose) ;
Short = ExRem(Haopen > Haclose , Haopen < Haclose) ;


if (Buy[BarCount-1]==true)
{
PopupWindow("Buy Signal Initiated","Buy Signal",4, 1350, 150, 150,100);
Say("Attention, Buy Signal Initiated");
}

if (Sell[BarCount-1]==true)
{
PopupWindow("Sell Signal Initiated","Sell Signal",4, 1350, 150, 150,100);
Say("attention, Sell Signal Initiated");
}

//========================================================================
dist = 1.5*ATR(20); 

distbuy=10;
distsell=25;


for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] ) PlotText( "@ " + O[ i ], i, L[ i ]-dist[i], colorLime ); 
if( Sell[i] ) PlotText( "@ " + O[ i ], i, L[ i ]+dist[i], colorYellow ); 
} 
//========================================================================


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();

There are built-in variables (arrays) to control entry and exit prices. Look for the following in the documentation:

BuyPrice
SellPrice
ShortPrice
CoverPrice

Thank you very much for your reply. I have used Buyprice/Sellprice code. however, this also gave me a result of Open/Close price only. I want the value where the signal gets generated.

Let's assume that the candle (Day candle) started at "100". I have got a buy signal at "120". The day close is "150". As per the current code, it is giving me "150" as Buy value whereas the actual Buy value is "120".

Kindly help.

Regards,
Mahesh

No one on the forum will be able to identify your error if you don't post your code.

SECTION_BEGIN("HA");
SetChartOptions(0,chartShowArrows | chartShowDates);

HaClose = (O + H + L + C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
	
barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);

Plot( C, "Price", colorDefault, styleCandle );

Buy = ExRem(Haopen < Haclose , Haopen > Haclose) ;
Sell = ExRem(Haopen > Haclose , Haopen < Haclose) ;

Cover = ExRem(Haopen < Haclose , Haopen > Haclose) ;
Short = ExRem(Haopen > Haclose , Haopen < Haclose) ;


if (Buy[BarCount-1]==true)
{
PopupWindow("Buy Signal Initiated","Buy Signal",4, 1350, 150, 150,100);
Say("Attention, Buy Signal Initiated");
}

if (Sell[BarCount-1]==true)
{
PopupWindow("Sell Signal Initiated","Sell Signal",4, 1350, 150, 150,100);
Say("attention, Sell Signal Initiated");
}

//========================================================================
dist = 1.5*ATR(20); 

distbuy=10;
distsell=25;


for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] ) PlotText( "@ " + O[ i ], i, L[ i ]-dist[i], colorLime ); 
if( Sell[i] ) PlotText( "@ " + O[ i ], i, L[ i ]+dist[i], colorYellow ); 
} 
//========================================================================


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

_SECTION_END();

the above is the code..

I still don't see these variables being assigned in your AFL. What am I missing?

Thank you for your response and help.

If i use
buyprice=Close (it will give me close price of the candle)
buyprice=open (this will give me open price of the candle)

I want the price where the signal get generates.

Regards,
Mahesh

You should tell it what the price should be when the signal gets generated.

EXACTLY, BUT HOW TO DO IT?
I mean how to amend the code for buyprice etc to get the price at which the signal generates , not the OLHC of the happening candle.

It is not obvious to me what "the price where the signal gets generated" should be. Your Buy and Sell signals are just based on the bar where HaClose crosses above (Buy) or below (Sell) HaOpen. The signals are not based on the high or low of the bar exceeding some limit price that you have set. Since HaClose depends on the Closing Price of the bar, you really can't evaluate whether or not you have a signal until the bar closes, which means that without any trade delays, the only BuyPrice/SellPrice that you can use is also the Close.

Perhaps if you give us an example of a Buy signal and the price at which you expect to enter the trade, that will help clear up any confusion. Or maybe another member of the forum has a better understanding of your question than I do.