Array copy and sort question

Questions about MultiCharts and user contributed studies.
User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Array copy and sort question

Postby joebone » 23 Jan 2019

If I want to copy and sort an array then take an action on the 5 highest values in that array.... Is this how I would do it?

Code: Select all


index1 = pmm_get_global_named_num("1");
index2 = pmm_get_global_named_num("2");
index3 = pmm_get_global_named_num("3");
index4 = pmm_get_global_named_num("4");
index5 = pmm_get_global_named_num("5");
index6 = pmm_get_global_named_num("6");
index7 = pmm_get_global_named_num("7");
index8 = pmm_get_global_named_num("8");
index9 = pmm_get_global_named_num("9");
index10 = pmm_get_global_named_num("10");



array: Indexes[1,10](0);

Indexes[1,1] = index1;
Indexes[1,2] = index2;
Indexes[1,3] = index3;
Indexes[1,4] = index4;
Indexes[1,5] = index5;
Indexes[1,6] = index6;
Indexes[1,7] = index7;
Indexes[1,8] = index8;
Indexes[1,9] = index9;
Indexes[1,10] = index10;

Array: C_Indexes[1,10](0);

array_copy(Indexes,1,C_Indexes,1,10)

array_sort(C_Indexes,1,10,True);


Do I need the 1 in the Array [ 1 , 10 ]

once sorted would you suggest I do a

if C_Indexes[1,1.....5] = Indexes[1,1] then etc.....

is that the best way to order and identify the highest 5 values?

Thanks


QUICK UPDATE I have taken out the 1 in [ 1 , 10 ] and gotten it to compile.

still kinda shooting in the dark a little

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

Re: Array copy and sort question

Postby TJ » 23 Jan 2019

Please post the complete code relating to the array.

ie. including the declarations.

User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Re: Array copy and sort question

Postby joebone » 23 Jan 2019

Please post the complete code relating to the array.

ie. including the declarations.

Im working through it ATM. This is my first array.


Here is the latest version.

Code: Select all


index1 = pmm_get_global_named_num("1");
index2 = pmm_get_global_named_num("2");
index3 = pmm_get_global_named_num("3");
index4 = pmm_get_global_named_num("4");
index5 = pmm_get_global_named_num("5");
index6 = pmm_get_global_named_num("6");
index7 = pmm_get_global_named_num("7");
index8 = pmm_get_global_named_num("8");
index9 = pmm_get_global_named_num("9");
index10 = pmm_get_global_named_num("10");



array: Indexes[10](0);

Indexes[1] = index1;
Indexes[2] = index2;
Indexes[3] = index3;
Indexes[4] = index4;
Indexes[5] = index5;
Indexes[6] = index6;
Indexes[7] = index7;
Indexes[8] = index8;
Indexes[9] = index9;
Indexes[10] = index10;

Array: Indexes_C[10](0);

array_copy(Indexes,1,Indexes_C,1,10);

array_sort(Indexes_C,1,10,False);

//Buy order
If i_marketposition = 0 then
If
Indexes[1] = Indexes_C[1] or
Indexes[1] = Indexes_C[2] or
Indexes[1] = Indexes_C[3] or
Indexes[1] = Indexes_C[4] or
Indexes[1] = Indexes_C[5]
then
buy at next bar open;

//Sell order
if i_marketposition = 1 then
If
Indexes[1] = Indexes_C[6] or
Indexes[1] = Indexes_C[7] or
Indexes[1] = Indexes_C[8] or
Indexes[1] = Indexes_C[9] or
Indexes[1] = Indexes_C[10]
then
sell at next bar open;


//END
is this the section you were asking for?

I think I could do those if then statements with a fold but I'm not an expert there either.

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

Re: Array copy and sort question

Postby TJ » 23 Jan 2019

. . .

is this the section you were asking for?

. . .
I am not asking for anything.
I am merely suggesting to you, if you are looking for coding help, you need to post your codes. Otherwise, people do not know how to help you.

User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Re: Array copy and sort question

Postby joebone » 23 Jan 2019

. . .

is this the section you were asking for?

. . .
I am not asking for anything.
I am merely suggesting to you, if you are looking for coding help, you need to post your codes. Otherwise, people do not know how to help you.

Right. I meant to imply, ** is this enough to identify and help me find a solution.


my objective is to order the indexes then take a position if index1 falls into the top 5 indexes.

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: Array copy and sort question

Postby rrams » 25 Jan 2019

Hi Joe, you are very close. Arrays are always indexed starting with zero, not one.
array: Indexes[1, 10](0); is a two dimensional array with eleven elements of value zero in each of the dimensions.
So instead you want: array: Indexes[9](0), Indexes_C[9](0);
When you copy the array you start at index zero and copy ten elements.
When you sort the copied array the start index is zero and the ending index is nine.
To determine if a variable value falls within the first five elements of the sorted array you can just use a greater than boolean operator comparison to the middle array element.

User avatar
joebone
Posts: 175
Joined: 05 Sep 2018
Has thanked: 53 times
Been thanked: 4 times

Re: Array copy and sort question

Postby joebone » 26 Jan 2019

Hi Joe, you are very close. Arrays are always indexed starting with zero, not one.
array: Indexes[1, 10](0); is a two dimensional array with eleven elements of value zero in each of the dimensions.
So instead you want: array: Indexes[9](0), Indexes_C[9](0);
When you copy the array you start at index zero and copy ten elements.
When you sort the copied array the start index is zero and the ending index is nine.
To determine if a variable value falls within the first five elements of the sorted array you can just use a greater than boolean operator comparison to the middle array element.

Hey! Thanks for the help. Does the sort_array work with element 0? I thought the MC array functions only work starting on the 1st one?

Thanks so much for the help!


Return to “MultiCharts”