Drawing Multiple Objects

 

I am new to Mql5, so excuse me for being stupid, but i have written this code to draw multiple support, resistance lines and it is only drawing one line. The reason i have the code in two separate functions is because i was testing out if DeInit was called to remove the objects.

void GetSupRes(int FurthestBck)
{
   int N = 0;
   int High = 10;
   int Low = 10;
   do
   {
   SetSupRes(High,Low, N);
   High += 10;
   Low += 10;
   }while(Low != FurthestBck);
}
void SetSupRes(int High,int Low, int N)
{
   int HighestCandle;
   int LowestCandle;
   // Create an array for the price data
   MqlRates PriceInfo[];
   //New time array
   // Sort the array from the current candle downwards
   ArraySetAsSeries(PriceInfo,true);
   // Fill the array with the price data
   int Data = CopyRates(_Symbol,_Period,0,100,PriceInfo);  
   //creates an array for price data
   double low[];
   double high[];
   //sorts the array form the current candle downwards
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(high,true);
   //Fills the array with data from candles and copies into low[] // high[]
   CopyHigh(_Symbol,_Period,0,Low,high);
   CopyLow(_Symbol,_Period,0,High,low);
   //Stores the highest and lowest values in the array into a variable   
   LowestCandle = ArrayMinimum(low,0,Low);
   HighestCandle = ArrayMaximum(high,0,High);
   //setting object properties for the line
   ObjectCreate(_Symbol, ("Support "+ N),OBJ_HLINE,0,0,LowestCandle);
   ObjectCreate(_Symbol, ("Resistance" + N),OBJ_HLINE,0,0,HighestCandle);
   //setting object colour for the line
   ObjectSetInteger(0,("Support "+ N),OBJPROP_COLOR,clrGreen);
   ObjectSetInteger(0,("Resistance" + N),OBJPROP_COLOR,clrRed);
   //setting object Width
   ObjectSetInteger(0,("Support "+ N),OBJPROP_WIDTH,3);
   ObjectSetInteger(0,("Resistance" + N),OBJPROP_WIDTH,3);
   //move line to correct Position
   ObjectMove(_Symbol,("Support "+ N),0,0,PriceInfo[LowestCandle].low);
   ObjectMove(_Symbol,("Resistance" + N),0,0,PriceInfo[HighestCandle].high);
   N +=1;
}
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Max Carrington:

I am new to Mql5, so excuse me for being stupid, but i have written this code to draw multiple support, resistance lines and it is only drawing one line. The reason i have the code in two separate functions is because i was testing out if DeInit was called to remove the objects.

in your code N not change cause you set it on top of your code to zero at all

you have to make a for loop and use N++ to make new line with new name

 
Faeze Bakhshayesh:

in your code N not change cause you set it on top of your code to zero at all

you have to make a for loop and use N++ to make new line with new name

void GetSupRes(int FurthestBck)
{
   int N        = 0;
   int High     = 10;
   int Low      = 10;
   for (N; N<=FurthestBck; N++)
    {
       SetSupRes(High,Low, N);
       High += 10;
       Low += 10;
    }
}
 
Max Carrington:

I am new to Mql5, so excuse me for being stupid, but i have written this code to draw multiple support, resistance lines and it is only drawing one line. The reason i have the code in two separate functions is because i was testing out if DeInit was called to remove the objects.

Hi Max Carrington, following Faeze Bakhshayesh, the "N" variable is passed into the "setsupres" function as a value-parameter, that is, "N" would be a local variable within "setsupres" and if you increment it, that new value from "N" won't return to the calling routine, hence next loop calling "setsupres" would go from "N=0" again.  Another way to resolve it is to turn "N" a reference-variable so that whenever the "setsupres" changes "N", it's changing the same "N" variable you formerly started from zero.

For short, adding "&" upfront definition of "N" in the "setsupres" functions should address what Faeze correctly advised:

void SetSupRes(int High,int Low, int &N)

For more information, check MQL5 doco about "function" - "parameterpass"

Reason: