Integrating the OHLC value from Python API to MT5 using MQL5

别等时光非礼了梦想. 提交于 2019-12-22 09:42:39

问题


I have obtained the OHLC values from the iqoption and trying to find out a way to use it with MT5.
Here is how I got the values:

import time
from iqoptionapi.stable_api import IQ_Option
I_want_money=IQ_Option("email","password")
goal="EURUSD"
print("get candles")
print(I_want_money.get_candles(goal,60,111,time.time()))

The above code library is here: iqoptionapi

The line: I_want_money.get_candles(goal,60,111,time.time()) output json as : Output of the command

Now I am getting json in the output so it work like an API, I guess so.

Meanwhile, I try to create a Custom Symbol in MT5 as iqoption. Now I just wanted to add the data of the OHLC from the API to it, so that it will continue fetching data from the Iqoption and display the chart on the chart window for the custom symbol iqoption.

But I am not able to load it in the custom symbol. Kindly, help me.

Edited

This is the code for live streaming data from the iqoption:

from iqoptionapi.stable_api import IQ_Option
import logging
import time
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s')
I_want_money=IQ_Option("email","password")
I_want_money.start_candles_stream("EURUSD")
thread=I_want_money.collect_realtime_candles_thread_start("EURUSD",100)
I_want_money.start_candles_stream("USDTRY")
thread2=I_want_money.collect_realtime_candles_thread_start("USDTRY",100)
time.sleep(3)
#Do some thing
ans=I_want_money.thread_collect_realtime.items()
for k, v in ans:
    print (k, v)
I_want_money.collect_realtime_candles_thread_stop(thread)
I_want_money.stop_candles_stream("EURUSD")
I_want_money.collect_realtime_candles_thread_stop(thread2)
I_want_money.stop_candles_stream("USDTRY")

回答1:


Ok, you need to
1. receive the feed from the broker(I hope you succeeded)
2. write it into a file
** (both - python) **
3. read and parse it
4. add it to the history centre/marketWatch
** (both - mt5) **

  1. So, you receive data as a string after I_want_money.get_candles(goal,60,111,time.time()) this string might be json or json-array.

  2. The important question is of course the path you are going to put the data. An expert in MQL45 can access only two folders (if not applying dll): C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\MY_TERMINAL_ID_IN_HEX_FORMAT\MQL4\Files and C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\Common\Files in the latter case you need to open a file with const int handle=FileOpen(,|*| FILECOMMON);

In order to parse json, you can use jason.mqh https://www.mql5.com/en/code/13663 library (there are few others) but as far as i remember it has a bug: it cannot parse array of objects correctly. In order to overcome that, I would suggest to write each tick at a separate line. And the last, you will recieve data from your python application at random time, and write it into Common or direct folder. The MT5 robot will read it and delete. Just to avoid confusion, it could be better to guarantee that a file has a unique name. Either random (random.randint(1,1000)) or milliseconds from datetime can help.

So far, you have python code:

 receivedString = I_want_money.get_candles(goal,60,111,time.time())  
 filePath = 'C:\Users\MY_NAME_IS_DANIEL_KNIAZ\AppData\Roaming\MetaQuotes\Terminal\MY_TERMINAL_ID_IN_HEX_FORMAT\MQL4\Files\iqoptionfeed'  
 fileName = os.path.join(filePath,"_"+goal+"_"+str(datetime.now())+".txt")   
 file = open(fileName, "w")  
 for string_ in receivedString:  
     file.write(string_)  
 file.close()  

In case you created a thread, each time you receive an answer from the thread you write such a file.

Next, you need that data in MT5. The easiest way is to loop over the existing files, make sure you can read them and read (or give up if you cannot) and delete after reading, then proceed with the data received. The easiest and faster way is to use 0MQ of course, but let us do it without dll's. In order to read the files, you need to setup a timer that can work as fast as possible, and let it go. Since you cannot make a windows app sleeping less then 15.6ms, your timer should sleep this number of time.

string path;
int OnInit()
  {
   EventSetMillisecondTimer(16);    
   path="iqoptionfeed\\*";
  }
void OnDeinit(const int reason) {   EventKillTimer();   }

string _fileName;
long _search_handle;
void OnTimer()
  {
   _search_handle=FileFindFirst(path,_fileName);
   if(_search_handle!=INVALID_HANDLE)
      {
       do
         {
          ResetLastError();
          FileIsExist(_fileName);
          if(GetLastError()!=ERR_FILE_IS_DIRECTORY)
             processFile(path+_fileName);
         }
       while(FileFindNext(_search_handle,_fileName));
       FileFindClose(_search_handle);
      }
  }  

