Scan or Exploration - How exploration scan bar by bar for all symbols?

I was trying to explore for all symbols data

But amibroker scan for signal for all bars for one symbol and then scans another.

I have requirement that - amibroker should scan for all symbol for same barcount at time and then scan for next bar for all symbols.

check my code it not getting as expected.

_SECTION_BEGIN("trail1");

for(i=0;i<BarCount;i++)
{
_TRACE("Buy " + Name() + "  barcount "  + i + "  BarIndex" + BarIndex());
}
_SECTION_END();

check result image.
Capture
Capture

The default result list is by design as AB analysis window AFL run is multithreaded.

You can add a Column with BarIndex() if they are ali aligned properly as it appears and Sort on that column.
https://www.amibroker.com/guide/afl/setsortcolumns.html

If BarIndex doesnt work, Use DateTime and sort on this column.
AddColumn( DateTime(), "dt", formatDateTime );

You can even have multiple SetSortColumns criteria defined along with Ascending or Descending option.

1 Like

I wanted to calculate cumulative average price of stock prices per barcount. it will not add all stock price per per bar.

Also tried new code but see what time it showing to complete. It show result as wanted but time to complete is huge.

Please suggest alternative.

_SECTION_BEGIN("trail1");

Buy=1;

for(i=0;i<BarCount;i++)
{

list = CategoryGetSymbols( categoryAll,1); 
 

   for( j= 1; ( sym = StrExtract( list, j ) ) != ""; j++ ) 
   { 
   
 

		_TRACE("Buy " + sym + "  barcount "  + i + "  BarIndex" + BarIndex());
		}



}



_SECTION_END();

see result and time taken complete!Capture

This is what happens when you provide partial information, you said you wanted a different sort and that's what i posted.

Also, you are saying you have posted code but that will only be useful when the approach is relevant.
Running a BarCount loop and Nesting all Symbols in it and then running it for ALL Symbols + ALL Quotes is a recipe for disaster. (As Tomasz says it).

Again, the AFL code runs in a multi-threaded way for each symbol.

If you want to access other symbols from current symbol, you need to use Foreign() functions and Name() will return the current base symbol for that thread.

In the 2nd image, it shows 2 threads, so have you limited this or is it a low-end CPU ?

Post exactly what you want to achieve, not the code part, then it will be of use to try and post a working code otherwise, you can discard the outer barcount loop, just keep the inner loop and access other symbols with a suitable Foreign() Function and get the Close array then add it cumulative for ONLY the current symbol and change the settings to only one symbol in Filter Menu.

1 Like

My backtest of scale out is not working properly as i am finding way to exit shares or by scanning per bar open positions.

But exploration or scan doesnot scan all stocks per bar.

Suggest any solution.

Also CBT scale out being complicated.

need some way out ot it.

@Tomasz please have a look.

_SECTION_BEGIN("EScale OUt");


SetChartOptions(0,chartShowArrows|chartShowDates);


Equityper=Param("Equity % per trade",5,1,100,1);

maxopenpos = 5;
SetOption("InitialEquity", 1500000);
SetPositionSize (Equityper,spsPercentOfEquity);
RoundLotSize=1;
SetOption("MaxOpenPositions",maxopenpos);

	 
// SET COMMISSIONS//  
SetOption("CommissionMode", 1); // Percent Mode  
SetOption("CommissionAmount",0.30); // 0.30% of Trade Value


Buycond= Close > Ref(Close, -1) AND Ref(Close, -1) > Ref(Close, -2) AND Ref(Close, -1) > Ref(Close, -3) AND IIf(Ref(Close, -3) < Ref(Close, -4), 1, IIf(Ref(Close, -4) < Ref(Close, -5),Ref(Close, -1) < Ref(Close, -4) OR( Ref(Close, -2) < Ref(Close, -4) AND Ref(Close, -3) >= Ref(Close, -5) ),IIf(Ref(Close, -5) < Ref(Close, -6), 1,Ref(Close, -6) < Ref(Close, -7))));
Buy = 0;



Buy=Buycond;
Sell=0;

Short=0;
Cover=0;

Target1 = 4/100;
Target2 = 5/100;
Trailingstop = 3/100;


priceatbuy = 0;
priceatshort=0;
exit = 0;
highsincebuy=Null;
lowsinceshort=Null;

Target1price = 0;
Target2price=0;
Trailstopprice=0;

bexit =0;
sexit =0;
openpos=0;
dt=DateTime();
dtformat = "\n%d-%m-%Y\n";





for(i=1;i<BarCount;i++)

