Download MetaTrader 5
To add comments, please log in or register
What are the latest MQL5.community news? The Wall section will tell you!
slavename
64
slavename 2014.08.01 23:19 

When backtesting my EA I am getting the error "incorrect start position 0 for arraysort function", but there's no info on that error.  When I do GetLastError()  I get error 4051 but I don't think it's related (I had that as my last error, but fixed it so think it's residual), but then again, I dont see what could be wrong with my arraysort all of a sudden..

 

double getNearestLoStop()
   {
      double low = iLow(Symbol(),PERIOD_H4, 1);
      double high = iHigh(Symbol(),PERIOD_H4, 1);
      double max = upper(low, high);
      double min = lower(low, high);
      
      //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_DESCEND);
      
      double ret;
      for (int ii = 0; ii < newSize; ii++)
         {
            if (min > lines[ii])
               {
                  ret = lines[ii];
                  break;  
               }
         }
      return(ret);
   }
Keith Watford
Moderator
7963
Keith Watford 2014.08.01 23:39  

This just seems odd

      int newSize = ObjectsTotal()-4;

 

 So there are always 4 objects that you don't refer to?

 

Does this work as expected?

            lines[index] = NormalizeDouble(ObjectGetDouble(0,index,OBJPROP_PRICE),5);

 index is an integer, but a string is expected for the object name

slavename
64
slavename 2014.08.02 00:20  
GumRai:

This just seems odd

 

 So there are always 4 objects that you don't refer to?

 

Does this work as expected?

 index is an integer, but a string is expected for the object name


For

 int newSize = ObjectsTotal()-4;

I have 4 static objects and an dynamic number of other objects hence the total - 4.  


lines[index] = NormalizeDouble(ObjectGetDouble(0,index,OBJPROP_PRICE),5);

Yes, the above is working perfectly, the lines are named 0-n via for loop.  Functionally, everything works, it's only in my backtest I'm having this issue.

whroeder1
13627
whroeder1 2014.08.02 00:55  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You "have 4 static objects and dynamic numbers" Therefor their indexes would be [4 .. N-1] not [0, N-4]. Don't assume. Process all objects and verify it's what you want.
slavename
64
slavename 2014.08.03 01:37  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You "have 4 static objects and dynamic numbers" Therefor their indexes would be [4 .. N-1] not [0, N-4]. Don't assume. Process all objects and verify it's what you want.


I assume you're referring to starting the array at position 4 to N-1 in for statement's parameters below?:

for (int x = 0; x < newSize; x++)
         {
            lines[index] = NormalizeDouble(ObjectGetDouble(0,index,OBJPROP_PRICE),5);
            index++; 
         }

I understand what you mean by processing all objects, but don't know technically how it's possible given that they're not included in the array at all as each element is called by name via a for statement (unless I change their names to 0, 1, 2 and 3 but still don't see the benefit in it?).  The below only determines the size of the array, nothing else relating to elements. 

int newSize = ObjectsTotal()-4;
whroeder1
13627
whroeder1 2014.08.03 11:54  
  1. lines[index] = NormalizeDouble(ObjectGetDouble(0,index,OBJPROP_PRICE),5);
    You are mixing apples and oranges. The index for any object will never, (in general,) match the index of your array. Don't assume.
    int newSize=0;
    for(int iObj = ObjectsTotal() - 1; iObj >= 0; --iObj)
    if(ObjectType("line_object2")!=OBJ_HLINE){ // Must be the right object type.
        string on = ObjectName(iObj);          // Must be the right name pattern.
        if(StringFind(on, PATTERN) >= 0){      // It's what I want.
          // does lines need to be resized here?
          lines[newSize++] = ObjectGetDouble(0,index,OBJPROP_PRICE);
    }  }  }
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
slavename
64
slavename 2014.08.04 00:14  
WHRoeder:
  1. You are mixing apples and oranges. The index for any object will never, (in general,) match the index of your array. Don't assume.
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong


Hi WHRoeder,

1.  I agree that's a more precise way of doing it, thank you, but it's still not exactly clear to me doing it this way. I modified it so it would work in my function, but have an issue with StringFind()'s "pattern".  The documentation is weak on it and there are no threads discussing, so I'm not sure what the substring should be or why it's necessary to find an Object (iObj).  In any event, even with just putting in a NULL to do some testing, it still returns "incorrect start position 0..." for it.  It may go away once StringFind is corrected, but I'm not certain.  As is it correctly pulls the right bars, but gets stuck at the last If.  

for (int iObj = ObjectsTotal() - 1; iObj >= 0; --iObj)
         {
            if(ObjectType(iObj) == OBJ_HLINE)
               {
                   string on = ObjectName(iObj);
                   if(StringFind(on, NULL) >= 0) //NULL as placeholder so it can compile
                     {
                        ArrayResize(lines,newSize);
                        lines[newSize++] = ObjectGetDouble(0,newSize++,OBJPROP_PRICE);
                     }
               }
         }

 2.  Thanks for the warning, I thought it was required for OrderSend but I guess I had read misleading post previously.  Much appreciated!

whroeder1
13627
whroeder1 2014.08.04 13:46  
alyehoud: I'm not sure what the substring should be or why it's necessary to find an Object (iObj).
You have to filter by the pattern. There are other objects on the chart, e.g. tester creates arrows and dotted trend line when you open and close an order. It creates a dash when you set/modify SL/TP. You don't want those.
You know what you named your objects, e.g. line1, line2 or myLine1, myLine2. The pattern would be line or myLine.
slavename
64
slavename 2014.08.04 17:04  
WHRoeder:
alyehoud: I'm not sure what the substring should be or why it's necessary to find an Object (iObj).
You have to filter by the pattern. There are other objects on the chart, e.g. tester creates arrows and dotted trend line when you open and close an order. It creates a dash when you set/modify SL/TP. You don't want those.
You know what you named your objects, e.g. line1, line2 or myLine1, myLine2. The pattern would be line or myLine.


Understood now, thanks.  Using that I made some progress and the good news is that I got it all working as you recommended (see below) and replaced NormalizedDouble throughout my EA.   Problem solved by moving the ArraySort function inside the for loop.

/
To add comments, please log in or register