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

2017/5/1
一.多周期多指標(biāo)EA
1.效果圖

image.png

2.策略原理
開多單
a.當(dāng)前k線碰到boll線底部
b.30分鐘短周期價(jià)格大于長周期
c.kdj金叉 (當(dāng)前大于,前一根小于)

平多單
a.kdj死叉

開空單
a.當(dāng)前k線碰到boll線頂部
b.30分鐘短周期價(jià)格小于長周期
c.kdj死叉 (當(dāng)前小于容劳,前一根大于)

平空單
a.kdj金叉

3.實(shí)現(xiàn)

//+------------------------------------------------------------------+
//|                            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;//止損點(diǎn)數(shù)
input int tpPoint=200;//止盈點(diǎn)數(shù)
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;//交易對(duì)象
Kline kl;//行情對(duì)象
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//為了函數(shù)間可以訪問數(shù)組喘沿,聲明生全局變量(注意指標(biāo)里面都做過倒序處理)
//1.定義獲取指標(biāo)數(shù)據(jù)的數(shù)組
//數(shù)據(jù)索引最大是1,所有只需要獲取2個(gè)值即可
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.獲取指標(biāo)值
   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.條件判斷進(jìn)行開倉和平倉
   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.印證(看看會(huì)不會(huì)按策略正常運(yùn)行)


image.png

二.虧損加倉馬丁格EA
1.效果

image.png

2.基本策略竭贩,
a.首次k線第二根突破boll帶蚜印,開始做同方向第一次開單。
b. 符合加倉條件的留量,之后先將所有的訂單止盈點(diǎn)修改成上一張最新單一樣窄赋,再開始按倍數(shù)下單
3.實(shí)現(xiàn)

//+------------------------------------------------------------------+
//|                                                    虧損加倉馬丁格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; //虧損加倉間隔點(diǎn)數(shù)
input double reOpen_multiple=2;//加倉倍數(shù)
input int reOpen_maxTimes=5;//同向最多加倉次數(shù)
input int slow_period=10;//大均線周期
input int boll_period=20;//布林帶周期
input double boll_deviations=2;//布林帶偏差
input int tpPoint=200;//回調(diào)止盈點(diǎn)數(shù)
Trade td;
Kline kl;
int count=4;//獲取k線數(shù)據(jù)個(gè)數(shù)
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;
  }
//+------------------------------------------------------------------+

三順勢(shì)加倉EA
1.效果圖


image.png

2.基本原理
a.首次k線第二根突破boll帶哟冬,開始做同方向第一次開單。
b. 符合順勢(shì)加倉條件的忆绰,再開始按倍數(shù)下單
c.符合平倉條件的 平掉所有同方向的訂單浩峡。
3.實(shí)現(xiàn)

//+------------------------------------------------------------------+
//|                                                       順勢(shì)加倉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; //順勢(shì)加倉間隔點(diǎn)數(shù)
input double reOpen_multiple=2;//加倉倍數(shù)
input int reOpen_maxTimes=5;//同向最多加倉次數(shù)
input int boll_period=20;//布林帶周期
input double boll_deviations=2;//布林帶偏差
input int totalEarning_takeprofit_level=100;//總盈利多少美金全部平倉
Trade td;
Kline kl;
int count=4;//獲取k線數(shù)據(jù)個(gè)數(shù)
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;}
        }
     }
//順勢(shì)加倉
   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;}
        }
     }
//順勢(shì)加倉
   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.多貨幣指標(biāo)實(shí)現(xiàn)
1.1效果


image.png

1.2實(shí)現(xiàn)

//+------------------------------------------------------------------+
//|                                         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線個(gè)數(shù)(設(shè)置太多,有時(shí)候會(huì)加載比較慢)
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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獲取數(shù)據(jù) 高開低收
   MqlRates rates[];
   int bars=showKlineNum;
   kl.getRates(rates,bars,symbolName,0);
//1.4放大縮小因子
   double scaleFactor=open[1]/rates[1].open;

