How to get the actual Buy/Sell/Short/Cover price instead of the OLHC of the candle?

////////////////////////////////////////////////chart & back ground color//////////////////////////////////////////////////
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
Plot( Close, "C", colorWhite, styleCandle); 
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorDarkBlue, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );

Buyst = EMA(Close,5) > EMA(Close,13);
Shortst = EMA(Close,5) < EMA(Close,13);

Buy =     Buyst  ;
Short =    Shortst  ;
Sell =Shortst ;   
Cover = Buyst ;


Signalshape=Buy*shapeUpArrow + Short*shapeDownArrow;
PlotShapes( Signalshape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );

pos = 2*ATR(15);
for( i = 0; i < BarCount; i++ ) {
       if( Buy[i] ) PlotText( "" + Close[i], i, Low[i] - pos[i], colorGreen );
       if( Short[i] ) PlotText( "" + Close[i], i, Low[i] + pos[i], colorRed );
Signalshape=Cover*shapeHollowUpArrow + Sell*shapeHollowDownArrow;
PlotShapes( Signalshape, IIf( Cover, colorGreen, colorRed ),0, IIf( Cover, Low, High ) );
pos = 2*ATR(15);
for( i = 0; i < BarCount; i++ ) {
      if( Cover[i] ) PlotText( "" + Close[i], i, Low[i] - pos[i], colorGreen );
      if( Sell[i] ) PlotText( "" + Close[i], i, Low[i] + pos[i], colorRed );


BarsSincebuy = BarsSince( BUY ); 
BarsSinceshort = BarsSince( SHORT ); 

LastSignal = IIf( BarsSincebuy < BarsSinceshort, 1, -1 ); 

	if ( SelectedValue( LastSignal ) == 1 ) 
        GfxSelectSolidBrush( colordarkGreen ); 
    if ( SelectedValue( LastSignal ) == -1 ) 
        GfxSelectSolidBrush( colorRed ); 

GfxSelectFont( "Tahoma", 8.5, 100 ); 
GfxSetBkMode( 1 ); 
GfxSetTextColor ( colorWhite ); 
y = Status( "pxchartheight" ) ; 
GfxSelectPen ( colorLightBlue, 1); // border color 
GfxRoundRect ( 5,  y- 35, 500, y , 7, 7 ) ; 

GfxTextOut ( ("" + WriteIf (Long  , "P/L for the LONG initiated at  "+ ValueWhen( Buy, Day(), 1 ) 
+ "/" + ValueWhen( Buy, Month(), 1 ) + "/" + ValueWhen( Buy, Year(), 1 ) + " " + ValueWhen( Buy, Hour(), 1 ) + ":" 
+ ValueWhen( Buy, Minute(), 1 )+ ":" + ValueWhen( Buy, Second(), 1 )+ " @ " +Prec( (BuyPrice), 2)+" =  "+Prec( (C-BuyPrice), 2),"")), 13, y-15); 

GfxTextOut ( ("" + WriteIf (shrt , "P/L for the SHORT initiated at "+ ValueWhen( Short, Day(), 1 ) 
+ "/" + ValueWhen( Short, Month(), 1 ) + "/" + ValueWhen( Short, Year(), 1 ) + " " + ValueWhen( Short, Hour(), 1 ) + ":" 
+ ValueWhen( Short, Minute(), 1 )+ ":" + ValueWhen( Short, Second(), 1 )+ " @ " +Prec( (ShortPrice) , 2)+" =  "+Prec( (ShortPrice-C), 2),"")), 13, y-15); 

The above code gives the output as below:


So, here the BUY/SELL/SHORT/COVER PRICE captured is the close of the candle and I can amend it as the OLHC of the happening candle. But , I want it to be the exact BUY/SELL/SHORT/COVER PRICE when they happened rather then the OLHC of the happening candle which is incorrect.

Please guide / help to solve the issue.

How is the program going to know what the exact price is going to be ?

You can dictate what the buyprice should be by saying either OHLC or something like:

Buyprice = ref(h,-1) + ? ; for a break of yesterdays high plus some amount.etc.


I think the program knows it, because it prints the arrows at the exact levels. And during the formation of the candle in which the buy signal prints, we can see that it appears and disappears many times as and when the exact buy price comes and goes.
Hence , I think we can amend the code for buyprice etc to get the price at which the signal generates , not the OLHC of the happening candle.
I might be wrong also, but experts views and guidance would be highly appreciated.

@santy, you already almost have it. Your code has a "BuyPrice =" line in it. Just set it to what you want it to be.

Like @Anthony showed you. YOU get to set the buy price to what you want it to be.


I am not getting any clue how to proceed.

@santy, I assume that you are just talking about in the AFL, and not talking about getting an execution price back from your broker. If you want your actual Broker Execution price, that is WAY beyond my current ability.

If you want the AFL processing value from the AFL code, you need to figure out what you think or want the price to be? From "your" code, your system sets a BUY SIGNAL when the 5 period EMA is above the 13 period EMA. So, What do you want the buy price to be?

Later in "your" code you set the Buy Price value. Since it is in the code, you control the calculation that sets it. So you can change it. You need to know what you want it to be, and figure out the appropriate calculation.

yes I am just talking about the afl only.

I want the buyprice as the exact price at which the 5 period EMA is above the 13 period EMA and not the OLHC of the happening candle.


if you see in the above code the 2nd argument only allows for the OLHC.

so, how to amend the above line of code to get the buyprice as the exact price at which the 5 period EMA is above the 13 period EMA and not the OLHC of the happening candle.

@santy, now you are getting in to the nitty gritty of programming. It seems to me (not meant to be offensive) that you do not understand the code.

So, Let's Review:

  1. Your Buy signal is when EMA 5 > EMA 13, so it will keep giving you the signal every bar
  2. Exrem gets rid of the "continuing" buy signals
  3. Buyprice is SET by YOU to be the CLOSE value when the Buy was Triggered.

So, if you want a different value, don't use the Close array.

If you review the Valuewhen documentation, you will see that the 2nd argument specifies an array.
Also see if the example sparks an idea for you (it did for me). I think making you work for this piece of code will help your AFL skills.


You BUY when you get a cross of EMA(Close), correct ? Guess what, you need close to calculate your EMA, so if you use any other price that is not Close or next open you are looking into the future

So, please guide how to code to take the buyprice as the open of the next candle.

@santy I think I finally understand what you are trying to do. For each bar, you want to find the MINIMUM closing value (minC) that would result in EMA(C,5) > EMA(C,13). If the current bar's High is greater than minC, then you want to generate a Buy signal and use minC as your BuyPrice. I think that if you look up the formula for calculating an EMA and do some algebra, you can probably come up with an equation for determining minC, which would allow you to perform intra-bar entries rather than entering at the close or at the next bar's open.

If you just want to buy at the next bar's open (as in your most recent message), then just do something like this:

Buy = Ref(Buyst, -1);
BuyPrice = Open;