Example Blox Description:

Box contains a simple Test-Level String Array Example using numbers in a string format. Numbers were selected because they are the least understood when it comes to sorting strings that look like numbers. Numbers are generated using the Trading Blox Random Number generator. There are also options in this blox for changing how the numbers are sequenced, and for how they are stored in the string array.

 

Example_String_Array_Sorting

Operation Notes:

When testing this blox, start off with the default settings to get an understanding of what it does. Once you've got a sense of how the blox operations, change the fix number option next so that the numbers will sort properly.

 

From here on, changes to the other parameters will help you get an understanding of how the character of the Trading Blox Random Number data series can change by changing the initial start value, or by changing the value of numeric Seed value supplied to the RandomSeed function.

 

When this blox is executed in a Simulation run, the only active script section is the Before Test Script. Before Test script location is the best place I've found for creating a Test-Level String array. It is also the best place for accessing selectable user parameters that can decide which scripts executes during a simulation run.

 

Any script section can be used to store, or access information in a Test-Level script once it has been created. There are no known restrictions on creating the Test-Level script array in other sections as long as you know it will be created before you need to store, or retrieve initialization information in the string array.

 

There are a lot of little script techniques available in this simple example along with the process for creating a Test-Level string array, storing data in each element in each column, sorting data in each column in either ascending, or descending orders, and for how to retrieve the values in each string element.

 

There are also some formatting techniques to make output more readable, or presentable for others. There are also two unique user functions. One user function is intended to return the value of the computer's time of day in the form of the number of seconds that have transpired since 12:00 AM midnight. This is useful for determining the differences between the two different times of the day, or even between days when a serialized date value is part of the calculations.

 

In the second function is a process for creating a unique random number seed value. To create a unique number the script function creates a date serialization process that uses an adjusted Julian date value appended to the value in the number of seconds from midnight. This unique number provides a good source for seeding the random number series with unique values so the random number generator will produce a unique series of number sequences each time the random number generator is operated.

 

 

User Parameters:

Descriptions:

Random Series Sequencing

In the default setting the numbers generated will always be the same for each simulation. This setting is using a fixed seed value. Fixed seed values initialize the Random Number series so that the same sequence of random numbers is generated each time the random number generator is accessed. Having a consistent number in a random series makes trouble shooting random number driven events easier to debug, but they aren't good for "Monte Carlo" type data methods.

 

A second option uses a unique number each time a series of numbers is generated. By using a unique seed value for each random number sequence the order of the numbers generated will not the be same for each simulation run.

Random Number Start

By default the settings use a small number to set the range of numbers that will be created for each series. This small number provides a higher probability that the range of random numbers in the sequence will be over a wider range of values. This was provided because of the limited number of numbers being generated to keep the example within tolerable reading range.

 

A larger start number is available to show that when the numbers have the same number of characters in their values, then the sorting option will sort those numbers correctly as long at the character count in a string number is the same for all string numbers. However, if the number of characters in the string-number changes, the sort operation is likely to not be sorted properly.

Fix String Array Sorting

First Option shows how numbers stored without regard for their ASCII character representation will not always sort correctly.

 

Second Option shows how to adjust for numbers that have different lengths so that when they are sorted they are in a correct ascending, or descending sequence. In the example shown, the number adjustments

Are using variable number of leading zero values. However spaces, underscore characters could also have been used. There are other methods for adjusting string numbers, so if the examples aren't enough information, then try some math approaches, or post a question about your idea.

Max Array Rows to Display

Trading Blox has an upper limit of 10,000 rows that can be created with a Test-Level String Array.

 

However, each row will reserve memory space times that will equally the array element time the number of columns. In this example we are only using 2-columns. However, that can be changed by modifying the script code, and by adjusting how the loops handle the increased, or reduced number of column.

 

For this simple example, the minimum number of rows is set to 10-rows. You get to set the row count from any number between 5, and whatever value seems reasonable to scroll through once the blox has completed.

Blox Use

No Portfolio instruments are required to run this blox.

 

To test this blox, create a system name you want, or use the default system name "Chart" and attach this Blox. When this blox executes, it will generate information into the Trading Blox Log window on the Main screen of the program when the Log option under the Window menu item is selected. It will always generate and send information to the "Print Output.csv" file.

 

"Print Output.csv" file is easily opened using the Main menu File selection and then scrolling down until Result selection is highlighted. With the list visible, select the Print Output option and the output will appear in an Excel spreadsheet. While the data appears easily, it will look better if you access it with the Windows Notepad. File is in the Trading Blox Results folder and will easily open with Notepad, or WordPad.

 

The code examples show ideas about the use of String Array Sorting.  The examples execute the BEFORE TEST script section of the blox available in the Blox MarketPlace.  When example code is executed it will produce something similar to the details shown below:

Example:

'  ==============================================================
'  Example_String_Array_Sorting
'  BEFORE TEST SCRIPT - START
'  ==============================================================
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  CONSTANTS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  StringPad Len Must be as Large as Largest Number Length
sNbrPad = "000000"  
NbrPadLen = Len(sNbrPad)
 
sStrPad = "    "  
StrPadLen = Len(sStrPad)
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  RANDOM SEED ASSIGNMENT
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  Allow Unique Random Series, or Static Series
If Random_Series = UNIQUE_NUMBER_SERIES THEN
  Seed_Number = SystemTime( 3 )'Script.Execute( "Unique_Number" )
ELSE
  Seed_Number =  1084239526
ENDIF '  Random_Series = UNIQUE_SERIES
 
'  Initialize Random Number Series Sequence
Seed_Number = RandomSeed( Seed_Number )
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  CREATE PARAMETER SELECTORS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  Next conditional statement requires a parameter to select
'  the size of the number to use for creating values.  
'  In operation it changes the Range of Numbers so String
'  Sorting Issue is Shown
If Rnd_Number_Start = USE_SMALL_NUMBER THEN
  Rnd_Start = 100   '  Small Value will geneate small numbers
ELSE
  Rnd_Start = 1000 '  Generates larger, 2-digit numbers
ENDIF
 
'  Next conditional statment uses a parameter selector to
'  control how the sorting is performed.
If Pad_Number = ADJUST_NUMBER_ON THEN
  '  Show Sorting Number Fix
  sPadFix = "Fix Number Sorting"
ELSE
  '  Show Sorting Number Fix
  sPadFix = "DO NOT Fix Number Sorting"
ENDIF  
 
'  Use Maximum Row Count Value  
Series_Rows = Max(5, Row_Count)
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  STRING ARRAY ROW & COLUMN SIZE DETAILS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  Array Setup Details
StrRowLen = 512       ' 512 = Maximum TB String Length
Array_Columns = 100
Element_Rows = Series_Rows
 
'  Array Column Locators
Column_1 = 1
Column_2 = 2
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  CREATE TEST-LEVEL STRING ARRAY
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.CreateStringArray( Array_Columns, Element_Rows, StrRowLen )
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  GIVE USER SOME INFORMATION
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRINT
PRINT "Blox: ", block.name, "        Script Name: ", block.scriptName
PRINT
PRINT "Computer Date & Time: ", SystemDate (), " ", SystemTime()
PRINT
PRINT "String Array Creation & String Number Sorting Examples"
PRINT
PRINT "PARAMETERS:"
PRINT "     Array Rows    : ", Element_Rows
PRINT "     Random Start #: ", Rnd_Start
PRINT "     Fix # Sorting : ", sPadFix
PRINT "     Seed Number   : ", Seed_Number
PRINT
PRINT "ARRAY DEFAULTS:"  
PRINT "     String Element Size : ", StrRowLen
PRINT "     Array Column Count  : ", Array_Columns
PRINT
PRINT "-----------------------------------------------------------"
PRINT
PRINT "String Array Created with " _
    + AsString(Array_Columns, 0) _
    + " Columns, and " _
    + AsString(Element_Rows, 0) _
    + " Element Rows."
PRINT
PRINT "Generating Alpha-Numeric Data for Column 1 Rows:"
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  CREATE DATA FOR ARRAY'S FIRST COLUMN ROWS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Ndx = 1 TO Element_Rows
  '  Generate a random number for this row
  sRandom_Num = AsString(Random( Rnd_Start, 10000 ) * 0.01, 2)
 
  '  USER CAN CHANGE HOW NUMBER SORTING WORKS
  If Pad_Number = ADJUST_NUMBER_ON THEN
    '  Build an Initial Row Location Identifier
     sRow = Left(sStrPad, StrPadLen - Len(Ndx)) _
            + AsString(Ndx, 0)
 
    '  Build the data in a simple COMMA Delimited Record
     sTemp1 = Left(sNbrPad, NbrPadLen - Len(sRandom_Num)) _
              + sRandom_Num + "," + " in Row " + sRow
 
    '  Show User the Simple Record
    PRINT sRow, "  " + sTemp1
  ELSE
    '  Build an Initial Row Location Identifier
     sRow = AsString(Ndx, 0)
 
    '  Build the data in a simple COMMA Delimited Record
     sTemp1 = sRandom_Num + "," + " in Row " + sRow
 
    '  Show User the Simple Record
    PRINT Ndx, "  " + sTemp1
  ENDIF
 
  '  Send the Record to the Array in Column 1, Row of Ndx
  test.SetStringArrayElement( Column_1, Ndx, sTemp1 )
Next '  Ndx
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  SEND MORE INFORMATION
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRINT
PRINT "Generating Alpha-Numeric Data for Column 2 Rows:"
PRINT
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  CREATE DATA FOR ARRAY'S SECOND COLUMN ROWS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Ndx = 1 TO Element_Rows
  '  Generate a random number for this row
  sRandom_Num = AsString(Random( Rnd_Start, 10000 ) * 0.01, 2)
 
  '  USER CAN CHANGE HOW NUMBER SORTING WORKS
  If Pad_Number = ADJUST_NUMBER_ON THEN
    '  Build an Initial Row Location Identifier
     sRow = Left(sStrPad, StrPadLen - Len(Ndx)) _
            + AsString(Ndx, 0)
 
    '  Build the data in a simple COMMA Delimited Record
     sTemp2 = Left(sNbrPad, NbrPadLen - Len(sRandom_Num)) _
              + sRandom_Num + "," + " in Row" + sRow
 
    '  Show User the Simple Record
    PRINT sRow, "  " + sTemp2
  ELSE
    '  Build an Initial Row Location Identifier
     sRow = AsString(Ndx, 0)
 
    '  Build the data in a simple COMMA Delimited Record
     sTemp2 = sRandom_Num + "," + " in Row" + sRow
 
    '  Show User the Simple Record
    PRINT Ndx, "  " + sTemp2
  ENDIF
 
  '  Send the Record to the Array in Column 1, Row of Ndx
  test.SetStringArrayElement( Column_2, Ndx, sTemp2 )
Next '  Ndx
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  SEND MORE INFORMATION
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRINT
PRINT "-----------------------------------------------------------"
PRINT
PRINT "Sorting Array Elements in the Column #1 in Descending Order:"
PRINT "Sorting Array Elements in the Column #2 in Ascending Order:"
PRINT
PRINT "-----------------------------------------------------------"
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  SORT EACH STRING ARRAY COLUMN IN DIFFERENT DIRECTIONS
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.SortStringArray( Column_1, 0 , Element_Rows )
test.SortStringArray( Column_2, 1, Element_Rows )
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  SHOW WHAT HAS BEEN ACCOMPLISHED
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRINT
PRINT "Sorted Alpha-Numeric Data for Both Array Columns:"
PRINT
PRINT "Index  Column 1                |    Column 2"
 
