Multiple instances of EA

 

Hello,

I have a few questions about running EA in MT4:

  1. If I want to run an EA on many currency pairs, do I have to drag-n-drop it on every chart? Is there a simple way to run it on many pairs even if some of them do not have chart displayed on the terminal?
  2. When an EA running on multiple currency pairs (like multi-thread), how to coordinate access to "shared" (or "common") data to avoid conflict? For example, the EA instance running on EURUSD is checking open and pending orders by using the following code segment while another instance running on EURJPY submits a new order during the for loop of first instance.
    int total=OrdersTotal();
      for(int pos=0;pos<total;pos++)
        {
         if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
         // doing something;
        }
    

Account balance, current pending orders and positions, etc are all "common" data shared among multiple instance of EA. How to handle this situation in mql4? Does it have mutex or semaphore?

 
  1. If I want to run an EA on many currency pairs, do I have to drag-n-drop it on every chart?
    Yes unless the EA is written to handle many currencies at same time (you cant backtest this easily in MT4)
  2. Is there a simple way to run it on many pairs even if some of them do not have chart displayed on the terminal?
    The pair has to be in the data window but the pair/s one EA is coded for can be one or many
  3. When an EA running on multiple currency pairs (like multi-thread), how to coordinate access to "shared" (or "common") data to avoid conflict? For example, the EA instance running on EURUSD is checking open and pending orders by using the following code segment while another instance running on EURJPY submits a new order during the for loop of first instance.
    Firstly always count down to zero
    Remember the orders 'collection' is zero-based so total = OrdersTotal()-1
    Check OrderMagicNumber() and OrderSymbol() to track orders

-BB-

Oh and read the Book, see link above, or Search for answers to most newby questions

 
BarrowBoy:
  1. If I want to run an EA on many currency pairs, do I have to drag-n-drop it on every chart?
    Yes unless the EA is written to handle many currencies at same time (you cant backtest this easily in MT4)
  2. Is there a simple way to run it on many pairs even if some of them do not have chart displayed on the terminal?
    The pair has to be in the data window but the pair/s one EA is coded for can be one or many
  3. When an EA running on multiple currency pairs (like multi-thread), how to coordinate access to "shared" (or "common") data to avoid conflict? For example, the EA instance running on EURUSD is checking open and pending orders by using the following code segment while another instance running on EURJPY submits a new order during the for loop of first instance.
    Firstly always count down to zero
    Remember the orders 'collection' is zero-based so total = OrdersTotal()-1
    Check OrderMagicNumber() and OrderSymbol() to track orders

-BB-

Oh and read the Book, see link above, or Search for answers to most newby questions


Thank you for answering my questions.

One drawback of having one EA run over a pre-defined set of symbols is that function start() is called on every tick of the symbol to whose chart this EA is attached. If you happen to attach it to a slow-moving symbol, then you may miss several ticks for the fast-moving symbols (like EURUSD).

OrdersTotal() returns the total number of open and pending orders, it is zero-based. I used for(int pos=0;pos<total;pos++) rather than for(int pos=1;pos<=total;pos++) in my original posting. But this has nothing to do with access common data without conflict.

 

Fusion a terrible EA traded multi-pair symbol about 6 or 8

Was attached to one chart but for every currency you had to have a chart open on your MT4...

 

It's preferred to attach the EA to multiple charts. This is the advice which is most given on this forum and I agree with it. The reason I agree with it is because there's no Tick event within mql4 compared to what I've read within mql5. However, with that said, I've recently created a template for my multi-currency EAs which runs on a single chart. Reason I created this is because I don't like having 6-Charts opened, simple as that.

1st: I created an external string of the currencies I wish to trade as seen below.

extern string iSymb_    ="USD_EUR_GBP_AUD_JPY_CHF_CAD";

2nd: I created an array for the options of pairs which I want on the list. *Be careful with symbol names, Sometimes brokers add stuff at the End but never in Front. Example EURUSDxxx

string iSymbol_Ray[6]={"EURUSD","GBPUSD","AUDUSD","USDJPY","USDCHF","USDCAD"};

3rd: create a loop which goes through the symbol array. Starts with USDCAD to EURUSD in this case. iSymbol_ replaces the standard Symbol(). For all intents and purposes.

for(y=ArraySize(iSymbol_Ray)-1;y>=0;y--){
    //~~~~~~~~~~
    iSymbol_=iSymbol_Ray[y];
    Base_=StringSubstr(iSymbol_,0,3);
    Quot_=StringSubstr(iSymbol_,3,3);
    //~~~~~~~~~~
    if(StringFind(iSymb_,Base_,0)==-1
    || StringFind(iSymb_,Quot_,0)==-1
    ){
        continue;
    }
    //~~~~~~~~~~
    Market_Info(iSymbol_);
}

4th: My marketinfo function which lists the stuff I care about. Flexible enough so that I can pass any Symbol at anytime within the program and get MarketInfo for it.

