Download MetaTrader 5
To add comments, please log in or register
They are already making profit using the Market. You can join them!
slavename
64
slavename 2014.07.28 07:29 

I'm having an issue with a for statement, constantly returning 0, even though it shouldn't (by my expectation).  Any help would be appreciated.  I can post entire code if it's not enough to assume everything is correct except something here (for statement checks a variable (min) versus an array's values (lines[]):

Note: when declaring ret, no matter what I set it to, it still returns 0 (e.g., int ret = 999; returns 0 still).

for (int ii = 1; ii < newSize; ii++)
         {
            if (min < lines[0])
               {
                   if (min < lines[ii])
                     {
                        ret = lines[ii];  
                     }
                   else ret = lines[ii-1];
               }
            ret = low;
         }
return(ret);
Keith Watford
Moderator
7906
Keith Watford 2014.07.28 07:50  
for (int ii = 1; ii < newSize; ii++)
         {
            if (min < lines[0])                 //whatever the outcome of this if
               {
                   if (min < lines[ii])         //or this if
                     {
                        ret = lines[ii];  
                     }
                   else ret = lines[ii-1];      //or this else
               }
            ret = low;                          //ret will always be reset to whatever value low equals every pass of the loop
         }
return(ret);
whroeder1
13610
whroeder1 2014.07.28 11:53  
What he said. And if you are just wanting the maximum, why don't you just use:
return ArrayMaximum(lines, newSize, 0);
slavename
64
slavename 2014.07.28 15:19  
GumRai:


Sorry, it should read like this, which resolves the loop issue (actual working copy was correct):

for (int ii = 1; ii < newSize; ii++)
         {
            if (min < lines[0])                 
               {
                   if (min < lines[ii])         
                     {
                        ret = lines[ii];  
                     }
                   else ret = lines[ii-1];      
               }
            else ret = low;                          
         }
return(ret);

 But yes, you're correct, ret should get set = to low each pass, yet when I printed the value of low, and the value of ret, they weren't being set equal.  Even with the corrected code here, still returns 0 no matter what.

 

WHRoeder: I'm comparing a low of a bar to it's nearest S/R line for stoploss calculation.  I did it all hard coded before and now that I'm using arrays to calculate the S/R values, it of course doesn't work, since lines[30] for example doesn't always exist in each chart.

Ian Venner
2583
Ian Venner 2014.07.28 16:43  
alyehoud:

I'm having an issue with a for statement, constantly returning 0, even though it shouldn't (by my expectation). 


What is the point of posting code that has variables no one knows the values of ?

What is the value of newSize ? what is the value of low ?

slavename
64
slavename 2014.07.28 16:46  
SDC:

What is the point of posting code that has variables no one knows the values of ?
Happy to oblige as mentioned in original post, but the point was that it's not a value-related problem but a technical one. :(
int test()
   {
      double close = iClose(Symbol(),PERIOD_H4, 14);
      double open = iOpen(Symbol(),PERIOD_H4, 14);
      double low = iLow(Symbol(),PERIOD_H4, 14);
      double min = lower(open, close);
      
      //Define vertical shift between Stop
      double vShift;
      //.### vs .##### check (Yen pairs)
         if (Digits() == 5)
            {
               vShift = 0.00060 + (MarketInfo(Symbol(),MODE_SPREAD)*Point);
            }
         else vShift = 0.060 + (MarketInfo(Symbol(),MODE_SPREAD)*Point);
      
      //Array variables
      int newSize = ObjectsTotal()-4;
      int index = 0;
      
      //Set array up and resize
      double lines[];
      ArrayResize(lines,newSize);
      
      //Fill array with all chart lines' values
      for (int x = 0; x < newSize; x++)
         {
            lines[index] = NormalizeDouble(ObjectGetDouble(0,index,OBJPROP_PRICE),5);
            index++; 
         }
      ArraySort(lines,WHOLE_ARRAY,0,MODE_ASCEND);
      
      double ret = 100;
      for (int ii = 1; ii < newSize; ii++)
         {
            if (min < lines[0])
               {
                   if (min < lines[ii])
                     {
                        ret = lines[ii];  
                     }
                   else ret = lines[ii-1];
               }
            else ret = low;
         }
      return(ret);
   }
  
  
  
  
  void start()
{
   Print("Ret: ", test());
} 
   double lower(double x, double y)
   {
      double answer;
      
      if (x < y)
         {
            answer = x;
         }
      else answer = y;
      return (answer);
   }
Ian Venner
2583
Ian Venner 2014.07.28 16:56  

It is a value related problem.

int newSize = ObjectsTotal()-4;

The for loop will do nothing if there are less than 6 objects.

for (int ii = 1; ii < newSize; ii++)


Also your function is an integer function and you are trying to return a double low.

slavename
64
slavename 2014.07.28 17:19  
SDC:

It is a value related problem.

The for loop will do nothing if there are less than 6 objects.


Also your function is an integer function and you are trying to return a double low


Changing the function from int to double fixed it.  Thank you, I never ever would have been looking up there for the problem. 

Ian Venner
2583
Ian Venner 2014.07.28 17:32  
alyehoud:


Changing the function from int to double fixed it.  Thank you, I never ever would have been looking up there for the problem. 


Always take notice of the warnings in your compiler.
/
To add comments, please log in or register