//1.5 賦值緩存數(shù)組
   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.三貨幣對(duì)套利EA(掌握原理即可错敢,不推薦使用)
2.1不推薦原因
a.(考慮到滑點(diǎn)翰灾,基本上是套不到利的。暫時(shí)沒有事物回滾功能)
b.(注意下單量 0.1*0.1=0.1 不一定正確稚茅,需要換算纸淮,小數(shù)點(diǎn)位數(shù)可能很多,)

2.1原理
eurusd*usdjpy=eurjpy
2.2實(shí)現(xiàn)

//+------------------------------------------------------------------+
//|                                     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);
     }
  }
//+------------------------------------------------------------------+

五.系統(tǒng)自動(dòng)生成EA
1.設(shè)置步驟


image.png

2.對(duì)應(yīng)的文檔位置

image.png

3.自動(dòng)生產(chǎn)的代碼

//+------------------------------------------------------------------+
//|                                               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();
  }

如果您發(fā)現(xiàn)本文對(duì)你有所幫助咽块,如果您認(rèn)為其他人也可能受益,請(qǐng)把它分享出去欺税。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末侈沪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子魄衅,更是在濱河造成了極大的恐慌峭竣,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晃虫,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡扣墩,警方通過查閱死者的電腦和手機(jī)哲银,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來呻惕,“玉大人荆责,你說我怎么就攤上這事⊙谴啵” “怎么了做院?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長濒持。 經(jīng)常有香客問我键耕,道長,這世上最難降的妖魔是什么柑营? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任屈雄,我火速辦了婚禮,結(jié)果婚禮上官套,老公的妹妹穿的比我還像新娘酒奶。我一直安慰自己蚁孔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布惋嚎。 她就那樣靜靜地躺著杠氢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪另伍。 梳的紋絲不亂的頭發(fā)上修然,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音质况,去河邊找鬼愕宋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛结榄,可吹牛的內(nèi)容都是我干的中贝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼臼朗,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼邻寿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起视哑,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤绣否,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后挡毅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蒜撮,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年跪呈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了段磨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡耗绿,死狀恐怖苹支,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情误阻,我是刑警寧澤债蜜,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站究反,受9級(jí)特大地震影響寻定,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜奴紧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一特姐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧黍氮,春花似錦唐含、人聲如沸浅浮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滚秩。三九已至,卻和暖如春淮捆,著一層夾襖步出監(jiān)牢的瞬間郁油,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國打工攀痊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留桐腌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓苟径,卻偏偏與公主長得像案站,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子棘街,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 指標(biāo)的作用在股市里還是有著重要的位置的蟆盐,不管是價(jià)投還是技投,希望能在恰當(dāng)?shù)臅r(shí)機(jī)買入自己滿意的價(jià)格遭殉,基本也是我自己想...
    雨飄碎歸塵閱讀 1,468評(píng)論 3 1
  • 選擇題部分 1.(),只有在發(fā)生短路事故時(shí)或者在負(fù)荷電流較大時(shí),變流器中才會(huì)有足夠的二次電流作為繼電保護(hù)跳閘之用石挂。...
    skystarwuwei閱讀 12,905評(píng)論 0 7
  • 文/金銀花 向一束花借一些顏色,光在 光年中綻放险污; 向一匹馬尋一些力量痹愚,道路 漫長且不再蒼茫; 向一棵樹找一些蔭涼...
    金銀花開時(shí)閱讀 64評(píng)論 0 1
  • 這是我參加的第二輪打卡 第一輪打卡后我休息了一個(gè)月罗心,這個(gè)月重新回到我們的大雁群里伯,跟著大家一起飛行 1】學(xué)習(xí)?Y ①...
    Dianne1閱讀 238評(píng)論 0 0
  • 再過幾天就是“六一兒童節(jié)”啦! 那是我們孩子的節(jié)日渤闷,我會(huì)在舞臺(tái)上和我的同學(xué)表演我們的節(jié)目---小合唱。我還會(huì)上臺(tái)領(lǐng)...
    奇妙的小豆豆閱讀 248評(píng)論 2 2