//~~~~~~~~~~Market_Info-Function:
void Market_Info(string iSym_){
        //~~~~~~~~~~
        iAsk=       MarketInfo(iSym_,MODE_ASK);
        iBid=       MarketInfo(iSym_,MODE_BID);
        Spreads=    MarketInfo(iSym_,MODE_SPREAD);
        Tick_Value= MarketInfo(iSym_,MODE_TICKVALUE);
        Stop_Level= MarketInfo(iSym_,MODE_STOPLEVEL);
        Lots_Steps= MarketInfo(iSym_,MODE_LOTSTEP);
        Broker_Min= MarketInfo(iSym_,MODE_MINLOT);
        Broker_Max= MarketInfo(iSym_,MODE_MAXLOT);
        Freeze_Lv=  MarketInfo(iSym_,MODE_FREEZELEVEL);
        Margin_R=   MarketInfo(iSym_,MODE_MARGINREQUIRED);
        Allowed?=   MarketInfo(iSym_,MODE_TRADEALLOWED);
        Tick_Size=  MarketInfo(iSym_,MODE_TICKSIZE);
        iDigits=    MarketInfo(iSym_,MODE_DIGITS);
        iPoint=     MarketInfo(iSym_,MODE_POINT);
        Tick_Time=  MarketInfo(iSym_,MODE_TIME);
        Sym_Size=   MarketInfo(iSym_,MODE_MARGININIT);
    //~~~~~~~~~~Point2Pip:
    Point2Pip=iPoint;
    if(iDigits==2 || iDigits==3){Pip2Real=100;}
    if(iDigits==4 || iDigits==5){Pip2Real=10000;}
    if(iDigits==3){Point2Pip=0.01;}
    if(iDigits==5){Point2Pip=0.0001;}
    //~~~~~~~~~~
}

So how did I solve the fact that It's attached to EURUSD and if something else is moving fast it might miss ticks? I didn't. My systems are Not that sensitive to price. If EURUSD don't get a tick for 10 mins oh-well. That does not mean theres no way to fix this. One solution is to use Loop & Sleep every second for example. Why don't I use Loops & Sleeping? Well because sometimes it causes glitches.

**Because you asked about this somewhere else, I'll try to touch up on it here. The best way to get around mt4 re-initializing Every variable when you change the time-frame {and a bunch of other stuff which triggers reset} recompiling code another example; is to use either Terminal Variables or Use Loops & Time-Stamps. Terminal variables are Global-Variable-Set(). Example of Loop & Time-Stamp: static variable Last_Order_Was_Loss dropped its value because you restarted your pc. When mt4 comes back up, it'll be necessary to search the history again to check if Last_Order_Was_Loss, but you don't want it going through this loop every-time therefore you time-stamp.

Terminal Variable is also a great way to communicate between EA's and even in some cases with Indicators.

Ps: I'm not a Software Engineer therefore my codes could seem simplistic/noobish/long. Apologize in advance.

 
Ubzen:

It's preferred to attach the EA to multiple charts. This is the advice which is most given on this forum and I agree with it. The reason I agree with it is because there's no Tick event within mql4 compared to what I've read within mql5. However, with that said, I've recently created a template for my multi-currency EAs which runs on a single chart. Reason I created this is because I don't like having 6-Charts opened, simple as that.

1st: I created an external string of the currencies I wish to trade as seen below.

2nd: I created an array for the options of pairs which I want on the list. *Be careful with symbol names, Sometimes brokers add stuff at the End but never in Front. Example EURUSDxxx

3rd: create a loop which goes through the symbol array. Starts with USDCAD to EURUSD in this case. iSymbol_ replaces the standard Symbol(). For all intents and purposes.

4th: My marketinfo function which lists the stuff I care about. Flexible enough so that I can pass any Symbol at anytime within the program and get MarketInfo for it.

So how did I solve the fact that It's attached to EURUSD and if something else is moving fast it might miss ticks? I didn't. My systems are Not that sensitive to price. If EURUSD don't get a tick for 10 mins oh-well. That does not mean theres no way to fix this. One solution is to use Loop & Sleep every second for example. Why don't I use Loops & Sleeping? Well because sometimes it causes glitches.

**Because you asked about this somewhere else, I'll try to touch up on it here. The best way to get around mt4 re-initializing Every variable when you change the time-frame {and a bunch of other stuff which triggers reset} recompiling code another example; is to use either Terminal Variables or Use Loops & Time-Stamps. Terminal variables are Global-Variable-Set(). Example of Loop & Time-Stamp: static variable Last_Order_Was_Loss dropped its value because you restarted your pc. When mt4 comes back up, it'll be necessary to search the history again to check if Last_Order_Was_Loss, but you don't want it going through this loop every-time therefore you time-stamp.

Terminal Variable is also a great way to communicate between EA's and even in some cases with Indicators.

Ps: I'm not a Software Engineer therefore my codes could seem simplistic/noobish/long. Apologize in advance.

Your code is so nice but if you publish the variable type also it is so helpful to beginners. and is it possible to provide any designed complected EA/ Template to us.

Thank u

Reason: