How to default array fix in inside of do while loop?

I try the array function in inside of do while as below code. But can’t solve the one particular array value to default and executive with one if condition. That “bi” array see the below code.

Please help “bi” array need to default within conditions executive.

imsfc= 2;
do
{
	CC[0] = 1;
	CC[1] = Iif( C[ 1 ] > C[ 0 ],1,2);
	
	a[imsfc] = C[imsfc];
	b[imsfc] = C[imsfc-1];
	d[imsfc] = C[imsfc-2];	
	
				if(a[imsfc] > b[imsfc] And a[imsfc] > d[imsfc])
				{
					CC[imsfc] = 1;
				}
				else
				{
					if(a[imsfc] < b[imsfc] And a[imsfc] < d[imsfc])
					{
						CC[imsfc] = 2;
					}
					else
					{			
						bi = abs(LastValue(BarIndex())) - abs((imsfc-LastValue(BarIndex())));
						
						prevCC[bi]  = CC[bi-1];
						
						if( C[bi-1] > C[bi-2] )
						{
							max_b[bi] = C[bi-1];
							min_d[bi] = C[bi-2];
						}
						else
						{
							if( C[bi-1] < C[bi-2] )
							{
								max_b[bi] = C[bi-2];
								min_d[bi] = C[bi-1];
							}
						}
						
						if(a[imsfc] <= max_b[bi] And a[imsfc] >= min_d[bi] )
						{
							do
							{	
								CC[imsfc] = 2+prevCC[bi];
								imsfc++;
								a[imsfc] = C[imsfc];
							}while(a[imsfc] <= max_b[bi] And a[imsfc] >= min_d[bi] );
						}				
					}
				}

}while( ++imsfc < BarCount );

Tomasz, could you help the above .

Typically if you don’t get answers it means that your question is totally unclear to the readers. And quite frankly I have absolutely no idea what you mean with “how to default array fix”?
If you try again to describe what you need, with some more clarity, then somebody may be able to help.

Tomasz,
I hope the below image easy to understand to you.

Any one help me for above code debug rectified.

Ganesan: I’m not sure anyone understands what you’re trying to achieve, and the diagram was only marginally helpful. Perhaps you could try explaining your goal without using pseudo-code. How would you explain your goal to another trader who is NOT a programmer?

  • Matt
1 Like

@Ganesan It looks like your code has this logic:

if this bar's closing price is higher than the previous 2 bars
  CC[bar] = 1
else if this bar's closing price is lower than the previous 2 bars
  CC[bar] = 2
Otherwise
  CC[bar] = something other value

Could you clarify your CC array? What are the possible values in the CC array? Are you trying to measure a numeric quantity, or capture a categorical state?

TIPS:

  1. It might be possible to convert your code to array expressions. Using vectorized array operations is usually easier to understand, is less likely to have bugs, and is also much faster than writing loops.
  2. Make sure the CC array is always assigned a value at every position in the array. Do not skip over some positions.
  3. Move invariant expressions outside loops. For example, LastValue(BarIndex()) never changes and can be computed once before the loop.
2 Likes

Thanks Steve,

It’s working, one single array should fix default with in conditions.

bi = StrToNum(NumToStr( LastVisibleValue(BarIndex())-(LastVisibleValue(BarIndex())-imsfc),1.2)) ;

The key to writing the code is ability to express one’s idea CLEARLY in plain language.
If your description lacks clarity you can’t code it. You first need to crystalize idea in your mind and be able to describe it clearly (in the way @Steve has shown). Only once you are able to describe idea in plain sentences with pure mathematical quality somebody may be able to transfer your idea into the code.