Problem: Working with Arrays

 

I am designing an EA that requires storing the lowest price ad time of the candlesticks in an array which would later be called and used in other logic.

The problem is that these price and time are not being saved to the array and apear haphazard and incorrect when i print the array. How do I solve this?

//+------------------------------------------------------------------+
//|                                                        Test1.mq5 |
//|                                          Copyright 2023, Usiola. |
//|                                   https://www.trenddaytrader.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Usiola."
#property link      "https://www.trenddaytrader.com"
#property version   "1.00"
#include <Arrays\ArrayObj.mqh>

int barsTotal;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int bars = iBars(_Symbol, PERIOD_CURRENT);
   if (barsTotal != bars)
   {
      barsTotal = bars;
 
      lowestPrice();
      PrintAllValues();
   }   
  }
//+------------------------------------------------------------------+
void UpdateFVGValues(double &arr[], double value)
{
    ArraySetAsSeries(arr, true);
    ArrayResize(arr, ArraySize(arr) + 1);
    arr[0] = value;
}
//+------------------------------------------------------------------+

void UpdateTIMEValues(datetime &arr[], datetime value)
{
    ArraySetAsSeries(arr, true);
    ArrayResize(arr, ArraySize(arr) + 1);
    arr[0] = value;
}

//+------------------------------------------------------------------+
double blockLowPrice1[];
datetime lowTime1[];

int lowestPrice()
   {
      MqlRates rates[];
      ArraySetAsSeries(rates, true);
      int copied = CopyRates(_Symbol, PERIOD_MN1, 0, 50, rates);
      double blockLowPrice = rates[1].low;
      datetime lowTime = rates[1].time;
      NormalizeDouble(blockLowPrice,_Digits);
          if (
               rates[1].low > 0
             ) {
                              // Store values in arrays
                  UpdateFVGValues(blockLowPrice1, blockLowPrice);
                  UpdateTIMEValues(lowTime1, lowTime);
                  
               }
      return 0;
   }
//+------------------------------------------------------------------+
void PrintAllValues()
{
    Print("Printing all values:");  
    PrintArray(blockLowPrice1);
    PrintArrayT(lowTime1);
}
//+------------------------------------------------------------------+

// Function to print array of doubles
void PrintArray(const double &arr[])
{
    for (int i = ArraySize(arr) - 1; i >= 0; i--)
    {
        Print("Index ", i, ": ", arr[i]);
    }
}
//+------------------------------------------------------------------+

// Function to print array of datetime
void PrintArrayT(const datetime &arr[])
{
    for (int i = ArraySize(arr) - 1; i >= 0; i--)
    {
        Print("Index ", i, ": ", arr[i]);
    }
}

//+------------------------------------------------------------------+                  
Files:
 
2024.01.19 21:33:55.627 Core 1 2023.01.03 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.01.03 01:00:00   Index 0: 1765.84
2024.01.19 21:33:55.627 Core 1 2023.01.03 01:00:00   Index 0: 2022.12.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.02.01 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.02.01 01:00:00   Index 1: 1.7e-322
2024.01.19 21:33:55.627 Core 1 2023.02.01 01:00:00   Index 0: 1823.71
2024.01.19 21:33:55.627 Core 1 2023.02.01 01:00:00   Index 1: wrong datetime
2024.01.19 21:33:55.627 Core 1 2023.02.01 01:00:00   Index 0: 2023.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 2: 0.0
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 1: 1.7e-322
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 0: 1804.65
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 2: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 1: wrong datetime
2024.01.19 21:33:55.627 Core 1 2023.03.01 01:00:00   Index 0: 2023.02.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 3: 0.0
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 2: 0.0
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 1: 1.7e-322
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 0: 1809.33
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 3: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 2: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 1: wrong datetime
2024.01.19 21:33:55.627 Core 1 2023.04.03 01:00:00   Index 0: 2023.03.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 4: 0.0
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 3: 0.0
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 2: 0.0
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 1: 1.7e-322
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 0: 1949.63
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 4: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 3: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 2: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 1: wrong datetime
2024.01.19 21:33:55.627 Core 1 2023.05.01 01:00:00   Index 0: 2023.04.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Printing all values:
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 5: 0.0
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 4: 0.0
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 3: 0.0
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 2: 0.0
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 1: 1.7e-322
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 0: 1931.97
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 5: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 4: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 3: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 2: 1970.01.01 00:00:00
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 1: wrong datetime
2024.01.19 21:33:55.627 Core 1 2023.06.01 01:00:00   Index 0: 2023.05.01 00:00:00

 

I have resolved it. I used static arrays instead of dynamic.

Here is the corrected version

//+------------------------------------------------------------------+
//|                                                        Test1.mq5 |
//|                                          Copyright 2023, Usiola. |
//|                                   https://www.trenddaytrader.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Usiola."
#property link      "https://www.trenddaytrader.com"
#property version   "1.00"

#include <Arrays\ArrayDouble.mqh>

int barsTotal;

CArrayDouble blockLowPrices;
datetime lowTimes[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   //---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   int bars = iBars(_Symbol, PERIOD_CURRENT);
   if (barsTotal != bars)
   {
      barsTotal = bars;

      lowestPrice();

      // Print values in blockLowPrices
      PrintBlockLowPrices();
   }
}
//+------------------------------------------------------------------+

void lowestPrice()
{
   MqlRates rates[];
   ArraySetAsSeries(rates, true);
   int copied = CopyRates(_Symbol, PERIOD_MN1, 0, 50, rates);
   
   double blockLowPrice = rates[1].low;
   datetime lowTime = rates[1].time;
   NormalizeDouble(blockLowPrice, _Digits);
   
   if (rates[1].low > 0) 
   {
      // Store values in dynamic arrays
      blockLowPrices.Add(blockLowPrice);
      ArrayResize(lowTimes, ArraySize(lowTimes) + 1);
      lowTimes[ArraySize(lowTimes) - 1] = lowTime;

      // Print values
      Print("Block Low Price: ", blockLowPrice, ", Low Time: ", TimeToString(lowTime));
   }
}

// Function to print values in blockLowPrices
void PrintBlockLowPrices()
{
   Print("Printing values in blockLowPrices:");
   for (int i = 0; i < blockLowPrices.Total(); i++)
   {
      Print("Index ", i, ": ", blockLowPrices[i]);
   }
}
//+------------------------------------------------------------------+