this piece of code loops the folder and processes each file it managed to find. Now reading the file (two functions) and processing the message inside it:

void processFile(const string fileName)
  {
   string message;
   if(ReadFile(fileName,message))
      processMessage(message,fileName);
  }
bool ReadFile(const string fileName,string &result,const bool common=false)
  {
   const int handle = FileOpen(fileName,common?(FILE_COMMON|FILE_READ):FILE_READ);
   if(handle==INVALID_HANDLE)
     {
      printf("%i - failed to find file %s (probably doesnt exist!). error=%d",__LINE__,fileName,GetLastError());
      return(false);
     }
   Read(handle,result);
   FileClose(handle);
   if(!FileDelete(fileName,common?FILE_COMMON:0))
      printf("%i - failed to delete file %s/%d. error=%d",__LINE__,fileName,common,GetLastError());
   return(true);
  }
void Read(const int handle,string &message)
  {
   string text="";
   while(!FileIsEnding(handle) && !IsStopped())
     {
      text=StringConcatenate(text,FileReadString(handle),"\n");
     }
   //printf("%i %s - %s.",__LINE__,__FUNCTION__,text);
   message=text;
  }

And the last but not the least: process the obtained file. As it was suggested above, it has a json formatted tick for each new tick, separated by \r\n.
Our goal is to add it to the symbol. In order to parse json, jason.mqh is an available solution but you can parse it manually of course.

void processMessage(const string message,const string fileName)
  {
   string symbolName=getSymbolFromFileName(fileName);
   if(!SymbolSelect(symbolName,true))
     {
      if(!CustomSymbolCreate(symbolName))
         return;
     }
   string lines[];
   int size=StringSplit(message,(ushort)'\n',lines);
   for(int i=0;i<size;i++)
     {
      if(StringLen(lines[i])==0)
        continue;
      CJAVal jLine(jtUNDEF,NULL);
      jLine.Deserialize(lines[i]);
      MqlTick mql;
      //here I assume that you receive a json file like " { "time":2147483647,"bid":1.16896,"ask":1.16906,"some_other_data":"someOtherDataThatYouMayAlsoUse" } "
      mql.time=(datetime)jLine["time"].ToInt();
      mql.bid=(double)jLine["bid"].ToDbl();
      mql.ask=(double)jLine["ask"].ToDbl();
      ResetLastError();
      if(CustomTicksAdd(symbolName,mql)<0)
          printf("%i %s - failed to upload tick: %s %s %.5f %.5f. error=%d",__LINE__,__FILE__,symbolName,TimeToString(mql.time),mql.bid,mql.ask,GetLastError()); 
     }
  }
string getSymbolFromFileName(const string fileName)
  {
   string elements[];
   int size=StringSplit(fileName,(ushort)'_',elements);
   if(size<2)
      return NULL;
   return elements[1];
  }

Do not forget to add debugging info and request for GetLastError() is for some reason you get errors.

Can this work in a back tester? Of course not. Fist, OnTimer() is not supported in MQL tester. Next, you need some history record in order to make it running. If you do not have any history - Nobody can help you unlees a broker can give it to you; the best idea could be to start collecting and storing it right now, and when the project is ready (maybe another couple months), you will have it ready and be able to test and optimize the strategy with the available dataset. You can apply the collected set into tester (MQL5 is really the next step in algo trading development compared to MQL4), either manually or with something like tickDataSuite and its Csv2Fxt.ex4 file that makes HST binary files that the tester can read and process; anyway that is another question and nobody can tell you if your broker stores their data somewhere to provide it to you.




回答2:


After second-reading what you wrote (and edited) I can see you want:

  • a symbol synchronized with iqoption [ through your proxy / remotely ]
  • The symbol could be used for backtesting
  • The symbol could be used for on-screen live/strategy/indicator run

That implies operations outside strategy/indicator which MT platforms do not allow in an automated manner - you can achieve it manually by providing a data package, parsing it to CSV and importing to custom symbol creator. Well documented here.

Unfortunately, you choose a platform that by-design stands for self-contained strategies and indicators, more for beginners than professionals taking it seriously.

Refer to the link I provided and see for yourself. The official doc states you can create a custom symbol via mql ref, yet even though they state, in the foreword, it allows 3rd party providers - it's not referenced anywhere else and does not show any integration possibilities.


custom indicators

custom symbol properties



来源:https://stackoverflow.com/questions/51061059/integrating-the-ohlc-value-from-python-api-to-mt5-using-mql5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!