MT5入门到精通之十三(EA)

2017/5/1
一.多周期多指标EA
1.效果图

image.png

2.策略原理
开多单
a.当前k线碰到boll线底部
b.30分钟短周期价格大于长周期
c.kdj金叉 (当前大于,前一根小于)

平多单
a.kdj死叉

开空单
a.当前k线碰到boll线顶部
b.30分钟短周期价格小于长周期
c.kdj死叉 (当前小于,前一根大于)

平空单
a.kdj金叉

3.实现

//+------------------------------------------------------------------+
//|                            multiIndicatorAndMultiTimeFrameEA.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 <wz\Trade.mqh>
#include <wz\Kline.mqh>

input int magic=170501;
input double lots=0.1;//开单量
input int slPoint=200;//止损点数
input int tpPoint=200;//止盈点数
input int fast_period=5;//小均线周期
input int slow_period=10;//大均线周期
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int kdj_kPeriod=5;//KDJ K period
input int kdj_dPeriod=3;//KDJ D period
input int kdj_jPeriod=3;//KDJ J period

Trade td;//交易对象
Kline kl;//行情对象
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//为了函数间可以访问数组,声明生全局变量(注意指标里面都做过倒序处理)
//1.定义获取指标数据的数组
//数据索引最大是1,所有只需要获取2个值即可
int count=2;
double m30_fast[];
double m30_slow[];

double h4_fast[];
double h4_slow[];

double m30_bollUp[],m30_bollLow[],m30_bollMid[];
double h4_bollUp[],h4_bollLow[],h4_bollMid[];

double m30_kdj_main[],m30_kdj_signal[];
double h4_kdj_main[],h4_kdj_signal[];

MqlRates m30_rates[];
MqlRates h4_rates[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//2.获取指标值
   kl.MA(m30_fast,count,Symbol(),PERIOD_M30,fast_period,0,MODE_SMA,PRICE_CLOSE);
   kl.MA(m30_slow,count,Symbol(),PERIOD_M30,slow_period,0,MODE_SMA,PRICE_CLOSE);

   kl.MA(h4_fast,count,Symbol(),PERIOD_H4,fast_period,0,MODE_SMA,PRICE_CLOSE);
   kl.MA(h4_slow,count,Symbol(),PERIOD_H4,slow_period,0,MODE_SMA,PRICE_CLOSE);

   kl.Bands(m30_bollUp,m30_bollLow,m30_bollMid,count,Symbol(),PERIOD_M30,boll_period,0,boll_deviations,PRICE_CLOSE);
   kl.Bands(h4_bollUp,h4_bollLow,h4_bollMid,count,Symbol(),PERIOD_H4,boll_period,0,boll_deviations,PRICE_CLOSE);

   kl.Stochastic(m30_kdj_main,m30_kdj_signal,count,Symbol(),PERIOD_M30,kdj_kPeriod,kdj_dPeriod,kdj_jPeriod,MODE_SMA,STO_LOWHIGH);
   kl.Stochastic(h4_kdj_main,h4_kdj_signal,count,Symbol(),PERIOD_H4,kdj_kPeriod,kdj_dPeriod,kdj_jPeriod,MODE_SMA,STO_LOWHIGH);

   kl.getRates(m30_rates,count,Symbol(),PERIOD_M30);
   kl.getRates(h4_rates,count,Symbol(),PERIOD_H4);

//3.条件判断进行开仓和平仓
   if(satifyOpenBuy())
     {
      td.buyPlus(Symbol(),lots,slPoint,tpPoint,Symbol()+"buy",magic);
     }
   if(satifyCloseBuy())
     {
      td.closeAllBuy(Symbol(),magic);
     }
   if(satifyOpenSell())
     {
      td.sellPlus(Symbol(),lots,slPoint,tpPoint,Symbol()+"sell",magic);
     }
   if(satifyCloseSell())
     {
      td.closeAllSell(Symbol(),magic);
     }
  }
//+------------------------------------------------------------------+
bool satifyOpenBuy()
  {
   bool result=false;
   if(h4_rates[0].low<h4_bollLow[0] && m30_fast[0]>m30_slow[0] && m30_kdj_main[0]>m30_kdj_signal[0] && m30_kdj_main[1]<m30_kdj_signal[1])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyCloseBuy()
  {
   bool result=false;
   if(m30_kdj_main[0]<m30_kdj_signal[0] && m30_kdj_main[1]>m30_kdj_signal[1])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyOpenSell()
  {
   bool result=false;
   if(h4_rates[0].high>h4_bollUp[0] && m30_fast[0]<m30_slow[0] && m30_kdj_main[0]<m30_kdj_signal[0] && m30_kdj_main[1]>m30_kdj_signal[1])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyCloseSell()
  {
   bool result=false;
   if(m30_kdj_main[0]>m30_kdj_signal[0] && m30_kdj_main[1]<m30_kdj_signal[1])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+

4.印证(看看会不会按策略正常运行)


image.png

二.亏损加仓马丁格EA
1.效果

image.png

2.基本策略,
a.首次k线第二根突破boll带,开始做同方向第一次开单。
b. 符合加仓条件的,之后先将所有的订单止盈点修改成上一张最新单一样,再开始按倍数下单
3.实现

//+------------------------------------------------------------------+
//|                                                    亏损加仓马丁格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 <wz\Trade.mqh>
#include <wz\Kline.mqh>

input int magic=170501;
input double lots=0.01;//开单量
input int reOpen_IntervalPoint=1000; //亏损加仓间隔点数
input double reOpen_multiple=2;//加仓倍数
input int reOpen_maxTimes=5;//同向最多加仓次数
input int slow_period=10;//大均线周期
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int tpPoint=200;//回调止盈点数
Trade td;
Kline kl;
int count=4;//获取k线数据个数
MqlRates rates[];
double bollUp[],bollLow[],bollMid[];
//1.1一根k线只开一次单
datetime buyTime=0;
datetime sellTime=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   kl.getRates(rates,count);
   kl.Bands(bollUp,bollLow,bollMid,count,Symbol(),0,boll_period,0,boll_deviations,PRICE_CLOSE);

//2.1多单策略
   int buy_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_BUY,magic);
   if(buy_PositionNum==0)
     {
      if(satifyOpenBuy() && buyTime!=rates[0].time)
        {
         int t=td.buyPlus(Symbol(),lots,0,tpPoint,Symbol()+"buy",magic);
         if(t>0){buyTime=rates[0].time;}
        }
     }
//亏损加仓
   else
     {
      double openPrice,openLots,openStopLoss,openTakeProfit;
      datetime openTime;
      td.getNewestPositionOrder(Symbol(),POSITION_TYPE_BUY,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
      //PrintFormat("buynewest_openTakeProfit=%lf",openTakeProfit);
      //修改所有其他持仓单的止盈,修改为最新单的止盈单
      td.mofiyStoplossAndTakeProfit(Symbol(),POSITION_TYPE_BUY,-1,openTakeProfit,magic);
      if(buy_PositionNum<=reOpen_maxTimes && (openPrice-kl.getAsk(Symbol()))>=reOpen_IntervalPoint*Point())
        {
         td.buyPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,tpPoint,Symbol()+"buy"+IntegerToString(buy_PositionNum),magic);
        }
     }

//2.2空单策略
   int sell_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_SELL,magic);
   if(sell_PositionNum==0)
     {
      if(satifyOpenSell() && sellTime!=rates[0].time)
        {
         int t=td.sellPlus(Symbol(),lots,0,tpPoint,Symbol()+"sell",magic);
         if(t>0){sellTime=rates[0].time;}
        }
     }
//加仓
   else
     {
      double openPrice,openLots,openStopLoss,openTakeProfit;
      datetime openTime;
      td.getNewestPositionOrder(Symbol(),POSITION_TYPE_SELL,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
      //修改所有其他持仓单的止盈
      //PrintFormat("sellnewest_openTakeProfit=%lf",openTakeProfit);
      td.mofiyStoplossAndTakeProfit(Symbol(),POSITION_TYPE_SELL,-1,openTakeProfit,magic);
      if(sell_PositionNum<=reOpen_maxTimes && (kl.getBid(Symbol())-openPrice)>=reOpen_IntervalPoint*Point())
        {
         td.sellPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,tpPoint,Symbol()+"sell"+IntegerToString(buy_PositionNum),magic);
        }
     }

  }
//+------------------------------------------------------------------+
bool satifyOpenBuy()
  {
   bool result=false;
   if(rates[1].open>bollLow[1] && rates[1].close>bollLow[1] && rates[2].low<bollLow[2])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyOpenSell()
  {
   bool result=false;
   if(rates[1].open<bollUp[1] && rates[1].close<bollUp[1] && rates[2].high>bollUp[2])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+

三顺势加仓EA
1.效果图


image.png

2.基本原理
a.首次k线第二根突破boll带,开始做同方向第一次开单。
b. 符合顺势加仓条件的,再开始按倍数下单
c.符合平仓条件的 平掉所有同方向的订单。
3.实现

//+------------------------------------------------------------------+
//|                                                       顺势加仓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 <wz\Trade.mqh>
#include <wz\Kline.mqh>

input int magic=170501;
input double lots=0.01;//开单量
input int reOpen_IntervalPoint=1000; //顺势加仓间隔点数
input double reOpen_multiple=2;//加仓倍数
input int reOpen_maxTimes=5;//同向最多加仓次数
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int totalEarning_takeprofit_level=100;//总盈利多少美金全部平仓
Trade td;
Kline kl;
int count=4;//获取k线数据个数
MqlRates rates[];
double bollUp[],bollLow[],bollMid[];
//1.1一根k线只开一次单
datetime buyTime=0;
datetime sellTime=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   kl.getRates(rates,count);
   kl.Bands(bollUp,bollLow,bollMid,count,Symbol(),0,boll_period,0,boll_deviations,PRICE_CLOSE);

//2.1多单策略
   int buy_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_BUY,magic);
   if(buy_PositionNum==0)
     {
      if(satifyOpenBuy() && buyTime!=rates[0].time)
        {
         int t=td.buyPlus(Symbol(),lots,0,0,Symbol()+"buy",magic);
         if(t>0){buyTime=rates[0].time;}
        }
     }
//顺势加仓
   else
     {
      double openPrice,openLots,openStopLoss,openTakeProfit;
      datetime openTime;
      td.getNewestPositionOrder(Symbol(),POSITION_TYPE_BUY,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
      //PrintFormat("buynewest_openTakeProfit=%lf",openTakeProfit);

      if(buy_PositionNum<=reOpen_maxTimes && (kl.getAsk(Symbol())-openPrice)>=reOpen_IntervalPoint*Point())
        {
         td.buyPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,0,Symbol()+"buy"+IntegerToString(buy_PositionNum),magic);
        }
     }

//平仓条件
   if(satifyCloseBuy())
     {
      td.closeAllBuy(Symbol(),magic);
     }

//2.2空单策略
   int sell_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_SELL,magic);
   if(sell_PositionNum==0)
     {
      if(satifyOpenSell() && sellTime!=rates[0].time)
        {
         int t=td.sellPlus(Symbol(),lots,0,0,Symbol()+"sell",magic);
         if(t>0){sellTime=rates[0].time;}
        }
     }
//顺势加仓
   else
     {
      double openPrice,openLots,openStopLoss,openTakeProfit;
      datetime openTime;
      td.getNewestPositionOrder(Symbol(),POSITION_TYPE_SELL,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
      //PrintFormat("sellnewest_openTakeProfit=%lf",openTakeProfit);
      if(sell_PositionNum<=reOpen_maxTimes && (openPrice-kl.getBid(Symbol()))>=reOpen_IntervalPoint*Point())
        {
         td.sellPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,0,Symbol()+"sell"+IntegerToString(buy_PositionNum),magic);
        }
     }
//平仓条件
   if(satifyCloseSell())
     {
      td.closeAllSell(Symbol(),magic);
     }

  }
