simple array use not working

 


Hi I cannot figure out why the array is not working .
I have a function

int start()
{
  int label_count = 0;
  for(int i = 0; i < SymbolsTotal(true); i++)
  {
    string symbol_name = SymbolName(i, True);

    int conditions_met = 0;

    // Condition 1: MA5 crosses over MA8
    double ma5 = iMA(symbol_name, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 1);
    double ma8 = iMA(symbol_name, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 1);

    if (ma5 > ma8) {
      conditions_met++;
    }

    // Condition 2: Stochastics signal line crosses over 50 level
    double stoch = iStochastic(symbol_name,0,5,3,3,MODE_SMA,STO_LOWHIGH,MODE_SIGNAL,1);

    if (stoch > 50) {
      conditions_met++;
    }

    // Condition 3: RSI crosses over 80 level
    double rsi = iRSI(symbol_name, 0, 14, PRICE_CLOSE, 1);

    if (rsi > 50) {
      conditions_met++;
    }

    // Create label if at least one condition is met
    if (conditions_met >= numConditions ) {
      string objn = "lbl_Period_" + IntegerToString(label_count);

      ObjectCreate(ChartID(), objn, OBJ_LABEL, 0, 0, 0);
      
      string txt = symbol_name + " (" + IntegerToString(conditions_met) + ")";
int x = 77 ;
      int h = 2 * FontSize;
      int y = 58 + label_count * h;

      ObjectSetInteger(ChartID(), objn, OBJPROP_FONTSIZE, FontSize);
      ObjectSetInteger(ChartID(), objn, OBJPROP_COLOR, color1);
      ObjectSetString(ChartID(), objn, OBJPROP_TEXT, txt);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XDISTANCE, x);
      ObjectSetInteger(ChartID(), objn, OBJPROP_YDISTANCE, y);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XSIZE, 10 * FontSize);
      ObjectSetInteger(ChartID(), objn, OBJPROP_SELECTABLE, 0);
      label_count++;
    }
  }
  return(0);
}

so it is a simple one which displays   all the symbols from marketwatch nd how many conditions they have met inside () next to them like in the picture . The symbols can be filtered by minimum number of conditions met.



I wanted to modfy the code so the symbols with most condition appear at the top and in descending order . Used array as below , but it does not work. There are no compiler errors and  no symbols displayed at all . What is the mistake . Is there a more easier and simpler way to achieve this ? Thank you All


int start()
  {
   int label_count = 0;
   int conditions_array[];
   string symbol_array[];
   int symbol_index = 0;

   for(int i = 0; i < SymbolsTotal(true); i++)
     {
      string symbol_name = SymbolName(i, True);

      int conditions_met = 0;

      // Condition 1: MA5 crosses over MA8
      double ma5 = iMA(symbol_name, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 1);
      double ma8 = iMA(symbol_name, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 1);

      if(ma5 > ma8)
        {
         conditions_met++;
        }

      // Condition 2: Stochastics signal line crosses over 50 level
      double stoch = iStochastic(symbol_name,0,5,3,3,MODE_SMA,STO_LOWHIGH,MODE_SIGNAL,1);

      if(stoch > 50)
        {
         conditions_met++;
        }

      // Condition 3: RSI crosses over 80 level
      double rsi = iRSI(symbol_name, 0, 14, PRICE_CLOSE, 1);

      if(rsi > 80)
        {
         conditions_met++;
        }

      // Store the conditions met count and symbol name if at least one condition is met
      if(conditions_met >= 1)
        {
         conditions_array[symbol_index] = conditions_met;
         symbol_array[symbol_index] = symbol_name;
         symbol_index++;
        }
     }

// Sort the symbols based on conditions met count
   for(int i = 0; i < symbol_index - 1; i++)
     {
      for(int j = i + 1; j < symbol_index; j++)
        {
         if(conditions_array[i] < conditions_array[j])
           {
            int temp = conditions_array[i];
            conditions_array[i] = conditions_array[j];
            conditions_array[j] = temp;

            string temp_string = symbol_array[i];
            symbol_array[i] = symbol_array[j];
            symbol_array[j] = temp_string;
           }
        }
     }

// Create labels for the sorted symbols
   for(int i = 0; i < symbol_index; i++)
     {
      string objn = "lbl_Period_" + IntegerToString(label_count);

      ObjectCreate(ChartID(), objn, OBJ_LABEL, 0, 0, 0);

      string txt = symbol_array[i] + " (" + IntegerToString(conditions_array[i]) + ")";
      int x = 77 ;
      int h = 2 * FontSize;
      int y = 58 + label_count * h;

      ObjectSetInteger(ChartID(), objn, OBJPROP_FONTSIZE, FontSize);

      ObjectSetInteger(ChartID(), objn, OBJPROP_COLOR, color1);
      ObjectSetString(ChartID(), objn, OBJPROP_TEXT, txt);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XDISTANCE, x);
      ObjectSetInteger(ChartID(), objn, OBJPROP_YDISTANCE, y);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XSIZE, 10 * FontSize);
      ObjectSetInteger(ChartID(), objn, OBJPROP_SELECTABLE, 0);
      label_count++;
      
     }
   return(0);
  }
