Need to label a sorted array on screen- any ideas ?  [SOLVED]

Questions about MultiCharts and user contributed studies.
sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Need to label a sorted array on screen- any ideas ?

Postby sptrader » 05 Sep 2015

I'm not a pro programmer but I'm familiar with PL. I've never tried the more advanced tricks like this (sorting arrays etc)-. I've searched the MC wiki , internet etc, and can't find a solution.. to what should be a simple problem...

I'm trying to use an array to sort the market sectors by percentage gain (or loss) best being on top... This is a small example of what I have so far. I can list the sector "values" and sort the percentages correctly but I can't figure out how to label the sorted values from highest to lowest by sector, for example:

Code: Select all

pcnt = -1.564;//ES
pcnt2 = -1.501;//Tech
pcnt3 = -1.943;//Fin

MyArray[1] = pcnt;//ES
MyArray[2] = pcnt2;//Tech
MyArray[3] = pcnt3;//Fin

For Counter = 1 to 3
begin
Sort1 = SortArray(MyArray,3,1);// sorts percentages correctly

print(date:6:0,",",Time:4:0,",",myarray[counter]:2:3,",","Counter=",counter);
end;// prints out the correct order in the printlog ...
The code sorts the numbers correctly but how do I attach labels to the sorted numbers, to show on the screen ? (or can I print the PE output bar on the screen) ?..

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Need to label a sorted array on screen- any ideas ?

Postby TJ » 05 Sep 2015

What do you mean by "Label"?

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Need to label a sorted array on screen- any ideas ?

Postby sptrader » 05 Sep 2015

If I have 10 sector percentages listed and sorted but without some kind of ID, how do I tell them apart. ? They need some kind of label ..to tell which number goes with which sector... ES, tech, financials, health..etc.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Need to label a sorted array on screen- any ideas ?

Postby JoshM » 05 Sep 2015

If you use a two-dimensional array, the first dimension can act as the key (here, the sector) with the second being the value (here the percentage; or vice versa, depending on the ways you want to output it). That way the sector name is always associated with its own percentage.

You can sort those arrays with the `Sort2DArrayByKey()` function.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Need to label a sorted array on screen- any ideas ?

Postby sptrader » 09 Sep 2015

A simple example of a 2 dimensional array,initialized(not sure how to) and incorporating 3 or more numbers and labels would be a huge help...
I can't seem to find an example of what you described anywhere...(wiki, internet etc)

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Need to label a sorted array on screen- any ideas ?

Postby JoshM » 12 Sep 2015

A simple example of a 2 dimensional array,initialized(not sure how to) and incorporating 3 or more numbers and labels would be a huge help...
I can't seem to find an example of what you described anywhere...(wiki, internet etc)
Sorry, I was too quick to respond. A 2 dimensional array can only be of one type: either string for the symbols/sector names or numbers for the percentages. My fault was assuming that an array could hold two different types (the name and the percentage). Instead, that behaviour is possible with dictionaries (which PowerLangauge doesn't have).

Both that's not a problem, it just takes more code. One of the ways to approach this is the following:

* Create a two-dimensional (2D) array, just like you'd have in Excel with the first column holding the percentages and the second column an unique identifier. For example:

Code: Select all

Column A | Column B
23.39 | 1
34.08 | 2
-10.00 | 3
* We then need to sort the 2D array based on the first column (the percentages) from high to low, just like in Excel with the A-Z button. Since the second column is associated with the first, it moves along with the sorting:

Code: Select all

Column A | Column B
34.08 | 2
23.39 | 1
-10.00 | 3
* The last step is then to use the identifier (in the example above: 2, 1, 3) and connect them with their name so that we end up with this:

Code: Select all

Column A | Column B | Column C
34.08 | 2 | Retail
23.39 | 1 | Financial services
-10.00 | 3 | Construction
In the final output we drop the 'Column B' with the identifier since we don't need that anymore. In that case we end up with:

Code: Select all

Column A | Column C
34.08 | Retail
23.39 | Financial services
-10.00 | Construction
And then we have sorted the percentage and associated it with its name. Now onward to the code:

Code: Select all

Variables:
x(0);

Arrays:
Sectors[10](""),
Returns[10, 2](0);

once begin

ClearDebug;

// We'll pull the sector name from this array later on
Sectors[0] = "ES";
Sectors[1] = "Tech";
Sectors[2] = "Fin";
Sectors[3] = "Banks";
Sectors[4] = "Automobile";
Sectors[5] = "Retail";
Sectors[6] = "Real estate";
Sectors[7] = "Medicine";
Sectors[8] = "SomethingElse";
Sectors[9] = "AnotherThing";

// Output
Print("These are the names we match the numbers later with:");
for x = 0 to 9 begin

Print(NumToStr(x, 0), " - Name: ", Sectors[x]);

end;

// We use the second index of the second array as our 'identifier'
Returns[0, 1] = 1;
Returns[1, 1] = 2;
Returns[2, 1] = 3;
Returns[3, 1] = 4;
Returns[4, 1] = 5;
Returns[5, 1] = 6;
Returns[6, 1] = 7;
Returns[7, 1] = 8;
Returns[8, 1] = 9;
Returns[9, 1] = 10;

// The first index of the second array will be our 'percentage returns'
Returns[0, 0] = 234.49;
Returns[1, 0] = 4.49;
Returns[2, 0] = -30.3;
Returns[3, 0] = -99;
Returns[4, 0] = 0.01;
Returns[5, 0] = 120;
Returns[6, 0] = 0.003;
Returns[7, 0] = 564;
Returns[8, 0] = 5;
Returns[9, 0] = 25;

// Output before sorting
Print(NewLine, NewLine, "Before sorting");
Print("(these are the percentages and their unique identifier):");
for x = 0 to 9 begin

Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 0], 5));

