nthHighestArray  [SOLVED]

Questions about MultiCharts and user contributed studies.
FATSTrader
Posts: 15
Joined: 14 Feb 2011
Has thanked: 7 times
Been thanked: 3 times

nthHighestArray  [SOLVED]

Postby FATSTrader » 25 Jan 2015

Hi,

I am trying to use nthHighestArray( array, size, nth ) per the EL documentation but in MC I only get zero as return value in a fully populated array.

Anyone use this is MC? The functions are there but I can't seem to get them to work.

Appreciate you help.

Thanks!

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

Re: nthHighestArray

Postby TJ » 25 Jan 2015

Hi,
I am trying to use nthHighestArray( array, size, nth ) per the EL documentation but in MC I only get zero as return value in a fully populated array.
Anyone use this is MC? The functions are there but I can't seem to get them to work.
Appreciate you help.
Thanks!
Please post your code for debugging.



ps.
[FAQ] How to Post Codes (that people can read)
viewtopic.php?f=16&t=11713

FATSTrader
Posts: 15
Joined: 14 Feb 2011
Has thanked: 7 times
Been thanked: 3 times

Re: nthHighestArray

Postby FATSTrader » 25 Jan 2015

Hi, Below is the code. No success with nthHighestArray, nthLowestArray or nthExtremesArray.

Code: Select all

Vars: count( 0 ), cell( 0 ), oExtremeVal( 0 ), oExtremePosRaw( 0 );
Array: myDynamicArray[]( 0 );

if High > Highest( High,20 )[1] then count = count + 1;
Array_SetMaxIndex( myDynamicArray, count );

myDynamicArray[ count ] = High;

if lastBarOnChart then
Begin
value1 = array_Sort( myDynamicArray, 1, 5, false );

for cell=1 to count
begin
print( ELDateToString(Date):7:0,", ", myDynamicArray[cell],", ", cell:4:0 );
end;

print( "MaxIndex/Sum:, ", Array_GetMaxIndex( myDynamicArray ):4:0,"/",Array_Sum( myDynamicArray, 1, 2 ):2:2 );

value1 = ExtremesArray( myDynamicArray, count, 1, oExtremeVal, oExtremePosRaw );
print( "Extremes: ", oExtremeVal:4:2, ", ", oExtremePosRaw:4:0 );

value1 = nthHighestArray( myDynamicArray, count, 1 );
print( "nthHighest: ", value1 );

value1 = nthLowestArray( myDynamicArray, count, 1 );
print( "nthLowest: ", value1 );

value1 = NthExtremesArray( myDynamicArray, count, 1, -1, oExtremeVal, oExtremePosRaw );
print( "nthExtremes: ", value1, ", ", oExtremeVal:4:2, ", ", oExtremePosRaw:4:0 );


End;
Output:
MaxIndex/Sum:, 1808/4.43
Extremes: 4.15, 246
nthHighest: 0.00
nthLowest: 0.00
nthExtremes: -1.00, 0.00, -1

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

Re: nthHighestArray

Postby JoshM » 27 Jan 2015

I am trying to use nthHighestArray( array, size, nth ) per the EL documentation but in MC I only get zero as return value in a fully populated array.
MaxIndex/Sum:, 1808/4.43
Extremes: 4.15, 246
nthHighest: 0.00
nthLowest: 0.00
nthExtremes: -1.00, 0.00, -1
The source code of `nthHighestArray()` shows that it uses the `NthExtremesArray()` function (see the `value1` line):

Code: Select all

inputs:
PriceValueArray[MaxSize]( numericarray ),
Size( numericsimple ),
N ( numericsimple ) ;

variables:
var0( 0 ),
var1( 0 ) ;

Value1 = NthExtremesArray( PriceValueArray, Size, N, 1, var0, var1) ;

NthHighestArray = var0 ;
And this `NthExtremesArray` function can only work with arrays that are at most 100 elements big (see the `condition1` statement):

Code: Select all

inputs:
PriceValueArray[MaxSize]( numericarray ),
Size( numericsimple ),
N ( numericsimple ),
HiLo( numericsimple ),
oExtremeVal( numericref ),
oExtremePosRaw( numericref ) ;



arrays:
arr0[ 2, 100 ]( 0 ) ;

condition1 = N > Size or Size > MinList( MaxSize, 100 ) ;
if condition1 then
begin
oExtremeVal = 0 ;
oExtremePosRaw = -1 ;
NthExtremesArray = -1 ;
end
else
begin



for Value1 = 1 to Size
begin
arr0[ 1, Value1 ] = PriceValueArray[Value1] ;
arr0[ 2, Value1 ] = Value1 - 1 ;
end ;


Value1 = Sort2DArray( arr0, 2, Size, HiLo ) ;

oExtremeVal = arr0[ 1, N ] ;
oExtremePosRaw = arr0[ 2, N ] ;
NthExtremesArray = 1 ;
end ;
So your dynamic array of 1808 elements is too big for these functions.

Wouldn't it be easier (and with cleaner code than these functions have) to use Array_Sort to sort your dynamic array? The highest value will then be the first (or last) element, while the lowest will be the last (or first) element.

FATSTrader
Posts: 15
Joined: 14 Feb 2011
Has thanked: 7 times
Been thanked: 3 times

Re: nthHighestArray

Postby FATSTrader » 27 Jan 2015

Thanks for catching that for me, Josh. Actually for my current purpose I can use the ExtremesArray since it doesn't have the 100 cell limitation, or as you said, just sort it myself.

But I will need the Nth capability for another project. Easy enough to create a new version of NthExtremesArray:

Code: Select all

inputs:
PriceValueArray[MaxSize]( numericarray ),
Size( numericsimple ),
Nth ( numericsimple ),
HiLo( numericsimple ),
oExtremeVal( numericref ),
oExtremePosRaw( numericref ) ;

//Update max array size variable and array declaration as required
vars: arrayMax( 2000 );
arrays:
arr0[ 2, 2000 ]( 0 ) ;

condition1 = Nth > Size or Size > MinList( MaxSize, arrayMax ) ;
if condition1 then
begin
oExtremeVal = 0 ;
oExtremePosRaw = -1 ;
NthExtremesArrayII = -1 ;
RaiseRunTimeError("NthExtremesArrayII: Input Array Size is greater than array maximum! See code. " );
end
else
begin
for Value1 = 1 to Size
begin
arr0[ 1, Value1 ] = PriceValueArray[Value1] ;
arr0[ 2, Value1 ] = Value1 ;
end ;

Value1 = Sort2DArray( arr0, 2, Size, HiLo ) ;

oExtremeVal = arr0[ 1, Nth ] ;
oExtremePosRaw = arr0[ 2, Nth ] ;
NthExtremesArrayII = 1 ;
end ;

MaxIndex/Sum:, 1809/4.41
Extremes: 4.15, 246
nthHighest: 0.00
nthLowest: 0.00
nthExtremes: -1.00, 0.00, -1
nthExtremesII: 1.00, 4.15, 246 (1st highest
nthExtremesII: 1.00, 4.08, 243 (2nd highest)
nthExtremesII: 1.00, 1.20, 315 (1st Lowest)

If MC supported multi-dimensional dynamic arrays would be best update, but this will do for my purposes now.

Cheers


Return to “MultiCharts”