MT5CTP的视频教学可以到网站上关于我们栏目的EA邦的各视频平台里观看。
这是第12课的代码:EA文件代码:
双均线交叉_EA_v1.0.mq5
(8.54 KB, 下载次数: 1)
- //+------------------------------------------------------------------+
- //| 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;
- //+------------------------------------------------------------------+
- //| 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()
- {
- //---
- if(ddsl(0)==0) //多单
- {
- 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(1)==0) //空单
- {
- 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)
- {
- //开仓空单
- }
- }
- }
- //+------------------------------------------------------------------+
- //| Timer function |
- //+------------------------------------------------------------------+
- void OnTimer()
- {
- //---
- }
- //+------------------------------------------------------------------+
- 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);
- }
- //+------------------------------------------------------------------+
复制代码
|