Download MetaTrader 5

Using multiple pairs in EA to calculate trading parameter

To add comments, please log in or register
Nick
23
Nick 2014.04.21 03:29 
This is my first time trying to create an EA that uses multiple pairs. I have been having trouble with MetaTrader printing out the correct numbers and I can't figure it out. I have dumbed my code down to find the problem. The variable DiffClose should produce HigherPairClose / LowerPairClose, or the close prices of EURJPY / USDJPY. When I have this commented on the screen DiffClose is equal to something like 1 billion when it should be around 1.38... I am using this EA attached to a EURUSD 1-minute chart on a DEMO account. Also, I tried this on the Strategy Tester and got a zero divide error. My understanding is that Strategy Tester cannot do multiple pair strategies, is this true?
#define MAGIC 978
//--- input parameters
input string   MainPair="EURUSD";
input string   LowerPair="USDJPY";
input string   HigherPair="EURJPY";
input double   Lots=0.1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
//---
   if(Bars(_Symbol,_Period) < 60 && IsTradeAllowed() == false)
   {
      Alert("There are less than 60 bars on the chart, EA terminated!");
      return;
   }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---   
   double MainPairClose = MarketInfo(MainPair,MODE_BID);
   if(MainPairClose == 0)
   {
      Print("MainPairClose = 0");
      return;
   }
   double LowerPairClose = MarketInfo(LowerPair,MODE_BID);
   if(LowerPairClose == 0)
   {
      Print("LowerPairClose = 0");
      return;
   }
   double HigherPairClose = MarketInfo(HigherPair,MODE_BID);
   if(HigherPairClose == 0)
   {
      Print("HigherPairClose == 0");
      return;
   }
   double DiffClose = HigherPairClose / LowerPairClose;  
   
   Comment(StringFormat("MainPair: %G\nLowerPair: %G\nHigherPair: %G\nDiffClose: %d",MainPairClose,LowerPairClose,HigherPairClose,DiffClose));
   
   int ticket;
   bool closeTicket;
   int total = 0;
   for(int i = 0; i < OrdersTotal(); i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false)
         break;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
         total++;
   }
   if(total == 0)
   {
     if(DiffClose > 3)
      {
         ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,NULL,MAGIC,0,clrRed);
         return;
      }
      if(DiffClose < -3)
      {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,NULL,MAGIC,0,clrBlue);
         return;
      }
   }
   if(total >= 1)
   {
      if(DiffClose <= 2.5 && DiffClose >= -2.5)
      {
         for(int i = 0; i < OrdersTotal(); i++)
         {
            if(OrderType() == OP_BUY)
            {
               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
               {
                  if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
                     closeTicket = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrWhite);
               }
               else
                  break;
            }
            if(OrderType() == OP_SELL)
            {
               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true)
               {
                  if(OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
                     closeTicket = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrWhite);
               }
               else
                  break;
            }
         }
      }
   }
   return;
  }
//+------------------------------------------------------------------+
Ian Venner
2584
Ian Venner 2014.04.21 07:20  
1468714724:
This is my first time trying to create an EA that uses multiple pairs. I have been having trouble with MetaTrader printing out the correct numbers and I can't figure it out. I have dumbed my code down to find the problem. The variable DiffClose should produce HigherPairClose / LowerPairClose, or the close prices of EURJPY / USDJPY. When I have this commented on the screen DiffClose is equal to something like 1 billion when it should be around 1.38... I am using this EA attached to a EURUSD 1-minute chart on a DEMO account. Also, I tried this on the Strategy Tester and got a zero divide error. My understanding is that Strategy Tester cannot do multiple pair strategies, is this true?

According to the MT4 build 625 help, Strategy Iester can now do multi currency amongst a whole bunch of other new things the trouble is since the upgrades mt5/mql5 features that are not imlemented in mt4/mql4 are mixed up with the real mt4/mql4 features in the docs. The only way to find out what new features really are implemented in mt4/mql4 is to try it and see.

Alain Verleyen
Moderator
31177
Alain Verleyen 2014.04.21 10:11  
SDC:

According to the MT4 build 625 help, Strategy Iester can now do multi currency amongst a whole bunch of other new things the trouble is since the upgrades mt5/mql5 features that are not imlemented in mt4/mql4 are mixed up with the real mt4/mql4 features in the docs. The only way to find out what new features really are implemented in mt4/mql4 is to try it and see.

MT4 strategy tester can't work with multi-currency. Is it really in the help file, can you indicate where please ?
Ian Venner
2584
Ian Venner 2014.04.21 16:27  
Yes it is in the User Guide from the MT4 toolbar > Help > Help Topics > Client terminal > New Terminal > Strategy Tester.
Alain Verleyen
Moderator
31177
Alain Verleyen 2014.04.21 21:56  
SDC:
Yes it is in the User Guide from the MT4 toolbar > Help > Help Topics > Client terminal > New Terminal > Strategy Tester.
This part of the help file is about MT5 (and was already present in "old" help file).
To add comments, please log in or register