Trader’s Tech – Writing Your Own EA Part 67 – Arrays

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 66, we wrapped up our discussion of Event Detection and our DetectTradeEvent() function. Our next topic of discuss is Arrays.

An array is a series of variables with a subscript. It allows you to group variables and tick through them using an integer counter variable.

Let’s say you want to see if there are any open orders on a specific group of symbols. You could use a series of variables like Pair1, Pair2, Pair3 and so on to store the symbol names. But then you would have to execute the code individually for each variable in the group to determine whether there is an open order. The code would look like this:

bool IsThereAnOrder()
   {
   int i;
   int TotalOrders = OrdersTotal();

   //Pair1 Loop
   for(i=0;i<TotalOrders;i++)
      {
      OrderSelect(i,SELECT_BY_POS)
      if(OrderSymbol() == Pair1)
         {
         //Do Things Here on Pair1
         break;
         }  //if(OrderSymbol() == Pair1)
      }  //for(i=0;i<TotalOrders;i++)

   //Pair2 Loop
   for(i=0;i<TotalOrders;i++)
      {
      OrderSelect(i,SELECT_BY_POS)
      if(OrderSymbol() == Pair2)
         {
         //Do Things Here on Pair2
         break;
         }  //if(OrderSymbol() == Pair1)
      }  //for(i=0;i<TotalOrders;i++)

   //Pair3 Loop
   for(i=0;i<TotalOrders;i++)
      {
      OrderSelect(i,SELECT_BY_POS)
      if(OrderSymbol() == Pair3)
         {
         //Do Things Here on Pair3
         break;
         }  //if(OrderSymbol() == Pair3)
      }  //for(i=0;i<TotalOrders;i++)

   //Pair4 Loop
   for(i=0;i<TotalOrders;i++)
      {
      OrderSelect(i,SELECT_BY_POS)
      if(OrderSymbol() == Pair4)
         {
         //Do Things Here on Pair4
         break;
         }  //if(OrderSymbol() == Pair4)
      }  //for(i=0;i<TotalOrders;i++)

   } //bool IsThereAnOrder()

 

As you can see, this construct would be pretty tedious. Especially if you were looking through hundreds of items. Each item would require a minimum of nine lines of code plus whatever code would be necessary to do the things on each item you would like to do. Then, imagine having to add items to the list. Or adding a couple of lines of code to each item. Lastly there’s the biggie – imagine having to debug all that mess.

There is a better way. And I sure am glad…. You can use an array to hold your list. Arrays allow you to loop through code you need to execute on all the items. An array element looks like this: Pair[0], Pair[1], Pair[2]…Pair[n]. You can substitute an integer variable for the integer in the brackets. So it’s easy to set up a loop to perform code on each one:

bool IsThereAnOrder()
   {
   int i,j;
   int TotalOrders = OrdersTotal();

   for(j=0;j<TotalPairs;j++)
      {
      //Pair[] Loop
      for(i=0;i<TotalOrders;i++)
         {
         OrderSelect(i,SELECT_BY_POS)
         if(OrderSymbol() == Pair[j])
            {
            //Do Things Here on Pair[j]
            break;
            }  //if(OrderSymbol() == Pair[j])
         }  //for(i=0;i<TotalOrders;i++)
      }  //for(j=0;j<TotalPairs;j++)
   } //bool IsThereAnOrder()

 

That’s all the code required to loop through four or four hundred items. Of course you have to declare and initialize your array and assign a value for the integer variable TotalPairs before you call this particular function. We’ll talk more about that in our next installment.

Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

 

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news