How to import bid/ask from another currency pair? - page 5

 
HosseinKOGO:

This is GBPAUD H4 3.12.2018 whole day! And I did not skip to end this time.

May print function lose some reports when it has too much to print?
I guess another issue could be because ticks of these 3 instruments come out in different milliseconds so when we use start/OnTick function on GBPAUD,  it just does the start function whenever GBPAUD tick comes out. And I guess your code might says to return all of those 3 pairs prices when non of them are 0. If so, it Does return whenever all ask/bid prices of all instruments come out at the same exact time.

Data looks fine...

You're right about the print function... I see many missing lines in my testing too. But when I print everything to file, nothing is missed out.

Here's some information about the code: the time of each GBPAUD tick will be used to get the most recent ticks of the other pairs (up to the same time), so it'll always return something, won't be zero.

I've decided to put the function into a class, which should be tidier. See attached. Place it in your expert folder, together with your EA. Feel free to modify it.

Following sample shows you one way of using it (highlighted lines are essential):

#include "TicksInfo.mqh"
CTicksInfo ticksInfo;

string otherPairs[] = {"GBPUSD","EURUSD"};
string allPairs[] = {};

int tickCount = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ticksInfo.init(otherPairs);
   int numSym = ArraySize(otherPairs)+1;
   ArrayResize(allPairs,numSym);
   allPairs[0] = _Symbol;
   for (int i=1; i<numSym; i++)
      allPairs[i] = otherPairs[i-1];
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ticksInfo.deInit();
   Print ("Total Ticks = ", tickCount);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   tickCount++;
   Print ("=== Now process tick ", tickCount);
   MqlTick myTick;
   
   for (int i=0; i<ArraySize(allPairs); i++)
   {
      if (ticksInfo.getTick(allPairs[i],myTick))
         printTick (allPairs[i],myTick);
      else
         Print (allPairs[i], " Error");
   }
  }
//+------------------------------------------------------------------+

void printTicktoFile(string sym, MqlTick &tick)
{
   int file = FileOpen("LogFile.log",FILE_WRITE|FILE_READ|FILE_TXT);
   FileSeek(file,0,SEEK_END);
   FileWrite(file, sym, " tick number ", tickCount, ", at ", 
          tick.time, " ",
          IntegerToString(tick.time_msc-(tick.time*1000),3,'0'),
          " Ask = ", tick.ask, " Bid = ", tick.bid);
   FileClose(file);
}

void printTick(string sym, MqlTick &tick)
{
   Print (sym, " tick number ", tickCount, ", at ", 
          tick.time, " ",
          IntegerToString(tick.time_msc-(tick.time*1000),3,'0'),
          " Ask = ", tick.ask, " Bid = ", tick.bid);
}

So the key really, is just this line: ticksInfo.getTick(<SYMBOL String>,<MqlTick Struct>). If it returns false, you'll have to check the journal for the error message.

Files:
TicksInfo.mqh  5 kb
 
Seng Joo Thio:

Data looks fine...

You're right about the print function... I see many missing lines in my testing too. But when I print everything to file, nothing is missed out.

Here's some information about the code: the time of each GBPAUD tick will be used to get the most recent ticks of the other pairs (up to the same time), so it'll always return something, won't be zero.

I've decided to put the function into a class, which should be tidier. See attached. Place it in your expert folder, together with your EA. Feel free to modify it.

Following sample shows you one way of using it (highlighted lines are essential):

So the key really, is just this line: ticksInfo.getTick(<SYMBOL String>,<MqlTick Struct>). If it returns false, you'll have to check the journal for the error message.

Thank you for your help,

I've copied all these lines into every proper areas. But since I do not understand the code, please advise me how to put the Ask and Bid of SecondPair into Ask2,Bid2 and the same thing for ThirdPair.
Here is my global area:

#include "TicksInfo.mqh"
CTicksInfo ticksInfo;

extern string SecondPair;
extern string ThirdPair;

string otherPairs[] = {SecondPair,ThirdPair};
string allPairs[] = {};

int tickCount = 0;
.
.
.

And This part returns 2 errors while compiling. What should I do with them?:
'SecondPair' - constant expression required     MyEA!.mq4
'ThirdPair' - constant expression required     MyEA!.mq4

 
HosseinKOGO:

Thank you for your help,

I've copied all these lines into every proper areas. But since I do not understand the code, please advise me how to put the Ask and Bid of SecondPair into Ask2,Bid2 and the same thing for ThirdPair.
Here is my global area:

And This part returns 2 errors while compiling. What should I do with them?:
'SecondPair' - constant expression required     MyEA!.mq4
'ThirdPair' - constant expression required     MyEA!.mq4

Then declare your otherPairs the same way as allPairs. And in OnInit, do this:

ArrayResize(otherPairs,2);
otherPairs[0] = SecondPair;
otherPairs[1] = ThirdPair;

And to assign values into Ask2, Bid2, Ask3, Bid3, do this in OnTick:

   MqlTick myTick;
   
   if (ticksInfo.getTick(SecondPair,myTick))
   {
      Ask2 = myTick.ask;
      Bid2 = myTick.bid;
   }
   else
      Print (SecondPair, " Error");

   if (ticksInfo.getTick(ThirdPair,myTick))
   {
      Ask3 = myTick.ask;
      Bid3 = myTick.bid;
   }
   else
      Print (ThirdPair, " Error");
 
Seng Joo Thio:

Then declare your otherPairs the same way as allPairs. And in OnInit, do this:

And to assign values into Ask2, Bid2, Ask3, Bid3, do this in OnTick:

It gives me 00 again :(
My global area:

#include "TicksInfo.mqh"
CTicksInfo ticksInfo;

extern string SecondPair;
extern string ThirdPair;

string otherPairs[] = {};
string allPairs[] = {};

int tickCount = 0;
.
.
.

OnInit:

   ticksInfo.init(otherPairs);
   int numSym = ArraySize(otherPairs)+1;
   ArrayResize(allPairs,numSym);
   allPairs[0] = _Symbol;
   for (int i=1; i<numSym; i++)
   allPairs[i] = otherPairs[i-1];
   
   ArrayResize(otherPairs,2);
   otherPairs[0] = SecondPair;
   otherPairs[1] = ThirdPair;
.
.
.

OnDeinit:

   ticksInfo.deInit();
   Print ("Total Ticks = ", tickCount);

OnTick:

   tickCount++;
   Print ("=== Now process tick ", tickCount);
   MqlTick myTick;
   
   for (int i=0; i<ArraySize(allPairs); i++)
   {
      if (ticksInfo.getTick(allPairs[i],myTick))
         printTick (allPairs[i],myTick);
      else
         Print (allPairs[i], " Error");
   }
   
   
   if (ticksInfo.getTick(SecondPair,myTick))
   {
      Ask2 = myTick.ask;
      Bid2 = myTick.bid;
   }
   else
      Print (SecondPair, " Error");

   if (ticksInfo.getTick(ThirdPair,myTick))
   {
      Ask3 = myTick.ask;
      Bid3 = myTick.bid;
   }
   else
      Print (ThirdPair, " Error");

Journal:
2019.05.14 17:37:14.686    2018.01.03 23:36:58   MyEA! GBPAUD,H4: GBPCAD Error
2019.05.14 17:37:14.686    2018.01.03 23:36:58   MyEA! GBPAUD,H4: GBPUSD Error
2019.05.14 17:37:14.686    2018.01.03 23:36:58   MyEA! GBPAUD,H4: GBPAUD tick number 221179, at 2018.01.03 23:36:58 000 Ask = 1.72479 Bid = 1.72459
2019.05.14 17:37:14.686    2018.01.03 23:36:58   MyEA! GBPAUD,H4: === Now process tick 221179



Where am I mistaken?
I'm sorry for my zero level skill :D

 
HosseinKOGO:

It gives me 00 again :(
My global area:

OnInit:

OnDeinit:

OnTick:

Where am I mistaken?
I'm sorry for my zero level skill :D

OnInit - the 3 new lines should go before everything. 
 
HosseinKOGO:

I'm sorry for my zero level skill :D

It's all about variables and functions, and the willingness to experiment. 
 
Seng Joo Thio:
OnInit - the 3 new lines should go before everything. 

I did it. But it's very strange: the timer for those two extra pairs does not work...

2019.05.14 17:41:55.469    2018.01.05 11:11:24   MyEA! GBPAUD,H4: GBPCAD tick number 370711, at 1970.01.01 00:00:00 000 Ask = 0.0 Bid = 0.0
2019.05.14 17:41:55.469    2018.01.05 11:11:24   MyEA! GBPAUD,H4: GBPUSD tick number 370711, at 1970.01.01 00:00:00 000 Ask = 0.0 Bid = 0.0
2019.05.14 17:41:55.469    2018.01.05 11:11:24   MyEA! GBPAUD,H4: GBPAUD tick number 370711, at 2018.01.05 11:11:24 000 Ask = 1.72644 Bid = 1.72624
2019.05.14 17:41:55.469    2018.01.05 11:11:24   MyEA! GBPAUD,H4: === Now process tick 370711
 
HosseinKOGO:

I did it. But it's very strange: the timer for those two extra pairs does not work...

Show me the whole ea, after you're done experimenting and still can't figure out why. I can take a look about 8 hours from now :). Now I need to 😴... 😂 
 
  1. Seng Joo Thio: OnInit - the 3 new lines should go before everything. 
    Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
              Corrected

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero the first call.

 
William Roeder:
  1. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
              Corrected

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero the first call.

You're right. No worries though, because the only things we do in OnInit, here, is to initialize arrays for storing names of symbols, and to prepare some file handles for reading our own data files. As to the bids and asks prices (i.e. 'tick' data), we're only accessing them in OnTick, and mostly from our own data files as well, since MT4 strategy tester always return zeros when queried on such data of other symbols (note that this behavior differs from the retrieval of OHLC data, which people are more familiar with).