OrdersTotal() on Mulitple Pairs

 

Hi all!


I've just started to dwell into the wonderful world of coding and MQL4, and I've written a simple EA to enter a trade when the RSI(13) crosses either the 20 or the 80 mark.


I coded it such that

If (OrdersTotal()<1)

{

code


But because I'm looking at a range of currency pairs, say for example audusd, usdjpy, gbpusd, if a trade is opened on pairs it will not open on the other pairs.

What I'm trying to achieve though is to not open another trade on a pair that is already open, but to still be able to open trades on the other pairs that I'm not entered.


I hope that was clear enough, and I'm pretty sure there's some way of coding this just that my inexperience in coding and programming is my limiting factor.


Regards,

-Jeremy

 

J

Use something like this to count open orders for a pair

Good Luck

-BB-

int OpenTradesForPair(string strPair)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
      if(OrderType()<=OP_SELL)  
        {
        if(OrderSymbol()==strPair)
         {
        retval++;
        
        //Print("Orders opened : ",retval);
         }
        }
     }

return(retval);
}

Call as

if (OpenTradesForPair("EURUSD")==0)
{
   // got none so open some :)

  }
 

Hey BB

Thanks for the quick reply!


I think I somewhat understand the code.

Just a few questions with it though,

as you said this is for a specific pair, so that means if I wanted to trade across say 12 different pairs I would have to write this code 12 times for each pair and then the appropriate OrderSend() code is that right?

also when you wrote

int OpenTradesForPair(string strPair)

does this mean that strPair is any of the pairs I'm interested in? i.e. if I'm looking at EURUSD, i'll write

int OpenTradesForPair(string EURUSD)

and does this mean OrderSymbol() will recognise it to continue on with the OrderSymbol()==EURUSD?

if that's the case would I be able to instead write something like

for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
      if(OrderType()<=OP_SELL)  
        {
        if(OrderSymbol()==Symbol())
         {
        retval++;
        
        //Print("Orders opened : ",retval);
         }
        }
     }

and I don't understand what it means by OrderType()<=OP_SELL, does that mean I'm only restricted to one direction of trading? because I'm interested in both opening long and short positions, so I'm not sure if writing that line might force the position to only be open one way.


and one small somewhat irrelevant question, I've been reading through a few sample EAs and a lot of them use the letter i to prefix a lot of things e.g. in this code "icnt" which I presume is short for iCount, but I just want to know what's the deal with "i" :D


Thanks a lot for your help BB and anyone else who is willing to put some input into this!

-Jeremy

 

Hey BB


been trying to implement the code you suggested


but one thing that keep coming up is


on the first line

int OpenTradesForPair(string strPair)

it says '(' function definition unexpected referring to the left bracket just before the string command


just can't get my head around it


and was just wondering with that string strPair line


should it be string "strPair"

where strPair is any of the currencies i'm trading on?


I'm not to sure as I've mentioned before this stuff is still very new to me

cheers

-Jeremy

Reason: