Do while conditions

Hi,

I have written do while conditions, the expression in a do-while, the two expression value is constantly & one expression value i++.

My doubts is how to constant value executive in value in numerical on do while.

for ( i = 3; i < BarCount ; i++)
{
	if( LastC[ i ] != LastC[ i-1 ] )
	{
		d[ i ] = Pvt   [ i-2 ];
		b[ i ] = Pvt   [ i-1 ];
		a[ i ] = Pvt   [ i ];		
					
		if(a[ i ] > b[ i ] And a[ i ] > d[ i ])
		{
			MS[ i ] = 1;
		}
		else
		{
			if(a[ i ] < b[ i ] And a[ i ] < d[ i ])
			{
				MS[ i ] = 2;
			}
			else
			{					
				max_b = Max(b[ i ], d[ i ]);
				min_d = Min(b[ i ], d[ i ]);
				prevMS[ i ] = MS[ i-1 ];

				do
				{
					MS[ i ] = 2 + IIf( prevMS[ i ] == 3, 1, IIf(prevMS[ i ] == 4, 2, prevMS[ i ]));		
						break;				
				}	
				}while( a[ i ] <= max_b And a[ i ] >= min_d   );
			}		
		}
	}
	else
	{	
		if( LastC[ i ] == LastC[ i-1 ] )
		{
			MS[ i ] = MS[ i-1 ];	
		}
	}	
}

Constant value is max_b & min_d. In this don't change once conditions complete. But value a[i] should be i++.

How to constant value in do value.

Sorry, I don't understand your question. However, I see that you've used a break statement inside your loop, which means that it will terminate on the first iteration. I'm guessing that's not what you want.

You should review this: https://www.amibroker.com/guide/keyword/break.html

My question is max_b & min_d value don't change once a[i] break above value of max_b or below the value of min_d.

while( a[ i ] <= max_b And a[ i ] >= min_d   )

Your code does not seem to have properly balanced braces. There is extra brace before 'while':
Instead of:

                do
				{
					MS[ i ] = 2 + IIf( prevMS[ i ] == 3, 1, IIf(prevMS[ i ] == 4, 2, prevMS[ i ]));		
						break;				
				}	// one brace too much
				}while( a[ i ] <= max_b And a[ i ] >= min_d   );

you should rather have:

                do
				{
					MS[ i ] = 2 + IIf( prevMS[ i ] == 3, 1, IIf(prevMS[ i ] == 4, 2, prevMS[ i ]));		
						break;				
				} 
                while( a[ i ] <= max_b And a[ i ] >= min_d   );

Use Edit->Code Prettify to quickly find such things. Prettify does the indenting and makes it easier to spot such mistakes.

i removed extra brace, bot not constants value max_b & min_d, it is change i++.

i need constants value max_b & min_d.

while( a[ i ] <= max_b And a[ i ] >= min_d   );

see the comparison chart Explore result and Excel sheet.
2018_05_09_01_46_35_Start

Please listen to what @mradtke wrote - you've got break inside do-while which means exit the loop immediately. Also there can be other issues in your code (but it is incomplete and we can't run it because there are missing parts). You should use advice given here: How do I debug my formula?

thanks for debug information. it's really useful things.

i applied my condition in _TRACE and PLOT, while compare with explore value which showing different value. how it is possible and where i am mistakes.

trace

@Tomasz, thanks to advise me the above _TRACE.

TimeFrameSet( inDaily );

indexDailyClose = Close;

TimeFrameRestore();

PClose = Ref(TimeFrameExpand( indexDailyClose, inDaily),-1);

for( i = 1; i < BarCount ; i++ ) 
{ 
		LastC[i] = PClose[i];
}

MSF[ 1 ] = LastC[ 1 ];

for( i = 2; i < BarCount ; i++ ) 
{ 	
	if( dn != Day() )
	{
		prev = MSF[ i-1 ];
		MSF[ i ] =  round( int((( LastC[ i ] - prev )* 0.33 + prev ) * 100 )/ 5 ) * 0.05; 
	}
	else
	{
		if( dn == Day() )
		{
			MSF[ i ] =  MSF[ i-1 ];
		}
	}
}

Plot(MSF,"MSF",colorBlack);
_TRACE("MSF"+MSF);

@Tomasz thanks to reply

hello Friends,

i have some doubt for do statement while (expression) the following the below code.

i had using time frame inDaily.

previous Day 3 bar index value, that means past 3 days. suppose to yesterday (a[ f ]) value in between of previous 2 day (max_b & min_d). Do-While expression executive. a[ f ] array value check every day with max_b & min_d, if expression is false, statement terminate, if expression is true, statement repeated.

i tried many times but _Trace showing 0. where i am mistake.

bi = LastValue( BarIndex() );
						
max_b = Max( MSF[ bi-1 ] , MSF[ bi-2 ] ) ;
min_d = Min( MSF[ bi-1 ] , MSF[ bi-2 ] ) ;
prevMSFC[ f ] = MSFC[ f-1 ];
						
if( a[ f ] <= max_b And a[ f ] >= min_d )
{
	do
	{
		MSFC[ f ] = 2 + IIf( prevMSFC[ f ] == 3, 1, IIf(prevMSFC[ f ] == 4, 2, prevMSFC[ f ]));		
		f++;									
	}while( a[ f ] <= max_b And a[ f ] >= min_d );
}

Hello Friends

tell me, where i am mistaken on code.

what is the definition of the variable f ?

You can also share the _Trace() statements which are not there in your code.

1 Like

f for init-expression.

trace

MSFC[ 1 ] = 1; // G
MSFC[ 2 ] = iif(MSF[ 2 ] > MSF[ 1 ],1,2);
max_b[ 0 ] = 0; // Dummy Value for first Array
min_d[ 0 ] = 0; // Dummy Value for first Array

f = 3; // starting point
do
{
	d[ f ] = MSF[ f-2 ];
	b[ f ] = MSF[ f-1 ];
	a[ f ] = MSF[ f ];		
	
	MSFColor[ f ] = MSFC[ f-1 ];
		
		if(a[ f ] > b[ f ] And a[ f ] > d[ f ])
		{
			MSFC[ f ] = 1; // G
		}else
			{
				if(a[ f ] < b[ f ] And a[ f ] < d[ f ])
				{
					MSFC[ f ] = 2; // R
				}else
					{	
						bi = LastVisiblevalue( BarIndex() );
						
						max_b[ bi ] = Max( MSF[ bi-1 ] , MSF[ bi-2 ] ) ;
						min_d[ bi ] = Min( MSF[ bi-1 ] , MSF[ bi-2 ] ) ;
												
						prevMSFC[ f ] = MSFC[ f-1 ];
						
						if( a[ f ] <= max_b[ bi ] And a[ f ] >= min_d[ bi ] )
						{
							do
							{
								MSFC[ f ] = 2 + IIf( prevMSFC[ f ] == 3, 1, IIf(prevMSFC[ f ] == 4, 2, prevMSFC[ f ]));		
								f++;									
							}while( a[ f ] <= max_b[ bi ] And a[ f ] >= min_d[ bi ] );
							
						} 
					}		
			}	
}while( ++f < BarCount );