Store multiple values from double in an array

 

A double variable output is multiple values as shown in screenshot

I want to store them in array, how can i do that?

I am trying this but it only returns first value,

// e.g. if double value; // a variable which has multiple value
double value[];
value[0] = value;
 

I suggest you read up about https://www.mql5.com/en/docs/basis/types/dynamic_array

In your code there seem to be some issues:

1) Your array is sized 0 by default - you need to call ArrayResize() to create capacity

2) You also seem to be confusing the name of the array and the data (both are called "value" ).

Here is a small example to give you an idea - in case you need a 2d array the 2nd example shows that.

(PS - You don't need to call ArrayResize in the loop - you can call it just once before the loop if you know the max size)

//---
// e.g. if double value; // a variable which has multiple value
   double value = 0.000;
   double value_array[];

   for(int i=0; i<5; i++)
     {
      ArrayResize(value_array, i+1);
      value_array[i] = value++;
     }

   ArrayPrint(value_array);


//2 col array
   value = 0;
   double value_array2d[][2];
   for(int i=0; i<5; i++)
     {
      ArrayResize(value_array2d, i+1);
      value_array2d[i,0] = value++;
      value_array2d[i,1] = value++;
     }

   ArrayPrint(value_array2d);

Dynamic Array Object

Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
Documentation on MQL5: Language Basics / Data Types / Dynamic Array Object
  • www.mql5.com
Dynamic Array Object - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
R4tna C #:

I suggest you read up about https://www.mql5.com/en/docs/basis/types/dynamic_array

In your code there seem to be some issues:

1) Your array is sized 0 by default - you need to call ArrayResize() to create capacity

2) You also seem to be confusing the name of the array and the data (both are called "value" ).

Here is a small example to give you an idea - in case you need a 2d array the 2nd example shows that.

(PS - You don't need to call ArrayResize in the loop - you can call it just once before the loop if you know the max size)

Dynamic Array Object

thank you, how can i pass these value to EA using iCustom? when i pass these values to EA, it only result is first value, but on indicator it shows multiple values on printing

 
Arpit T #: , how can i pass these value to EA using iCustom?

ICustom only reads buffers, not arrays.

 
Arpit T #:

thank you, how can i pass these value to EA using iCustom? when i pass these values to EA, it only result is first value, but on indicator it shows multiple values on printing

I suggest reviewing the code example in the documentation - it shows the use of a double array and usage of a buffer:

double         Label1Buffer[];

...


SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);


https://www.mql5.com/en/docs/indicators/icustom

Documentation on MQL5: Technical Indicators / iCustom
Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
iCustom - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
William Roeder #:

ICustom only reads buffers, not arrays.

i want to create a Buffer which has multiple values and pass it to EA using iCustom

// on my zz indicator
double BufferValue[];

// I am setting it as Index on Init
   SetIndexBuffer(3,BufferValue,INDICATOR_DATA);

//On calculate my code is
BufferValue[0]=Ext_1;
//where Ext_1 has multiple values e.g. 1,2,3,4,5,6 etc


//On my EA my code is 
int handle = iCustom(_Symbol,_Period,"Examples/ZigZag",ExtDepth,ExtDeviation,ExtBackstep);
double BufferValue[];
double value_Buffer[]= {0.0};
CopyBuffer(handle,3,0,1,BufferValue);
value_Buffer[0] = NormalizeDouble(BufferValue[0],(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));

//but value_Buffer[0] only returns 1 (the first value and not multiple value as it has
 
Arpit T #:

i want to create a Buffer which has multiple values and pass it to EA using iCustom





        
        
        
        
        
value_Buffer[0] = NormalizeDouble(BufferValue[0],(int)SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));
//but value_Buffer[0] only returns 1 (the first value and not multiple value as it has

It looks like you are accessing the first element only with BufferValue[0]  -you will not get multiple values this way.

Use ArrayPrint to print all values out, or use the debugger to see the contents - if there are multiple values you can write a loop to get them (or ArrayCopy)

Also your declaration of value_buffer creates an array with the capacity of 1 - use ArrayResize if you want to hold many

Reason: