mt4 自动交易开发

运行环境和基础

mt4 脚本的 print 信息, 只会在 log 中显示.

mac , 安装了 wine, 路径在

/Users/bigzhu/Library/PlayOnMac/wineprefix/icmarket_/drive_c/Program Files/MetaTrader 4 IC Markets/MQL4

tail log 来查看交易的结果

想实现在 mac 命令行下运行脚本, 没成功

关键指标

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
 
const int days=4; // 取近4天的
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//  取近几天最高的纪录
    int highest_index=iHighest("XAUUSD",PERIOD_D1,MODE_HIGH,days,1);
    double highest=iHigh("XAUUSD",PERIOD_D1,highest_index); // 
    Print("hightest=",highest);
//  取近几天最低的纪录
    int lowest_index=iLowest("XAUUSD",PERIOD_D1,MODE_LOW,days,1);
    double lowest=iLow("XAUUSD",PERIOD_D1,lowest_index); // 
    Print("lowest=",lowest);
//  ATR
    double atr = iATR("XAUUSD",PERIOD_D1, days, 1);
    Print("ATR=",atr);
//  当前价格 根据买入还是卖出来设定 
    double current_price = MarketInfo("XAUUSD",MODE_ASK);   
    Print("current_price=",current_price);
//  unit 间隔 atr 的 1/4
    double unit = atr /4
    Print("unit=",unit);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                       bigzhu.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- input parameters
const int oper=0; // 1 buy 0 sell
const int days=4; // 取近4天的
const double the_exp=0.15; //
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   trade();

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getReverse(double best,double atr)
  {
   double reverse=0;
   if(oper==1) 
     {
      reverse=best-(atr*2); // 最高点两倍 atr 肯定要反转
        } else {
      reverse=best+(atr*2);
     }
   return reverse;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getStop(double worst,double unit,double reverse)
  {
   double stop=0;
   if(oper==1)
     {
      // 突破最低点一个单位, 则止损
      // 如果反转点更高, 那么用反转点止损
      stop=worst-unit;
      if(reverse>stop)
        {
         stop=reverse;
        };
        } else {

      stop=worst+unit;
      if(reverse<stop)
        {
         stop=reverse;
        }
     }
   return stop;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getOuts(double worst,double atr,double unit,double &out1,double &out2)
  {
//double out1=0;
//double out2=0;
   double tmp=0;
   if(oper==1)
     {
      out1=worst+atr+unit;                  // 最低点+atr+unit
      out2=worst+(atr*2); // 最低点加两倍atr
      if(out1>out2)
        {
         tmp=out1;
         out1=out2;
         out2=tmp;
        }
      // 考虑点差
      out1 = out1-the_exp;
      out2 = out1-the_exp;
        } else {

      out1=worst-atr+unit;                   // 最低点+atr+unit
      out2=worst-(atr*2); // 最低点加两倍atr
      if(out1<out2)
        {
         tmp=out1;
         out1=out2;
         out2=tmp;
        }
      out1 = out1+the_exp;
      out2 = out2+the_exp;
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getStep(double best,double unit,double stop, double current_price, double &step1, double &step2)
  {
   if(oper==1) {
      step1=best-unit+the_exp;
    while(step1>current_price) {
        step1=step1-unit;
      }
      step2=step1-unit;
        if(step1<=stop) {
            step1=0;
            step2=0;
        }

        } else {
            step1=best+unit-the_exp;
        while(step1<current_price) {
            step1=step1+unit
        }
            step2=step1+unit
        if(step1>=stop) {
            step1=0;
            step2=0;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getBest()
  {
   double best=0;
   int index=0;
   if(oper==1)
     {
      index=iHighest("XAUUSD",PERIOD_D1,MODE_HIGH,days,1);
      best=iHigh("XAUUSD",PERIOD_D1,index); // 
        }else {
      index=iLowest("XAUUSD",PERIOD_D1,MODE_LOW,days,1);
      // Comment(lowest_index);
      best=iLow("XAUUSD",PERIOD_D1,index); // 
     }
   return best;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getWorst()
  {
   double wrost=0;
   int index=0;
   if(oper==0)
     {
      index=iHighest("XAUUSD",PERIOD_D1,MODE_HIGH,days,1);
      wrost=iHigh("XAUUSD",PERIOD_D1,index); //
        } else {
      index=iLowest("XAUUSD",PERIOD_D1,MODE_LOW,days,1);
      wrost=iLow("XAUUSD",PERIOD_D1,index); // 
     }

   Comment(wrost);

   return wrost;
  }
// Trade 计算买入点, 止盈止损点
void trade()
  {
   double atr=iATR("XAUUSD",PERIOD_D1,days,1);

   double best=getBest();
   double worst=getWorst();

   double unit= atr/4;
   double out1=0;
   double out2=0;
   getOuts(worst,atr,unit,out1,out2);
   double reverse=getReverse(best,atr);
   double stop = getStop(worst, unit, reverse);
   double current_price = MarketInfo("XAUUSD",MODE_ASK);   

   double step1=0;
   double step2=0;
   getStep(best, unit, stop, current_price, step1,step2);

   Comment(oper, 
   " step1=", step1, 
   " step2=", step2, 
   " stop=", stop,
   " best=", best,
   " worst=", worst
   );
   

//Comment(atr);
  }
//+------------------------------------------------------------------+