How to create an array?

I try to specify & extracting time in minutes then manipulating result with str functions ,
the problem is that the result ( which is ETM ) is not an array ( i am not really sure but i think so ) and i mean that its value is defined only when i select a certain bar , its not a continuous series of date expressed by a line such as C,H,L,O,V

In the below code , i wrote a condition and used REF Functions to make some comparisons but it doesn't work


TN  = TimeNum();
ETM = Extracted_Time_in_Minute = StrToNum(StrLeft(StrRight(NumToStr(TN,1.0,False),4),2));  // for( i=0; i<BarCount; i++) { X[i] = ETM[i]; } // i tried to use loop but nothing change
XY  = int(ETM/5);
YX  = frac(ETM/5);
C1  = Ref(XY,0)==Ref(XY,-1) AND Ref(YX,0)>Ref(YX,-1);

My Questions is ...

How can i convert ETM to an array , so that ref can work properly ?

BTW , From programming point of view , Should i Consider ETM as a variable ? is that right ?

If you want to get minutes array there two example ways:

Extracting from TimeNum:

tn = TimeNum();
minutes = (tn / 100 ) % 100;

or

using Minute() function.

Comparison exploration:

tn = TimeNum();
minutes1 = (tn / 100 ) % 100;
minutes2 = Minute();

Filter = 1;
AddTextColumn( Interval(2), "Interval", 1 );
AddColumn( minutes1, "Minute from Timenum()", 1 );
AddColumn( minutes2, "From Minute() function", 1 );

to find out type of variable use typeof() (instead of guessing):

printf( "Type of ETM var: %s", typeof(ETM) );

Since you used StrToNum to convert string to number EMT type is number.

Your "to string" conversion of array will not create an array of strings but it just converts last value of array to string. Then StrtoNum conversion will get number from string of last or currently selected value of array.

3 Likes

Thanks @fXshrat for your continous help
Although your answer is direct and easy .. but i don't know why it did not cross my mind .. i really astonishing myself !!!
Apart from my issue which is now solved , is it possible to creat an array from a series of numbers ? .. i am just curious to know

I do not know what is your specific use case...
but possibly the simplest way to create a custom array (row or column vector) out of a series of known numbers is using MxFromString() function.

mat = MxFromString( "[1;2;3;4;5]" );
printf( MxToString(mat) );
3 Likes

Thanks fxshrat , i will study MxFromString() tommorow in detail
But what i meant if i have a case just as this one in the above .. do you think it is possible to create an array from it .. just curious to know :slight_smile:

You don't need to "create" array. Arrays are "created" automatically if you assign values to variable using array subscript operator:

// just assign values and 'x' will become an array
x[ 0 ] = 1;
x[ 1 ] = 3;
x[ 2 ] = 10;
...

Just make sure to check BarCount and don't exceed it because all arrays in AFL have length equal to underlying quotation data size, so if say MSFT has 10000 quotes you will have automatically 10000 elements in all arrays when MSFT is selected symbol.

http://www.amibroker.com/kb/2014/09/22/do-not-make-assumptions-on-number-of-bars/

4 Likes

I don't quite understand why you would want to do that since post #2 already has minute array.

But since you ask about your original post again I'm suspecting that you seem to be after converting time number(s) being located in some text file. Yes?

If so then simply read times column from text file and convert 6 digit timenum from string to number and then see post number two again for extracting minute from timenum array.

/// modified example from 'File' code snippets of AmiBroker editor
/// array of numbers been discussed here 
/// @link https://forum.amibroker.com/t/how-to-create-an-array/7516/7
time_num = Null;
if( Status( "action" ) == actionExplore ) {
	fh = fopen( "C:\\test.csv", "r" );
	
	if( fh ) {

		n = 0;
		while( ! feof( fh ) )  {
			line = fgets( fh ); // read a line of text
			
			if( n < 10 )
				_TRACE( line );

			// ......    
			column_2 = StrExtract( line, 1 );

			if( n < BarCount-1 ) {			
				time_num[n] = StrToNum( column_2 );
				if( n < 10 )
					_TRACEF( "time_num: %g", time_num[n] );	
				n++;				
			}
			// .....
			
		}

		fclose( fh );
	} else 
		Error( "ERROR: file can not be open" );		
	
	StaticVarSet( "TimeNum_From_File", time_num );
}

time_num = StaticVarGet( "TimeNum_From_File" );
minutes = (time_num / 100 ) % 100;

printf( "\ntype of 'minutes' var: %s", typeof(minutes) );

Filter = BarIndex() <= n-1;
AddTextColumn( Interval(2), "Interval", 1 );
AddColumn( BarCount, "BarCount", 1 );
AddColumn( n, "Number of text lines", 1 );
AddColumn( time_num, "time_num", 1 );
AddColumn( minutes, "Minute from time_num from file", 1 );

Content of test.csv been used for upper code:

2015-05-15,090000
2015-05-15,090100
2015-05-15,090200
2015-05-15,090300
2015-05-15,090400
2015-05-15,090500
2015-05-15,090600
2015-05-15,090700
2015-05-15,091000
2015-05-15,091100

153

3 Likes

Just additional information:
While Barcount length of 1-dim array (of a DB symbol) can not be exceeded (as mentioned)...
One the other hand 2-dim arrays (see Matrix features) can have size going beyond Barcount length. But their size is restricted as well... to 500 million elements (-> MxGetSize(mat,0) * MxGetSize(mat, 1)).

2 Likes

FYI, in upper code rather put

n = 0;

of line 10 to a line above of

if( Status( "action" ) == actionExplore ) {

Otherwise it will create error of "Variable n used without having been intialized" in regards to lines 42 and 45 if code is not applied in Exploration. (I only applied a test in exploration that's why I missed it)

2 Likes

Thank you very much Tomasz for clarification ,

I know that i can make an array like the one in your example which is similar to loop (as i understand it)

However in my case what i needed is to create an array from a variable number such that one in my example ( ETM variable ) ,

ETM has a a specific value at each element but they are not connected in one series that's why we classify the return as "Number" , and what i seeked is to connect all of these Numbers in one series to become an "Array"

Thanks fxshrat i appreciate so much your time and knowledge you dedicate to educate and help others

I am going to study your code in details row by row and word by word , because some functions are new to me

BTW , i always try to be clear and specific as much as possible and i wouldn't exhaust the reader by letting him guess my Intentions
and as i told you it 's just a curiosity :blush: