Please help!! get value from iVolume

 

Hi,

Please help me to correct this code below : 

I try to get value from iVolume but got warning when compile code :

"possible loss of data due to type conversion from 'long' to 'int' volume-test.mq5 29 18


and got error code 4807 when run expert.



input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // type of volume
input string               symbol=" ";                   // symbol 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // timeframe

string name=symbol;
int handle_iVolume;

int OnInit()
  {
//---
   //--- create handle of the indicator iVolume
   handle_iVolume=iVolumes(name,PERIOD_CURRENT,applied_volume);
   
//--- if the handle is not created
   if(handle_iVolume==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
//---
   
   
   double array_iVolume[] ;
   ArraySetAsSeries(array_iVolume,true);   
   
   int start_pos=1,count=5;
   
   if(!iGetArray(handle_iVolume,0,start_pos,count,array_iVolume))
      return;
   
   string text="";
   for(int i=count-1; i>0; i--)
   text=text+IntegerToString(i)+": "+DoubleToString(array_iVolume[i],Digits()+1)+"\n";      

   Comment(text);
      
   
  }
Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Often a necessity occurs to convert one numeric type into another. Not all numeric types can be converted into another. Here is the scheme of...
 

Please, read documentation. https://www.mql5.com/en/docs/series/ivolume

iVolume is not an indicator and do not provide an handle.

I strongely suggesst to use CopyVolume or CopyRates. Be also aware that volume is "long" type and not "integer".

Documentation on MQL5: Timeseries and Indicators Access / iVolume
Documentation on MQL5: Timeseries and Indicators Access / iVolume
  • www.mql5.com
Returns the tick volume of the bar (indicated by the 'shift' parameter) on the corresponding chart. Parameters symbol [in]  The symbol name of...
 
Thank you for your help. Sorry, i type missing word iVolumes  ( is not iVolume). Then i got error when run expert. : " 2024.07.31 16:09:17.076 volume-test (EURUSD,M5) cannot load indicator 'Volumes' [4302]. Please help

 

I made you an example

//+------------------------------------------------------------------+
//|                                                CopyingVolume.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"



long array_iVolume[];

input int bars = 1000;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ArraySetAsSeries(array_iVolume,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      if(CopyTickVolume(Symbol(), Period(), 0, bars, array_iVolume) == -1){
         Print("Somthing went wrong copying the volume");
      }

      Print("The volume of the most recent bar: ", array_iVolume[0]);

      Comment("The volume of the most recent bar: ", array_iVolume[0]); 
      
      Print("Using iVolume: ", iVolume(Symbol(), Period(), 0));
   
  }
//+------------------------------------------------------------------+
 
Conor Mcnamara #:

I made you an example

I got it . Thank you so much