Buy ans sell signal in same bar - Exploration

Please support for below issue.

while doing the exploration, if buy and sell signal is coming in the samebar, but it is showing only buy signal in exploration.

Please follow this advice: How to ask a good question
If you re-read your own post again you will see that in current shape it makes no sense.

1 Like

Dear Tomasz,

Sorry for inconvience. I am facing issue in my AFL code. While doing the exploration, If the buy and sell signal is coming on the same bar, i am getting only buy in the exploration Result. Kindly help me to reslove this issue. My code is shown in below.

type or paste code here`NewDay = (Day()!= Ref(Day(), -1));
EndDay = (Day()!= Ref(Day(), 1));  

N = Param("No.of Trades",1,1,5,1);
Exit_Time = ParamTime("Trade Exit","15:15:00");

TargetPct = Param("Target %", 0.3, 0, 20, 0.5);
MaxTarget = 100;  

Yesterday = Ref(NewDay, 1);
yesterdayvolume = IIf( Yesterday,hhv(TimeFrameGetPrice("V",in15Minute),25),Null);

Firstcandle =  NEWDAY AND TimeNum() == 091500;
FirstcandleH = ValueWhen( NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("H",in15Minute));
FirstcandleL = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("L",in15Minute));
FirstcandleC = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("C",in15Minute));
FirstcandleO = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("O",in15Minute));
FirstcandleVolume = ValueWhen( NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("V",in15Minute));

ORBHSL = Max(FirstcandleC,Firstcandleo); 
ORBLSL = Min(FirstcandleC,Firstcandleo);
ORBHTarget = FirstcandleH * (1+(TargetPct/100)); 
ORBLTarget = FirstcandleL * (1-(TargetPct/100)); 


Buy = Cross(H, FirstcandleH);
Short = Cross(FirstcandleL, L);

Sell = (L <= ORBHSL) OR (H >= ORBHTarget) OR (TimeNum() >= Exit_Time);
Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() >= Exit_Time);

Buy = Buy AND Sum( Buy, BarsSince( newDay ) + 1 ) <= N;
Short = Short AND Sum( Short, BarsSince( newDay ) + 1 ) <= N;

orbBothSides = IIf(Buy AND Short, 1, 0);  
Buy = IIf(orbBothSides AND C <= O, 0, Buy);  
Short = IIf(orbBothSides AND C > O, 0, Short);  
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);  
Sell = IIf((BarsSince(Buy) < (BarsSince(Ref(Sell,-1))+1)) OR (BarsSince(Buy) AND IsNull(BarsSince(Ref(Sell,-1)))),Sell,0); 
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);  
Cover = IIf((BarsSince(Short) < (BarsSince(Ref(Cover,-1))+1)) OR (BarsSince(Short) AND IsNull(BarsSince(Ref(Cover,-1)))),Cover,0); 

//--Set prices 
BuyPrice = IIf(Buy, FirstcandleH, Null);  
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);  
ShortPrice = IIf(Short, FirstcandleL, Null);  
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);  
`

and also please find the issue image details.
Exploration Result

Hi,

Your code is incomplete. Where is the Exploration code "AddColumn()" that prints the Signal column ?
There is 1 row for each bar, so if your code isn't specific maybe the first signal is printed.
Try plotting Buy and Sell variables in separate columns.

Thank you for your support. As per your feedback, i have tried the with seperate column. and also please share your feedback for below queries.

  1. Is it possible to get the entry and exit signal in two rows if the singal in same bar.
    currently i am getting the value in single row only.
  2. I cant able to get the yesterday highest volume in 15mins Bar.01
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Price", colorDefault, styleCandle );
RequestTimedRefresh( 1 );

// Day Checking
NEWDAY = Day() != Ref(Day(),-1);
EndDay = (Day()!= Ref(Day(), 1));  
FirstBarTime = ValueWhen(NewDay,TimeNum(),1);  
EndTime = ValueWhen(EndDay,TimeNum(),1); 
SquareOffTime = EndTime; 

//Param Toggle List
N = Param("No.of Trades",1,1,5,1);
Exit_Time = ParamTime("Trade Exit","15:15:00");
TargetPct = Param("Target %", 0.4, 0, 20, 0.5);
ORBV = ParamToggle("ORB","Show|Hide",1);
ProfitLoss = ParamToggle("P&L","Show|Hide",0);
MaxTarget = 100;  


Yesterday = Ref(NewDay, 1);
yesterdayvolume = IIf(Yesterday,HHV(TimeFrameGetPrice("V",in15Minute),26),Null);

//ORB Values
Firstcandle =  NEWDAY AND TimeNum() == 091500;
FirstcandleH = ValueWhen( NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("H",in15Minute));
FirstcandleL = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("L",in15Minute));
FirstcandleC = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("C",in15Minute));
FirstcandleO = ValueWhen(NEWDAY AND TimeNum() == 091500,TimeFrameGetPrice("O",in15Minute));