//+------------------------------------------------------------------+

I have attached  mq4 files , a working file array 23  and nor working file array 45

Files:
array_23.mq4  5 kb
array45.mq4  6 kb
 

Hello try this : (its a script) The addition was the resizing of the 2 arrays and moving the index increase before the passing of data    

//+------------------------------------------------------------------+
//|                                                     array45b.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
#property strict
string name ;
input int      FontSize=10;
input int numConditions = 0 ;
input color    color1  =clrYellow;
int x = 77 ;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string type = "SYMBOLS THAT MEET CONDITIONS" ;

   name=StringConcatenate(type);



// top label
   ObjectCreate("hey",OBJ_LABEL,0,0,0);
   ObjectSet("hey",OBJPROP_CORNER,0);
   ObjectSet("hey",OBJPROP_XDISTANCE,x);
   ObjectSet("hey",OBJPROP_YDISTANCE,12);
   ObjectSetText("hey",name,13,"",clrWhite);


//--- for making the chrt background black
   ChartSetInteger(ChartID(),CHART_SHOW_GRID,0);
   ChartSetInteger(ChartID(),CHART_COLOR_GRID,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_VOLUME,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_BACKGROUND,clrBlack);
   ChartSetInteger(ChartID(),CHART_COLOR_CANDLE_BEAR,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_CANDLE_BULL,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_CHART_LINE,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_CHART_UP,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_FOREGROUND,clrWhite);
   ChartSetInteger(ChartID(),CHART_COLOR_CHART_DOWN,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_BID,clrNONE);
   ChartSetInteger(ChartID(),CHART_COLOR_ASK,clrNONE);
   ChartSetInteger(ChartID(),CHART_MODE,CHART_LINE);
   ChartSetInteger(ChartID(),CHART_SHOW_OHLC,0);
   
   int label_count = 0;
   int conditions_array[];
   string symbol_array[];
   int symbol_index = 0;

   for(int i = 0; i < SymbolsTotal(true); i++)
     {
      string symbol_name = SymbolName(i, True);

      int conditions_met = 0;

      // Condition 1: MA5 crosses over MA8
      double ma5 = iMA(symbol_name, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 1);
      double ma8 = iMA(symbol_name, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 1);

      if(ma5 > ma8)
        {
         conditions_met++;
        }

      // Condition 2: Stochastics signal line crosses over 50 level
      double stoch = iStochastic(symbol_name,0,5,3,3,MODE_SMA,STO_LOWHIGH,MODE_SIGNAL,1);

      if(stoch > 50)
        {
         conditions_met++;
        }

      // Condition 3: RSI crosses over 80 level
      double rsi = iRSI(symbol_name, 0, 14, PRICE_CLOSE, 1);

      if(rsi > 80)
        {
         conditions_met++;
        }

      // Store the conditions met count and symbol name if at least one condition is met
      if(conditions_met >= 1)
        {
         symbol_index++;
         ArrayResize(conditions_array,symbol_index,0);
         ArrayResize(symbol_array,symbol_index,0);
         conditions_array[symbol_index-1] = conditions_met;
         symbol_array[symbol_index-1] = symbol_name;
        }
     }

// Sort the symbols based on conditions met count
   for(int i = 0; i < symbol_index ; i++)
     {
      for(int j = i + 1; j < symbol_index; j++)
        {
         if(conditions_array[i] < conditions_array[j])
           {
            int temp = conditions_array[i];
            conditions_array[i] = conditions_array[j];
            conditions_array[j] = temp;

            string temp_string = symbol_array[i];
            symbol_array[i] = symbol_array[j];
            symbol_array[j] = temp_string;
           }
        }
     }

// Create labels for the sorted symbols
   for(int i = 0; i < symbol_index; i++)
     {
      string objn = "lbl_Period_" + IntegerToString(label_count);

      ObjectCreate(ChartID(), objn, OBJ_LABEL, 0, 0, 0);

      string txt = symbol_array[i] + " (" + IntegerToString(conditions_array[i]) + ")";
      int h = 2 * FontSize;
      int y = 58 + label_count * h;

      ObjectSetInteger(ChartID(), objn, OBJPROP_FONTSIZE, FontSize);

      ObjectSetInteger(ChartID(), objn, OBJPROP_COLOR, color1);
      ObjectSetString(ChartID(), objn, OBJPROP_TEXT, txt);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XDISTANCE, x);
      ObjectSetInteger(ChartID(), objn, OBJPROP_YDISTANCE, y);
      ObjectSetInteger(ChartID(), objn, OBJPROP_XSIZE, 10 * FontSize);
      ObjectSetInteger(ChartID(), objn, OBJPROP_SELECTABLE, 0);
      label_count++;
      
     }      
  }
//+------------------------------------------------------------------+
 
Lorentzos Roussos #:

Hello try this : (its a script) The addition was the resizing of the 2 arrays and moving the index increase before the passing of data    


Hi Lorentzos THANK YOU so much for having a look into this .
I ran it as script it does not work , it only produces symbols that have met 2 conditions only . I have attched the pic below. Some mistake somehwere ?
Thanks again

script output

 
deral5 #:


Hi Lorentzos THANK YOU so much for having a look into this .
I ran it as script it does not work , it only produces symbols that have met 2 conditions only . I have attched the pic below. Some mistake somehwere ?
Thanks again


Yeah similarly on my terminal . 

Were you perhaps

  • using it as an EA 
  • having the arrays predefined 
  • not resetting the array

so each check found an additional condition met and added it ? while it is very rare ? 

I tried a modification which demands for the broker to deliver the indicator values and it returns 2 at most . Unless is something else i've not noticed .

 
  1. deral5: I have a function

    Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2.       string symbol_name = SymbolName(i, True);
          double ma5 = iMA(symbol_name, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 1);

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  3.    int conditions_array[];
       string symbol_array[];
       int symbol_index = 0;
       ⋮
          if(conditions_met >= 1)
            {
             conditions_array[symbol_index] = conditions_met;
             symbol_array[symbol_index] = symbol_name;
             symbol_index++;

    Your arrays have no size. Array exceeded.

    You would know that if you had used strict. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  4. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  3. Your arrays have no size. Array exceeded.

    You would know that if you had used strict. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  4. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

I did a version like this too but it was not getting more than 2 conditions met with all indicators . I think the OPs conditions are rare . I might be wrong of course .

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  3. Your arrays have no size. Array exceeded.

    You would know that if you had used strict. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  4. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

Hi William . Thank you so much for your pointers . Sorry for posting in the wrong section .
Thanks again .
 
Lorentzos Roussos #:

I did a version like this too but it was not getting more than 2 conditions met with all indicators . I think the OPs conditions are rare . I might be wrong of course .

Thank you Lorentzos . It seems like my whole logic was wrong . STrating from the scratch again.