MT5CTP的视频教学可以到网站上关于我们栏目的EA邦的各视频平台里观看。
这是第19课的代码:
双均线交叉_EA_v1.0.mq5
(18.83 KB, 下载次数: 5)
- //+------------------------------------------------------------------+
- //| Copyright 2020, EA邦 |
- //| http://www.eabang.com |
- //+------------------------------------------------------------------+
- #define __MT5CTP__
- // 包含库
- #ifdef __MT5CTP__
- #include <mt5ctp\mt5toctp.mqh>
- #endif
- #property copyright "Copyright 2021, MetaQuotes Ltd."
- #property link "https://www.eabang.com"
- #property version "1.00"
- //--- input parameters
- input int 短均线=5;
- input int 长均线=10;
- input double 开仓量=1.0;
- input int 止盈=100;
- input int 止损=100;
- CTrade m_trade;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //--- create timer
- EventSetTimer(60);
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //--- destroy timer
- EventKillTimer();
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- //---
- double mad_1=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,1);
- double mac_1=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,1);
- double mad_2=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,2);
- double mac_2=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,2);
- if(mad_1>mac_1 && mad_2<mac_2)
- {
- if(ddsl(0)==0) //多单开仓
- {
- bool buyKc=m_trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,开仓量,SymbolInfoDouble(_Symbol,SYMBOL_SESSION_PRICE_LIMIT_MAX),0,0,"均线交叉多单");//开仓多单
- if(buyKc==true)
- {
- Alert("多单开仓成功!");
- }
- else
- {
- Alert("多单开仓失败!"+IntegerToString(GetLastError()));
- }
- }
- if(ddsl(1)>0) //空单平仓
- {
- bool cSell=closeAll(1);
- if(cSell==true)
- {
- Alert("空单平仓成功!");
- }
- else
- {
- Alert("空单平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- if(mad_1<mac_1 && mad_2>mac_2)
- {
- if(ddsl(1)==0) //空单开仓
- {
- bool sellKc=m_trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,开仓量,SymbolInfoDouble(_Symbol,SYMBOL_SESSION_PRICE_LIMIT_MIN),0,0,"均线交叉空单");//开仓空单
- if(sellKc==true)
- {
- Alert("空单开仓成功!");
- }
- else
- {
- Alert("空单开仓失败!"+IntegerToString(GetLastError()));
- }
- }
- if(ddsl(0)>0) //多单平仓
- {
- bool cBuy=closeAll(0);
- if(cBuy==true)
- {
- Alert("多单平仓成功!");
- }
- else
- {
- Alert("多单平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- if(ddsl(0)>0) //多单止盈止损
- {
- if(止盈>0)
- {
- if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-openJ(0)>止盈*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
- {
- bool cBuy=closeAll(0);
- if(cBuy==true)
- {
- Alert("多单止盈平仓成功!");
- }
- else
- {
- Alert("多单止盈平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- if(止损>0)
- {
- if(openJ(0)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>止损*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
- {
- bool cBuy=closeAll(0);
- if(cBuy==true)
- {
- Alert("多单止损平仓成功!");
- }
- else
- {
- Alert("多单止损平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- }
- if(ddsl(1)>0) //空单止盈止损
- {
- if(止盈>0)
- {
- if(openJ(1)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>止盈*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
- {
- bool cSell=closeAll(1);
- if(cSell==true)
- {
- Alert("空单止盈平仓成功!");
- }
- else
- {
- Alert("空单止盈平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- if(止损>0)
- {
- if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-openJ(1)>止损*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
- {
- bool cSell=closeAll(1);
- if(cSell==true)
- {
- Alert("空单止损平仓成功!");
- }
- else
- {
- Alert("空单止损平仓失败!"+IntegerToString(GetLastError()));
- }
- }
- }
- }
- }
- //+------------------------------------------------------------------+
- //| Timer function |
- //+------------------------------------------------------------------+
- void OnTimer()
- {
- //---
- }
- //+------------------------------------------------------------------+
- bool closeAll(int path)
- {
- bool a=true;
- int ddzs=mt5ctp::MT5PositionsTotal();
- for(int i=0; i<ddzs; i++)
- {
- ulong ticket = 0;
- mt5ctp::MT5PositionGetTicket(i,ticket);
- MT5CTPOrders order_mt5;
- ZeroMemory(order_mt5);
- if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
- continue;
- string pos_symbol = ::CharArrayToString(order_mt5.symbol);
- int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);
- if(order_mt5.type==path)
- {
- if(m_trade.PositionClose(ticket,0)==false)
- a=false;
- }
- //Print("编号=",i);
- //Print("品种=",pos_symbol);
- //Print("订单号=",order_mt5.ticket);
- //Print("开仓时间=",order_mt5.time);
- //Print("持仓方向=",order_mt5.type);
- //Print("开仓量=",order_mt5.volume);
- //Print("开仓价=",order_mt5.price,digit_symbol);
- //Print("止损价=",order_mt5.sl,digit_symbol);
- //Print("止盈价=",order_mt5.tp,digit_symbol);
- //Print("盈亏=",order_mt5.profit);
- //Print("魔术码=",order_mt5.magic);
- //Print("注释=",CharArrayToString(order_mt5.comment));
- }
- return(a);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double ma(string sym,ENUM_TIMEFRAMES zhouqi,int zhi,int bias,ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int k) //获取均线
- {
- double buf[];
- ArraySetAsSeries(buf,true);
- int a=iMA(sym,zhouqi,zhi,bias,method,price);
- int copied=CopyBuffer(a,0,0,k+1,buf);
- return(buf[k]);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int ddsl(int path)
- {
- int a=0;
- int ddzs=mt5ctp::MT5PositionsTotal();
- for(int i=0; i<ddzs; i++)
- {
- ulong ticket = 0;
- mt5ctp::MT5PositionGetTicket(i,ticket);
- MT5CTPOrders order_mt5;
- ZeroMemory(order_mt5);
- if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
- continue;
- string pos_symbol = ::CharArrayToString(order_mt5.symbol);
- int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);
- if(order_mt5.type==path)
- {
- a++;
- }
- //Print("编号=",i);
- //Print("品种=",pos_symbol);
- //Print("订单号=",order_mt5.ticket);
- //Print("开仓时间=",order_mt5.time);
- //Print("持仓方向=",order_mt5.type);
- //Print("开仓量=",order_mt5.volume);
- //Print("开仓价=",order_mt5.price,digit_symbol);
- //Print("止损价=",order_mt5.sl,digit_symbol);
- //Print("止盈价=",order_mt5.tp,digit_symbol);
- //Print("盈亏=",order_mt5.profit);
- //Print("魔术码=",order_mt5.magic);
- //Print("注释=",CharArrayToString(order_mt5.comment));
- }
- return(a);
- }
- //+------------------------------------------------------------------+
- double openJ(int path) //获取开仓价
- {
- double a=0;
- int ddzs=mt5ctp::MT5PositionsTotal();
- for(int i=0; i<ddzs; i++)
- {
- ulong ticket = 0;
- mt5ctp::MT5PositionGetTicket(i,ticket);
- MT5CTPOrders order_mt5;
- ZeroMemory(order_mt5);
- if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
- continue;
- string pos_symbol = ::CharArrayToString(order_mt5.symbol);
- int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);
- if(order_mt5.type==path)
- {
- a=order_mt5.price;
- }
- //Print("编号=",i);
- //Print("品种=",pos_symbol);
- //Print("订单号=",order_mt5.ticket);
- //Print("开仓时间=",order_mt5.time);
- //Print("持仓方向=",order_mt5.type);
- //Print("开仓量=",order_mt5.volume);
- //Print("开仓价=",order_mt5.price,digit_symbol);
- //Print("止损价=",order_mt5.sl,digit_symbol);
- //Print("止盈价=",order_mt5.tp,digit_symbol);
- //Print("盈亏=",order_mt5.profit);
- //Print("魔术码=",order_mt5.magic);
- //Print("注释=",CharArrayToString(order_mt5.comment));
- }
- return(a);
- }
- //+------------------------------------------------------------------+
复制代码
|