ORBHSL = Max(FirstcandleC,Firstcandleo); 
ORBLSL = Min(FirstcandleC,Firstcandleo);
ORBHTarget = FirstcandleH * (1+(TargetPct/100)); 
ORBLTarget = FirstcandleL * (1-(TargetPct/100)); 
	
Buy = Cross(H, FirstcandleH);
Short = Cross(FirstcandleL, L);
Sell = (L <= ORBHSL) OR (H >= ORBHTarget) OR (TimeNum() >= Exit_Time);
Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() >= Exit_Time);

Buy = Buy AND Sum( Buy, BarsSince( newDay ) + 1 ) <= N;
Short = Short AND Sum( Short, BarsSince( newDay ) + 1 ) <= N;

orbBothSides = IIf(Buy AND Short, 1, 0);  
Buy = IIf(orbBothSides AND C <= O, 0, Buy);  
Short = IIf(orbBothSides AND C > O, 0, Short);  
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);  
Sell = IIf((BarsSince(Buy) < (BarsSince(Ref(Sell,-1))+1)) OR (BarsSince(Buy) AND IsNull(BarsSince(Ref(Sell,-1)))),Sell,0); 
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);  
Cover = IIf((BarsSince(Short) < (BarsSince(Ref(Cover,-1))+1)) OR (BarsSince(Short) AND IsNull(BarsSince(Ref(Cover,-1)))),Cover,0); 

//--Set prices 
BuyPrice = IIf(Buy, FirstcandleH, Null);  
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);  
ShortPrice = IIf(Short, FirstcandleL, Null);  
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);  

buycont = Flip(Buy,Sell);
shortcont = Flip(Short,Cover);

// Plot the ORB Values
ORBH = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null ,FirstcandleH);
ORBL = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null, FirstcandleL);
ORBHTarget1 = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null ,ORBHTarget);
ORBLTarget1 = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null, ORBLTarget);
ORBHSL1 = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null ,ORBHSL);
ORBLSL1 = IIf(TimeNum() < ValueWhen(newday,TimeNum()) +5, Null, ORBLSL);


if(ORBV)
	{
		Plot(ORBH,"ORB High",colorOrange, styleDots);
		Plot(ORBL,"ORB Low",colorOrange,styleDots);
		//Plot(SecondHigh,"Second High",colorOrange, styleDashed);
		//Plot(SecondLow,"Second Low",colorOrange,styleDashed);	
	}



if(ProfitLoss)
	{
	

		Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget1),"",colorGreen,styleDashed); 
		Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget1),"",colorGreen,styleDashed); 
		//PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud); 
		Plot(ORBHSL1,"",colorBlack,styleDashed);  
		Plot(ORBLSL1,"",colorBlack,styleDashed); 
	}


Signalcolor = IIf(Buy,colorGreen,IIf(Short,colorRed,IIf(Cover,colorBlue,IIf(Sell,colorBlue,Null))));


Price = IIf(Buy,BuyPrice,IIf(Short,ShortPrice,IIf(Cover,CoverPrice,IIf(Sell,SellPrice,Null))));

Filter = Buy OR Short OR Sell OR Cover;
//Filter =1;
AddColumn(IIf(Buy,'B',IIf(Short,'S',Null)),"Signal Entry",formatChar,colorWhite,Signalcolor);
AddColumn(IIf(Sell,'T',IIf(Cover,'C',Null)),"Signal Exit",formatChar,colorWhite,Signalcolor);
AddColumn(Price," Price",1.2);
AddColumn(FirstcandleH,"ORB High",1.2);
AddColumn(FirstcandleL,"ORB Low",1.2);
AddColumn(yesterdayVolume,"Yesterday Volume",1);

 
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorBlue, 0, H, Offset = -45 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );

Your code needs serious cleanup as currently it is spaghetti. To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

As explained in manual http://www.amibroker.com/guide/h_exploration.html
exploration produces A ROW (one row) for each bar where Filter variable is True. Exploration does NOT use Buy/Sell variables. It only cares about Filter variable.

If you want TWO separate rows for ONE bar, one for buy signal and one for sell signal occurring on SAME bar, you need to use SCAN, not exploration.

As @nsm51 already wrote you - if you want to use Exploration, you should have SEPARATE column for Buy and SEPARATE column for Sell if you want to see them both in one bar.

Thank you @Tomasz ,
As @nsm51 written, I have tried the two different colum. It is working fine.

while doing the the scan, It is showing only close price. so that only i have tried with Exploration for the breakout point price values. Is any other method is there for showing the breakout price details with buy and sell signal in same bar.