end;

// Now sort the two-dimensional array
Sort2DArrayByKey(Returns, 10, 1);

// Output after sorting
Print(NewLine, NewLine, "After sorting:");
Print("(now we've sorted based on the percentages)");
for x = 0 to 9 begin

Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 0], 5));

end;

// Now we need to match the identifiers with the sector/instrument name
Print(NewLine, NewLine, "Final output:");
Print("(matching the identifiers of the 2D array with the name from the 'Sectors' array)");

for x = 0 to 9 begin

Print("Identifier: ", NumToStr(Returns[x, 1], 0), " - ",
"Name: ", Sectors[Returns[x, 1]], " - ",
"Percentage: ", NumToStr(Returns[x, 0], 2), "%");

end;

end;
This code generates the following output:

Code: Select all

These are the names we match the numbers later with:
0 - Name: ES
1 - Name: Tech
2 - Name: Fin
3 - Name: Banks
4 - Name: Automobile
5 - Name: Retail
6 - Name: Real estate
7 - Name: Medicine
8 - Name: SomethingElse
9 - Name: AnotherThing


Before sorting
(these are the percentages and their unique identifier):
Identifier: 1 - Percentage: 234.49000
Identifier: 2 - Percentage: 4.49000
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000
Identifier: 5 - Percentage: 0.01000
Identifier: 6 - Percentage: 120.00000
Identifier: 7 - Percentage: 0.00300
Identifier: 8 - Percentage: 564.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 10 - Percentage: 25.00000


After sorting:
(now we've sorted based on the percentages)
Identifier: 8 - Percentage: 564.00000
Identifier: 1 - Percentage: 234.49000
Identifier: 6 - Percentage: 120.00000
Identifier: 10 - Percentage: 25.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 2 - Percentage: 4.49000
Identifier: 5 - Percentage: 0.01000
Identifier: 7 - Percentage: 0.00300
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000


Final output:
(matching the identifiers of the 2D array with the name from the 'Sectors' array)
Identifier: 8 - Name: SomethingElse - Percentage: 564.00%
Identifier: 1 - Name: Tech - Percentage: 234.49%
Identifier: 6 - Name: Real estate - Percentage: 120.00%
Identifier: 10 - Name: - Percentage: 25.00%
Identifier: 9 - Name: AnotherThing - Percentage: 5.00%
Identifier: 2 - Name: Fin - Percentage: 4.49%
Identifier: 5 - Name: Retail - Percentage: 0.01%
Identifier: 7 - Name: Medicine - Percentage: 0.00%
Identifier: 3 - Name: Banks - Percentage: -30.30%
Identifier: 4 - Name: Automobile - Percentage: -99.00%
As the final output shows, the numbers are sorted from high to low, and the correct name is displayed with each number. I hope this is what you meant? :)

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Need to label a sorted array on screen- any ideas ?  [SOLVED]

Postby sptrader » 13 Sep 2015

JoshM: Thanks so much , I had no idea that it would take so much code to do what I thought was a simple task... I expected a 2 dim array to accept an "identifier", it would have been much simplier that way..
I'll give it a try this week in Rt, thanks again ....

wilkinsw
Posts: 662
Joined: 21 Apr 2013
Has thanked: 154 times
Been thanked: 104 times

Re: Need to label a sorted array on screen- any ideas ?

Postby wilkinsw » 10 Nov 2016

Here's a head scratcher.....

Why, if I take JoshM's code and change it as to not use the ZERO array index, then the sort function doesn't succeed in sorting. Is there something about the Sort2DArrayByKey function that I'm missing?

Code: Select all

Variables:
x(0);