//+------------------------------------------------------------------+
bool satifyOpenBuy()
  {
   bool result=false;
   if(rates[1].open>bollLow[1] && rates[1].close>bollLow[1] && rates[2].low<bollLow[2])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyOpenSell()
  {
   bool result=false;
   if(rates[1].open<bollUp[1] && rates[1].close<bollUp[1] && rates[2].high>bollUp[2])
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyCloseBuy()
  {
   bool result=false;
   double allBuyProfit=td.profit(Symbol(),POSITION_TYPE_BUY,magic);
   if(allBuyProfit>=totalEarning_takeprofit_level)
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool satifyCloseSell()
  {
   bool result=false;
   double allSellProfit=td.profit(Symbol(),POSITION_TYPE_SELL,magic);
   if(allSellProfit>=totalEarning_takeprofit_level)
     {
      result=true;
     }
   return result;
  }
//+------------------------------------------------------------------+

四.多货币套利交易EA
1.多货币指标实现
1.1效果


image.png

1.2实现

//+------------------------------------------------------------------+
//|                                         multiSymbolIndicator.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
#property indicator_buffers 5
#property indicator_plots   1
//--- plot symbol
#property indicator_label1  "symbol"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrRed,clrYellow,C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0'
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         symbolBuffer1[];
double         symbolBuffer2[];
double         symbolBuffer3[];
double         symbolBuffer4[];
double         symbolColors[];

#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
Trade td;
Kline kl;
input string symbolName="GBPUSD";//货币名称
input int showKlineNum=500; //显示k线个数(设置太多,有时候会加载比较慢)
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,symbolBuffer1,INDICATOR_DATA);
   SetIndexBuffer(1,symbolBuffer2,INDICATOR_DATA);
   SetIndexBuffer(2,symbolBuffer3,INDICATOR_DATA);
   SetIndexBuffer(3,symbolBuffer4,INDICATOR_DATA);
   SetIndexBuffer(4,symbolColors,INDICATOR_COLOR_INDEX);

//1.1序列化
   setGlobalArrayAsSeries();

   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[])
  {
//1.2序列化
   setSystemReturnArrayAsSeries(time,open,high,low,close,tick_volume,volume,spread);

//1.3获取数据 高开低收
   MqlRates rates[];
   int bars=showKlineNum;
   kl.getRates(rates,bars,symbolName,0);
//1.4放大缩小因子
   double scaleFactor=open[1]/rates[1].open;

//1.5 赋值缓存数组
   int limit=(prev_calculated>0) ?(rates_total-prev_calculated+1) : rates_total;
   limit=(limit>showKlineNum)? showKlineNum : limit;
   for(int i=0;i<limit;i++)
     {
      symbolBuffer1[i]=rates[i].high*scaleFactor;
      symbolBuffer2[i]=rates[i].open*scaleFactor;
      symbolBuffer3[i]=rates[i].low*scaleFactor;
      symbolBuffer4[i]=rates[i].close*scaleFactor;
      if(symbolBuffer2[i]>symbolBuffer4[i])
        {
         symbolColors[i]=0;
        }
      else
        {
         symbolColors[i]=1;
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+

void setGlobalArrayAsSeries()
  {
   ArraySetAsSeries(symbolBuffer1,true);
   ArraySetAsSeries(symbolBuffer2,true);
   ArraySetAsSeries(symbolBuffer3,true);
   ArraySetAsSeries(symbolBuffer4,true);
   ArraySetAsSeries(symbolColors,true);
  }
//+------------------------------------------------------------------+
void setSystemReturnArrayAsSeries(
                                  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[])
  {
   ArraySetAsSeries(time,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(tick_volume,true);
   ArraySetAsSeries(volume,true);
   ArraySetAsSeries(spread,true);
  }
//+------------------------------------------------------------------+

2.三货币对套利EA(掌握原理即可,不推荐使用)
2.1不推荐原因
a.(考虑到滑点,基本上是套不到利的。暂时没有事物回滚功能)
b.(注意下单量 0.1*0.1=0.1 不一定正确,需要换算,小数点位数可能很多,)

2.1原理
eurusd*usdjpy=eurjpy
2.2实现

//+------------------------------------------------------------------+
//|                                     threeCurrencyArbitrageEA.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 <wz\Trade.mqh>
#include <wz\Kline.mqh>

input int positiveMagic=170501;
input int negativeMagic=170502;

Trade td;
Kline kl;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
// eurusd*usdjpy=eurjpy
   double eurusd_usdjpy=kl.getBid("EURUSD")*kl.getBid("USDJPY");
   double eurjpy=kl.getBid("EURJPY");
   printf(DoubleToString(eurusd_usdjpy,Digits())+"  "+DoubleToString(eurjpy,Digits()));

//1.套利开仓
   if((eurusd_usdjpy-eurjpy)>=150*Point())
     {
     //1.1大的做空,小的做多 (注意下单量 0.1*0.1=0.1 不一定正确)
      td.sellPlus("EURUSD",0.1,0,0,"EURUSD",positiveMagic);
      td.sellPlus("USDJPY",0.1,0,0,"USDJPY",positiveMagic);
      td.buyPlus("EURJPY",0.1,0,0,"EURJPY",positiveMagic);
     }
//2.套利平仓
   if((eurusd_usdjpy-eurjpy)<=0*Point())
     {
      td.closeAllSell("EURUSD",positiveMagic);
      td.closeAllSell("USDJPY",positiveMagic);
      td.closeAllBuy("EURJPY",positiveMagic);
     }

//1.套利开仓
   if((eurusd_usdjpy-eurjpy)<=150*Point())
     {
     //1.1大的做空,小的做多
      td.buyPlus("EURUSD",0.1,0,0,"EURUSD",negativeMagic);
      td.buyPlus("USDJPY",0.1,0,0,"USDJPY",negativeMagic);
      td.sellPlus("EURJPY",0.1,0,0,"EURJPY",negativeMagic);
     }
//2.套利平仓
   if((eurusd_usdjpy-eurjpy)<=0*Point())
     {
      td.closeAllBuy("EURUSD",negativeMagic);
      td.closeAllBuy("USDJPY",negativeMagic);
      td.closeAllSell("EURJPY",negativeMagic);
     }
  }
//+------------------------------------------------------------------+

五.系统自动生成EA
1.设置步骤


image.png

2.对应的文档位置

image.png

3.自动生产的代码

//+------------------------------------------------------------------+
//|                                               autoGenerateEA.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 <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\SignalAMA.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingFixedPips.mqh>
//--- available money management
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
//--- inputs for expert
input string             Expert_Title                  ="autoGenerateEA"; // Document name
ulong                    Expert_MagicNumber            =19635;            //
bool                     Expert_EveryTick              =false;            //
//--- inputs for main signal
input int                Signal_ThresholdOpen          =10;               // Signal threshold value to open [0...100]
input int                Signal_ThresholdClose         =10;               // Signal threshold value to close [0...100]
input double             Signal_PriceLevel             =0.0;              // Price level to execute a deal
input double             Signal_StopLevel              =50.0;             // Stop Loss level (in points)
input double             Signal_TakeLevel              =50.0;             // Take Profit level (in points)
input int                Signal_Expiration             =4;                // Expiration of pending orders (in bars)
input int                Signal_AMA_PeriodMA           =10;               // Adaptive Moving Average(10,...) Period of averaging
input int                Signal_AMA_PeriodFast         =2;                // Adaptive Moving Average(10,...) Period of fast EMA
input int                Signal_AMA_PeriodSlow         =30;               // Adaptive Moving Average(10,...) Period of slow EMA
input int                Signal_AMA_Shift              =0;                // Adaptive Moving Average(10,...) Time shift
input ENUM_APPLIED_PRICE Signal_AMA_Applied            =PRICE_CLOSE;      // Adaptive Moving Average(10,...) Prices series
input double             Signal_AMA_Weight             =1.0;              // Adaptive Moving Average(10,...) Weight [0...1.0]
//--- inputs for trailing
input int                Trailing_FixedPips_StopLevel  =30;               // Stop Loss trailing level (in points)
input int                Trailing_FixedPips_ProfitLevel=50;               // Take Profit trailing level (in points)
//--- inputs for money
input double             Money_FixLot_Percent          =10.0;             // Percent
input double             Money_FixLot_Lots             =0.1;              // Fixed volume
//+------------------------------------------------------------------+
//| Global expert object                                             |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Initializing expert
   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Creating signal
   CExpertSignal *signal=new CExpertSignal;
   if(signal==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating signal");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//---
   ExtExpert.InitSignal(signal);
   signal.ThresholdOpen(Signal_ThresholdOpen);
   signal.ThresholdClose(Signal_ThresholdClose);
   signal.PriceLevel(Signal_PriceLevel);
   signal.StopLevel(Signal_StopLevel);
   signal.TakeLevel(Signal_TakeLevel);
   signal.Expiration(Signal_Expiration);
//--- Creating filter CSignalAMA
   CSignalAMA *filter0=new CSignalAMA;
   if(filter0==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating filter0");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
   signal.AddFilter(filter0);
//--- Set filter parameters
   filter0.PeriodMA(Signal_AMA_PeriodMA);
   filter0.PeriodFast(Signal_AMA_PeriodFast);
   filter0.PeriodSlow(Signal_AMA_PeriodSlow);
   filter0.Shift(Signal_AMA_Shift);
   filter0.Applied(Signal_AMA_Applied);
   filter0.Weight(Signal_AMA_Weight);
//--- Creation of trailing object
   CTrailingFixedPips *trailing=new CTrailingFixedPips;
   if(trailing==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add trailing to expert (will be deleted automatically))
   if(!ExtExpert.InitTrailing(trailing))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set trailing parameters
   trailing.StopLevel(Trailing_FixedPips_StopLevel);
   trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);
//--- Creation of money object
   CMoneyFixedLot *money=new CMoneyFixedLot;
   if(money==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add money to expert (will be deleted automatically))
   if(!ExtExpert.InitMoney(money))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set money parameters
   money.Percent(Money_FixLot_Percent);
   money.Lots(Money_FixLot_Lots);
//--- Check all trading objects parameters
   if(!ExtExpert.ValidationSettings())
     {
      //--- failed
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Tuning of all necessary indicators
   if(!ExtExpert.InitIndicators())
     {
      //--- failed
      printf(__FUNCTION__+": error initializing indicators");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
   ExtExpert.OnTick();
  }
//+------------------------------------------------------------------+
//| "Trade" event handler function                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
   ExtExpert.OnTrade();
  }
//+------------------------------------------------------------------+
//| "Timer" event handler function                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   ExtExpert.OnTimer();
  }

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容

  • 指标的作用在股市里还是有着重要的位置的,不管是价投还是技投,希望能在恰当的时机买入自己满意的价格,基本也是我自己想...
    雨飘碎归尘阅读 1,450评论 3 1
  • 选择题部分 1.(),只有在发生短路事故时或者在负荷电流较大时,变流器中才会有足够的二次电流作为继电保护跳闸之用。...
    skystarwuwei阅读 12,839评论 0 7
  • 文/金银花 向一束花借一些颜色,光在 光年中绽放; 向一匹马寻一些力量,道路 漫长且不再苍茫; 向一棵树找一些荫凉...
    金银花开时阅读 62评论 0 1
  • 这是我参加的第二轮打卡 第一轮打卡后我休息了一个月,这个月重新回到我们的大雁群,跟着大家一起飞行 1】学习❥Y ①...
    Dianne1阅读 236评论 0 0
  • 再过几天就是“六一儿童节”啦! 那是我们孩子的节日,我会在舞台上和我的同学表演我们的节目---小合唱。我还会上台领...
    奇妙的小豆豆阅读 246评论 2 2