Result of adding unequal arrays

Hello,

I've searched for a long time and written many trials trying to solve my own problem. The closest I've found is a post about foreign synchronising the arrays but that doesn't solve what I'm trying to do.

Say I have multiple stocks of unequal bars (timeframes). Foreign synchronises them but whenever I perform any math on them the result is always the length of the shortest bars (timeline). I don't want this to happen I want the proceeding bars of the shorter timeframes considered as zero.

I have tried Nz but as far as I can see it only fixes up holes within a timeframe not add bars to make smaller ones equal to the chart price timeline.

I am considering copying each one into a matrix a padding earlier bars but hopefully there is an easier way?

Here is the core of my code cna assume declarations etc are done.

Any help appreciated.

Kinds regards

for( i = 0; ( symbol = StrExtract( tickerlist, i ) ) != ""; i++ )
{	fc = Foreign(symbol,"C",1);
	//fc = Nz( fc ); // Null to zero function
    	
    	if(NullCount(fc) < chosen_date_bar)
			{	chosen_start_bar = chosen_date_bar;}
		if(NullCount(fc) >= chosen_date_bar)
			{	chosen_start_bar = NullCount(fc);}
    	
    	relP = 100 * ( fc - fc[chosen_start_bar] ) / fc[chosen_start_bar];
    	
    	relPsum += relP;
     	   	    
     	plot_colour = 	colorLightOrange + ( ( 2 * i ) % 15 );
	plot_width	=	1;
	
		
	PlotTextSetFont( symbol,label_font , label_fontsize, LastValue( BarIndex() ) + 1, LastValue( relP ), GetChartBkColor(), plot_colour, -label_fontsize/2 );
	Plot( IIf( bar_out_of_range, relP, Null), symbol, plot_colour, styleLine, Null, Null, 0, 1, plot_width );
}
  
	relPav = relPsum / (i-1);
   
	PlotTextSetFont( "relPav",label_font , label_fontsize, LastValue( BarIndex() ) + 1, LastValue( relPav ), GetChartBkColor(), plot_colour, -label_fontsize/2 );
	Plot( IIf( bar_out_of_range, relPav, Null), "relPav", colorwhite, styleLine, Null, Null, 0, 1, 10*Plot_width );

With your code the chart 's symbol has to be one having all data. So choose symbol of s&p 500 for example and save result to static variable.

Actually you should not use looping in chart but use analysis environment to create a composite with pad and align being enabled (and without any looping code). Composites can be stored via AddtoConposite or via StaticVarAdd.

As aside Plot function does not belong inside loop code

Thank you for your reply.

I use ASX 200 as the chart symbol but even if the chart symbol has more bars than one of the foreign symbols the result is always limited to the lowest length symbol array. I know this becasue if I omit the symbol causing the issue it's fine (the calculated result plots over the entire range).

Basically,
A1 - 0,1,2,3,4,5,6
A2 - 0, 1, 2, 3

Foreign A2:
A1 - 0,1,2,3,4,5,6
A2 - 0, 1, 2, 3

A3 = A1 * A2:
A3 - 0, 1, 2, 3

I understand it because basically before A2[0] there is nothing but I'd like expressions (or some expression) to treat the nothing preceding as zero (or whatever set to).

I'm sure I could do it manually by creating matrices and filling in the missing data.

Thanks also for the context re work spaces. I'm no professional programmer but I am an Elect Engineer with programming experience. My code is no doubt sloppy to professionals however one of the things that took me a long time to work out was context and workspaces. There is little mention of this in the guides for example quick AFL. It took me ages to work out why I was getting different results for the same thing at times.

"As aside Plot function does not belong inside loop code"

Is this an efficiency thing? Or a code clash? Or results in an error? Or just sloppy as my experience level.

Cheers

I solved it with:

for(j=0; j <= (BarCount-1); j++)
    	{	if(j < NullCount(fc))
				{newrelp[j] = 0;}
			else {newrelp[j] = relp[j];}
    	}
    	    	
    	if(NullCount(fc) < chosen_start_bar)
			{relp = newrelp;}

I know it's not elegant and sloppy but it works and doesn't hurt the 7950x at all.

First rule of writing loops: MOVE loop invariant(s) OUTSIDE of the loop. Everything that does NOT change as loop goes on should be OUTSIDE the loop.

NullCount(fc) in your case is loop invariant and should NOT BE called in each iteration.

nc = NullCount( fc );
for(j=0; j <= (BarCount-1); j++)
    	{	if(j < nc)
				{newrelp[j] = 0;}
			else {newrelp[j] = relp[j];}
    	}
    	    	

Your code can also be rewritten NOT to use loop at all

newrelp = IIF( BarIndex() < NullCount( fc ), 0, relp );

Generally however synchronization provided by Foreign is HIGHLY DESIRED. And no what Foreign gives you is NOT limited to "lowest length array". That is false.
Foreign synchronizes to CURRENTLY SELECTED symbol. If you have pad and align turned on as @fxshrat wrote you, it will synchronize to "Reference symbol".

Please read the manual about "Pad and align"

https://www.amibroker.com/guide/w_settings.html

Thanks very much Tomasz.

Stupidly (I realise now) I have always been focussed on less lines of code and you've made me realise that I'm sacrificing execution time for less lines. Thanks.

Re Foreign I think I didn't explain myself correctly and I appreciate the info. I haven't used the analysis much st all but clearly something I should check out.

Thanks again to you both.

Kind regards.

I

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.