Arrays:
Sectors[10](""),
Returns[10, 2](0);

once begin

ClearDebug;

// We'll pull the sector name from this array later on
Sectors[1] = "ES";
Sectors[2] = "Tech";
Sectors[3] = "Fin";
Sectors[4] = "Banks";
Sectors[5] = "Automobile";
Sectors[6] = "Retail";
Sectors[7] = "Real estate";
Sectors[8] = "Medicine";
Sectors[9] = "SomethingElse";
Sectors[10] = "AnotherThing";

// Output
Print("These are the names we match the numbers later with:");
for x = 1 to 10 begin

Print(NumToStr(x, 0), " - Name: ", Sectors[x]);

end;

// We use the second index of the second array as our 'identifier'
Returns[1, 2] = 1;
Returns[2, 2] = 2;
Returns[3, 2] = 3;
Returns[4, 2] = 4;
Returns[5, 2] = 5;
Returns[6, 2] = 6;
Returns[7, 2] = 7;
Returns[8, 2] = 8;
Returns[9, 2] = 9;
Returns[10, 2] = 10;

// The first index of the second array will be our 'percentage returns'
Returns[1, 1] = 234.49;
Returns[2, 1] = 4.49;
Returns[3, 1] = -30.3;
Returns[4, 1] = -99;
Returns[5, 1] = 0.01;
Returns[6, 1] = 120;
Returns[7, 1] = 0.003;
Returns[8, 1] = 564;
Returns[9, 1] = 5;
Returns[10, 1] = 25;

// Output before sorting
Print(NewLine, NewLine, "Before sorting");
Print("(these are the percentages and their unique identifier):");
for x = 1 to 10 begin

Print("Identifier: ", NumToStr(Returns[x, 2], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 1], 5));

end;

// Now sort the two-dimensional array
Sort2DArrayByKey(Returns, 10, 1);

// Output after sorting
Print(NewLine, NewLine, "After sorting:");
Print("(now we've sorted based on the percentages)");
for x = 1 to 10 begin

Print("Identifier: ", NumToStr(Returns[x, 2], 0), " - ",
"Percentage: ", NumToStr(Returns[x, 1], 5));

end;

// Now we need to match the identifiers with the sector/instrument name
Print(NewLine, NewLine, "Final output:");
Print("(matching the identifiers of the 2D array with the name from the 'Sectors' array)");

for x = 1 to 10 begin

Print("Identifier: ", NumToStr(Returns[x, 2], 0), " - ",
"Name: ", Sectors[Returns[x, 2]], " - ",
"Percentage: ", NumToStr(Returns[x, 1], 2), "%");

end;

end;
output:

Code: Select all

These are the names we match the numbers later with:
1 - Name: ES
2 - Name: Tech
3 - Name: Fin
4 - Name: Banks
5 - Name: Automobile
6 - Name: Retail
7 - Name: Real estate
8 - Name: Medicine
9 - Name: SomethingElse
10 - Name: AnotherThing


Before sorting
(these are the percentages and their unique identifier):
Identifier: 1 - Percentage: 234.49000
Identifier: 2 - Percentage: 4.49000
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000
Identifier: 5 - Percentage: 0.01000
Identifier: 6 - Percentage: 120.00000
Identifier: 7 - Percentage: 0.00300
Identifier: 8 - Percentage: 564.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 10 - Percentage: 25.00000


After sorting:
(now we've sorted based on the percentages)
Identifier: 1 - Percentage: 234.49000
Identifier: 2 - Percentage: 4.49000
Identifier: 3 - Percentage: -30.30000
Identifier: 4 - Percentage: -99.00000
Identifier: 5 - Percentage: 0.01000
Identifier: 6 - Percentage: 120.00000
Identifier: 7 - Percentage: 0.00300
Identifier: 8 - Percentage: 564.00000
Identifier: 9 - Percentage: 5.00000
Identifier: 10 - Percentage: 25.00000


Final output:
(matching the identifiers of the 2D array with the name from the 'Sectors' array)
Identifier: 1 - Name: ES - Percentage: 234.49%
Identifier: 2 - Name: Tech - Percentage: 4.49%
Identifier: 3 - Name: Fin - Percentage: -30.30%
Identifier: 4 - Name: Banks - Percentage: -99.00%
Identifier: 5 - Name: Automobile - Percentage: 0.01%
Identifier: 6 - Name: Retail - Percentage: 120.00%
Identifier: 7 - Name: Real estate - Percentage: 0.00%
Identifier: 8 - Name: Medicine - Percentage: 564.00%
Identifier: 9 - Name: SomethingElse - Percentage: 5.00%
Identifier: 10 - Name: AnotherThing - Percentage: 25.00%


Return to “MultiCharts”