MT5入门到精通之四-EA各接口介绍

2017/4/21
MT5入门到精通之四
一.EA各接口介绍
1.全部选择所有接口

Paste_Image.png
Paste_Image.png

1.EA接口

//+------------------------------------------------------------------+
//|                                                         EA接口.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <..\Include\Common.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
1.EA拖入窗口时候(执行初始化动作)
*/
int OnInit()
  {
//--- create timer
   EventSetTimer(60); //60s执行一次OnTimer()
   MarketBookAdd(Symbol());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
1.将EA移除会触发
2.直接关闭图标也会执行
*/
void OnDeinit(const int reason)
  {
   EventKillTimer(); //消除定时器
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
1.每次tick
*/
void OnTick()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
定时器触发函数,没有tick变化也可以执行(跟单系统)
*/
void OnTimer()
  {
   printf(TimeToString(TimeLocal(),TIME_DATE|TIME_SECONDS));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
不建议使用
1.订单创建的时候会执行多次(7次)
2.订单移除的时候也会执行多次(7次)
*/
void OnTrade()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
推荐用这个(有疑问)
*/
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//服务器返回订单成功
   if(trans.type==TRADE_TRANSACTION_REQUEST)
     {
      if(request.action==TRADE_ACTION_DEAL && request.type==ORDER_TYPE_BUY)
        {
         Alert("开了一张多单");
        }
     }

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
测试历史数据用到
*/
double OnTester()
  {
   double ret=0.0;
   return(ret);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
以下三个接口,策略优化的时候用到
*/
void OnTesterInit()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTesterPass()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTesterDeinit()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
图表操作相关事件
*/
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//鼠标单击事件
   if(id==CHARTEVENT_CLICK)
     {
      Alert("您单击鼠标,位置X="+lparam+"y="+dparam);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
市场深度发生改变时调用,需要事先调用MarketBookAdd()
*/
void OnBookEvent(const string &symbol)
  {
   Alert(symbol+"场深度发生改变");
  }
//+------------------------------------------------------------------+

2.指标接口

//+------------------------------------------------------------------+
//|                                                  Indicator接口.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
推荐用这个,参数多,可以直接使用。
*/
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {

  }
//+------------------------------------------------------------------+

3.脚本接口

//+------------------------------------------------------------------+
//|                                                     Script接口.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
只执行一次
*/
void OnStart()
  {
  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之四-EA各接口介绍)