How to find yesterday last barindex number through array?


#1

Hello,

How to find yesterday last barindex number through array? because i have calculate pivot array value of yesterday Close. i had use normally ref(array,shift), which is today intraday pivot value each bar changing. so i had use another option Lastvalue(barindex()) which is also pivot value each bar changing. if find yesterday barindex value, i could Constant of barindex by intraday. please help.

_SECTION_BEGIN("PVT");

TimeFrameSet( inDaily );

indexDailyClose = Close;

TimeFrameRestore();

// align data back to original interval

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

for( iv = 1; iv < BarCount ; iv++ ) 
{ 

		LastC[iv] = PClose[iv];

}
				
for( i = 2; i < BarCount ; i++ ) 
{ 

		Pvt[ 1 ] = PClose[ 1 ];

			if( Pvt[i] != Pvt[i-1] )
			{
				Pvt[i] =  round( int((( LastC[ i ] - Pvt[i-1] )* 0.33 + Pvt[i-1] ) * 100 )/ 5 ) * 0.05;
				continue;	
			}
			else
			{
				do{
					if( Pvt[i] == Pvt[i-1] )
					{						
						Yday =  Ref(Day(),-1) != Day();
						bi = LastValue( BarIndex());
						Pvt[bi] = Pvt[bi-1];
						break;
					}
				}while( ++i < BarCount );				
			}

} 
GfxTextOut("" + Pvt, 0, 100);

_SECTION_END();

and also another one i need to know algorithm Axes Sales. see the image. how to code it?
number%20scale


#2

@Ganesan I didn't have time to look at your code and am sleep deprived, but I think you asked for

If that is all you are looking for then perhaps this might help.

dn = Day();
BI = BarIndex();

newDay = dn != Ref( dn, -1 ); // New Day, not needed just here for clarification/illustration
LastBarYesterday = dn != Ref( dn, 1 ); // Yesterday's last bar (you are using intra-day data)

LastBarYesterdayBI = ValueWhen( LastBarYesterday, BI ); // value of the Bar Index on that bar

Can produce results like this,

image


#3

Thanks you, I will check & revert you,

Meanwhile please help the code for the above my image file (algorithm chart) which need to movable GFX Axes scales.`
for( j = 1; j < 9; j++)
{
num1 = num1 + i;
}
i++;
}

`


#4

Hello, i had insert the LastBarYesterdayBI, but showing "Error9. Array subscript has to be a number", then i had add lastvalue function, which is more slowing the chart. what is the mistake i had?:roll_eyes:
see the image
image


#5

Recommended readings
https://www.amibroker.com/guide/h_understandafl.html
https://www.amibroker.com/guide/a_mistakes.html


#6

thanks for recommended page. i understood , barindex is returns ARRAY representing.
then why it is show Array subscript has to be a number


#7

@Ganesan

Array subscript has to be a number

means exactly that!

Everything inside the square brackets [] should be a number and NOT an array!

For example:

Close[BarCount-1]; // OK
Close[BarIndex()]; // WRONG

but

Close[LastValue(BarIndex())]; // OK

This is OK since you are using a function to retrieve a number to be used as an index in the [ ].
If you look at the functions documentation, you can clearly see the return type.

For instance, in the case of LastValue() we have:

SYNTAX: LastValue(ARRAY, lastmode = True )
RETURNS: NUMBER

In your code, you are using an array that is returned from a call to ValueWhen() that you are treating as an array subscript.
Check the function documentation again and try to figure out what value you actually need to use.
Adding some exploration columns with all the arrays used in your logic is always a good step to debug your code.


#8

thanks, one think i clear understood BarCount should be use it on [] but BarIndex should not use on array.

but now i could not identify to put this below code.
can you solve this code, because little bit not clear.

_SECTION_BEGIN("PVT");

TimeFrameSet( inDaily );

indexDailyClose = Close;

TimeFrameRestore();

// align data back to original interval

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

for( iv = 1; iv < BarCount ; iv++ ) 
{ 

		LastC[iv] = PClose[iv];

}
				
for( i = 2; i < BarCount ; i++ ) 
{ 

		Pvt[ 1 ] = PClose[ 1 ];

			if( Pvt[i] != Pvt[i-1] )
			{
				Pvt[i] =  round( int((( LastC[ i ] - Pvt[i-1] )* 0.33 + Pvt[i-1] ) * 100 )/ 5 ) * 0.05;
				continue;	
			}
			else
			{
				do{
					if( Pvt[i] == Pvt[i-1] )
					{						
						Yday =  Ref(Day(),-1) != Day();
						bi = valuewhen( Yday, BarIndex());
						Pvt[bi] = Pvt[bi-1];
						break;
					}
				}while( ++i < BarCount );				
			}

} 
GfxTextOut("" + Pvt, 0, 100);

_SECTION_END();

#9

@Ganesan

Is "bi" in this line a NUMBER or an ARRAY?

Before going forward, I suggest you to study until you completely understand the first document that was linked by @codejunkie.

It is vital to use AFL and there are no shortcuts!

Learn how to use array functions, like @portfoliobuilder already showed you, since in many cases there is no need for the loop (try his sample code and see if you are able to explain precisely how it works).

Only when there is no alternative you need to use loops over the bars, but in this case, it is imperative to fully comprehend how to access appropriately any element using the array subscript.

And as I said before, please, also learn how to use exploration to debug your code


#10

bi is Number.
,
yes, your correct said, i am learning this array function step by step.

again i tried, i am little improved.
the analysis report is given detail without error but chart stated some error.

how it is possible.
,image1

image2

						dn = Day();
						BI = BarIndex();

						newDay = dn != Ref( dn, -1 ); // New Day, not needed just here for clarification/illustration
						LastBarYesterday = dn != Ref( dn, 1 ); // Yesterday's last bar (you are using intra-day data)

						LastBarYesterdayBI = ValueWhen( LastBarYesterday, BI ); // value of the Bar Index on that bar
for( i = 2; i < BarCount ; i++ ) 
{ 
		Pvt[ 1 ] = PClose[ 1 ];
			if( LastC[i] != LastC[i-1] )
			{
				Pvt[i] =  round( int((( LastC[ i ] - Pvt[i-1] )* 0.33 + Pvt[i-1] ) * 100 )/ 5 ) * 0.05;
				continue;	
			}
			else
			{
				do{
					if( LastC[i] == LastC[i-1] )
					{	
						Pvt[i] = Pvt[LastBarYesterdayBI];
						break;
					}
				}while( ++i < BarCount );				
			}
} 

#11

@Ganesan your exploration (and the documentation) shows that

LastBarYesterdayBI = ValueWhen( LastBarYesterday, BI ); 

LastBarYesterdayBI is an ARRAY.

So this line, as expected, gives an error.....

Pvt[i] = Pvt[LastBarYesterdayBI]; 

You NEED to use a NUMBER as an array subscript and be sure that this number is in the 0 .. BarCount-1 range.

Honestly, I don't know how to say that in such a way so you'll get it... :pensive:
(even without taking into account the rest of the code that needs to be significantly revised ...)

Again, I suggest to reread and study the linked docs.