项目作者: rami1942

项目描述 :
MT4-ODBC bridge and some scripts.
高级语言: MQL4
项目地址: git://github.com/rami1942/mt4odbc.git
创建时间: 2017-08-13T13:51:49Z
项目社区:https://github.com/rami1942/mt4odbc

开源协议:GNU Lesser General Public License v3.0

下载


mt4odbc

Yet another MT4-ODBC bridge, and some scripts.

Most of DBMS provides its ODBC driver, you can access any DBMS from your MQL programs. You can write more complex EAs, useful scripts, etc.

Notice these codes are writtern for my personal use and not intended to develop fully products.
I will maintain and fix bugs if I found or I need, but may not be fixed if I think I don’t need.
Yes you can use these codes by your own risk and rewrite for yourself.

WIPWIPWIP

Environment

You need Microsoft Visual C++ 2017 Redistributable x86 (https://go.microsoft.com/fwlink/?LinkId=746571)
MT4 is 32bit application, so you need 32bit ODBC/redistributable. NOT x64 even though OS is 64bit.

Install

  • Copy .\MQL4 to your MT4 data folder. You can open MT4 data folder by File -> Open Datafolder menu.
  • Install ODBC driver
  • Set up ODBC datasource
  • Include odbc.mqh from your MQL scripts.

Sample

See \MQL4\Scripts\export.mq4

Use

include

Init, Connect/ DeInit

  1. int OnInit() {
  2. if (!OdbcInitEnv()) {
  3. return (INIT_FAILED);
  4. }
  5. // In the case of SQLite3,
  6. if (!OdbcConnect(dsName, userName, password)) {
  7. return (INIT_FAILED);
  8. }
  9. return(INIT_SUCCEEDED);
  10. }
  11. void OnDeinit(const int reason) {
  12. OdbcDisconnect();
  13. OdbcDeInitEnv();
  14. }

Insert/ Update / Delete

  1. string sql = StringConcatenate("insert into usdjpy (time, open, high, low, close) values ('",
  2. dt, "',", open, ",", high, ",", low, ",", close, ")"); // Prepare/placefolder is not supported.
  3. if (!OdbcExecute(sql)) { // result is true/false. if you want last insert ID, use select last_insert_rowid() / last_insert_id() (Depends on DBMS)
  4. Print("Insert failed.");
  5. Print(OdbcErrorCode(), OdbcErrorMsg()); // You can get error reason
  6. }

Query

  1. ulong sth = OdbcQuery("select * from foo"); // You can open more than one cursor
  2. if (sth == 0) {
  3. ERROR
  4. }
  5. OdbcBindColInt(sth); // First column as int
  6. OdbcBindColDouble(sth); // Second column as double
  7. OdbcBindColLong(sth); // 3rd column as long
  8. OdbcBindColString(sth); // 4th column as string
  9. while(OdbcFetch(sth)) {
  10. if (OdbcIsNull(sth, 4)) { // Null check. Column no is base 1. Not base 0
  11. string str = OdbcGetColString(sth, 4); // if the column is null, return "" or 0
  12. }
  13. }
  14. OdbCloseStmt(sth); // Free buffer. Don't forget without you want memory leak.

Restrictions

  • Working 2 or more EAs are not tested.
  • date/time is not supported. Use string or int(unix time or something).