{
	//_TRACE("Open Postion " + openpos);
	if(Buycond[i] AND priceatbuy==0 AND priceatshort==0 )
	
	{
		Buy[i]=True;
		priceatbuy = close[i];
		Target1price = priceatbuy*(1+Target1);
		Target2price = priceatbuy*(1+Target2);
		Trailstopprice = priceatbuy*(1-Trailingstop);
		if(Buy[i]==True)
		{

		
		_TRACE(DateTimeToStr(dt[i]) + "," + Name()+ " #BUY Entry" + close[i] + " Target1 @ " + Target1price + " Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice   + " high " + High[i] + " low " + Low[i] ) ;
				

		
		}
		

	} 

	if(priceatbuy>0)
	
	{
	
		highsincebuy = Max(High[i],highsincebuy); 
		Trailstopprice = highsincebuy*(1-Trailingstop); //_TRACE("highsincebuy " + highsincebuy + " Trailstp");
		
		if(High[i]>=Target1price AND bexit==0)
		{
			
			Buy[i]=sigScaleOut;
			bexit=1;
			Target2price = priceatbuy*(1+Target2);
			
			_TRACE(DateTimeToStr(dt[i]) +  " " + Name() + " #BUY 50% Scaleout @ " + close[i]+ " Target1 @ " + Target1price + " Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice  +  " high " + High[i] + " low " + Low[i]);
				

		}
		
		
		if(High[i]>=Target2price AND bexit==1 )
		{
			
			Sell[i]=True;
			SellPrice[i]=Close[i];
			bexit=2;
			Target2price = priceatbuy*(1+Target2);
			
			_TRACE(DateTimeToStr(dt[i]) + " " + Name() + " #BUY 100% Exit @" + Close[i]  + "Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice + " high " + High[i] + " low " + Low[i] );
				

		
		}
		
		if(Low[i] <= Trailstopprice)
		
		{
			if(bexit==1)
			{
			Sell[i]=True;
			SellPrice[i]=Trailstopprice;
			bexit=3;
			
			_TRACE(DateTimeToStr(dt[i]) + " " + Name() + " #BUY 50% Trail Exit-  Stoploss @ " 
				+ Trailstopprice + " high " + High[i] + " low " + Low[i] );
				

			}
			
			
			if(bexit<=1)
			{
			Short[i]=1;
			bexit=4;
			priceatshort= ShortPrice[i]=Trailstopprice;
			Target1price = priceatshort*(1-Target1);
			Target2price = priceatshort*(1-Target2);
			Trailstopprice = priceatshort*(1+Trailingstop);
			_TRACE(DateTimeToStr(dt[i]) +  " " + Name() + " #Reverse Short Entry @ " + priceatshort+ " Target1 @ " + Target1price + " Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice  +  " high " + High[i] + " low " + Low[i]);
				

			
			}
			
			if(bexit>=3)
			{
				Buy[i]=0;
				priceatbuy=0;
				bexit=0;
				highsincebuy=0;
				_TRACE(DateTimeToStr(dt[i]) + " " + Name() + " All buyside values reset");
			
	
			}
		
			
		}
		
	
	}
	
	
	if(priceatshort>0)
	
	{			
		
		lowsinceshort=Max(Low[i],lowsinceshort);
		Trailstopprice = lowsinceshort*(1+Trailingstop);
				

	
		if(Low[i]<=Target1price AND sexit==0)
		
		{
			sexit=1;
			Short[i]=sigScaleOut;
			
			_TRACE(DateTimeToStr(dt[i]) +  " " + Name() + " #Short 50% Scaleout @ " + close[i]+ " Target1 @ " + Target1price + " Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice  +  " high " + High[i] + " low " + Low[i]);
			


		
		}
		
		if(Low[i]<=Target2price AND sexit==1)
		
		{
			sexit=2;
			Cover[i]=True;
			CoverPrice[i]=Target2Price;
			
			_TRACE(DateTimeToStr(dt[i]) +  " " + Name() + " #Short 100% Exit @ " + Target2Price+ " Target2 @ " + Target2price + " Stoploss @ " 
				+ Trailstopprice  +  " high " + High[i] + " low " + Low[i]);
			

		}
		
		
		if(High[i]>=Trailstopprice AND sexit==1)
		{

				
				sexit=3;
				Cover[i]=True;
				CoverPrice[i]=Trailstopprice;
				
				_TRACE(DateTimeToStr(dt[i]) +  " " + Name() + " #Short 50% Trail Exit @ "  + " Stoploss @ " + Trailstopprice  +  " high " + High[i] + " low " + Low[i]);
				

		
		}
		
				if(High[i]>=Trailstopprice AND sexit<1)
		{

				
				sexit=4;
				Cover[i]=True;
				CoverPrice[i]=Trailstopprice;
				

		}
	
		if(sexit>=3)
			{Short[i]=0;
			Cover[i]=0;
				priceatshort=0;
				sexit=0;
				lowsinceshort=0;
				_TRACE(DateTimeToStr(dt[i]) + " " + Name() + " All shortside values reset");
			
	
			}
	}



}





SetPositionSize( Equityper, spsPercentOfEquity ); 
SetPositionSize( Equityper, spsPercentOfPosition * ( Buy == sigScaleOut ) );
SetPositionSize( Equityper, spsPercentOfPosition * ( Short == sigScaleOut ) ); 


_SECTION_END();

@travick have a look and please reply if have solution for give full code.