For Ndx = 1 TO Element_Rows
  '  Get Record from Column 1, Row #Ndx
  sTemp1 = test.GetStringArrayElement( Column_1, Ndx)
 
  '  Get Record from Column 2, Row #Ndx
  sTemp2 = test.GetStringArrayElement( Column_2, Ndx)
 
  '  Show User Results of Sorted Columns
  sRow = Left(sStrPad, StrPadLen - Len(Ndx)) _
            + AsString(Ndx, 0)
  PRINT sRow, "  " + sTemp1, "    |    " + sTemp2
Next '  Ndx
PRINT
PRINT "-----------------------------------------------------------"
'  ==============================================================
'  BEFORE TEST SCRIPT - END
'  Example_String_Array_Sorting
'  ==============================================================

Returns:

Blox: ,Example_String_Array_Sorting,        Script Name: ,Before Test,

 

Computer Date & Time: ,2014-03-04, ,1:47:38 PM,

 

String Array Creation & String Number Sorting Examples,

 

PARAMETERS:,

    Array Rows    : ,10,

    Random Start #: ,100.000000000,

    Fix # Sorting : ,DO NOT Fix Number Sorting,

    Seed Number   : ,0.000000000,

 

ARRAY DEFAULTS:,

    String Element Size : ,512,

    Array Column Count  : ,100,

 

-----------------------------------------------------------,

String Array Created with 100 Columns, and 10 Element Rows.,

 

Generating Alpha-Numeric Data for Column 1 Rows:,

1,  55.33, in Row 1,

2,  59.69, in Row 2,

3,  71.81, in Row 3,

4,  84.59, in Row 4,

5,  60.67, in Row 5,

6,  85.94, in Row 6,

7,  54.94, in Row 7,

8,  84.88, in Row 8,

9,  42.94, in Row 9,

10,  62.73, in Row 10,

 

Generating Alpha-Numeric Data for Column 2 Rows:,

1,  64.94, in Row1,

2,  39.05, in Row2,

3,  44.32, in Row3,

4,  30.45, in Row4,

5,  89.29, in Row5,

6,  6.61, in Row6,

7,  96.41, in Row7,

8,  27.99, in Row8,

9,  38.96, in Row9,

10,  48.29, in Row10,

 

-----------------------------------------------------------,

Sorting Array Elements in the Column #1 in Descending Order:,

Sorting Array Elements in the Column #2 in Ascending Order:,

-----------------------------------------------------------,

Sorted Alpha-Numeric Data for Both Array Columns:,

 

Index  Column 1            |    Column 2,

  1,  85.94, in Row 6,    |    27.99, in Row8,

  2,  84.88, in Row 8,    |    30.45, in Row4,

  3,  84.59, in Row 4,    |    38.96, in Row9,

  4,  71.81, in Row 3,    |    39.05, in Row2,

  5,  62.73, in Row 10,   |    44.32, in Row3,

  6,  60.67, in Row 5,    |    48.29, in Row10,

  7,  59.69, in Row 2,    |    6.61, in Row6,

  8,  55.33, in Row 1,    |    64.94, in Row1,

  9,  54.94, in Row 7,    |    89.29, in Row5,

 10,  42.94, in Row 9,    |    96.41, in Row7,

-----------------------------------------------------------,

 

Links:

 

See Also:

 

 

Available Downloads:

Description:

Traders Round Table's Blox Marketplace:

Example: String Array Details, Sorting Columns & Random

There are two example blox (version 3.x & 4.x)  available in   Topic:

Example_String_Array_Sorting.tbx

TB4 - Example_String_Array_Sorting

Test-Level String Array Series.pdf

Information Shown Above

Example_String_Array_Sorting.tbx

TB Ver: 3.2.x Compatible String Array Creating, Access, Sorting, and Random Number details

 


Edit Time: 9/25/2017 09:09:58 AM


Topic ID#: 615

 

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software