EA邦程序化交易论坛

标题: MT5CTP程序化交易开发12:定义均线交叉 [打印本页]

作者: 唐老师    时间: 2021-9-14 11:30
标题: MT5CTP程序化交易开发12:定义均线交叉
MT5CTP的视频教学可以到网站上关于我们栏目的EA邦的各视频平台里观看。
这是第12课的代码:EA文件代码: 双均线交叉_EA_v1.0.mq5 (8.54 KB, 下载次数: 1)
  1. //+------------------------------------------------------------------+
  2. //|                                      Copyright 2020, EA邦        |
  3. //|                                     http://www.eabang.com        |
  4. //+------------------------------------------------------------------+
  5. #define __MT5CTP__
  6. // 包含库
  7. #ifdef __MT5CTP__
  8. #include <mt5ctp\mt5toctp.mqh>
  9. #endif

  10. #property copyright "Copyright 2021, MetaQuotes Ltd."
  11. #property link      "https://www.eabang.com"
  12. #property version   "1.00"
  13. //--- input parameters
  14. input int      短均线=5;
  15. input int      长均线=10;
  16. input double   开仓量=1.0;
  17. input int      止盈=100;
  18. input int      止损=100;
  19. //+------------------------------------------------------------------+
  20. //| Expert initialization function                                   |
  21. //+------------------------------------------------------------------+
  22. int OnInit()
  23.   {
  24. //--- create timer
  25.    EventSetTimer(60);

  26. //---
  27.    return(INIT_SUCCEEDED);
  28.   }
  29. //+------------------------------------------------------------------+
  30. //| Expert deinitialization function                                 |
  31. //+------------------------------------------------------------------+
  32. void OnDeinit(const int reason)
  33.   {
  34. //--- destroy timer
  35.    EventKillTimer();

  36.   }
  37. //+------------------------------------------------------------------+
  38. //| Expert tick function                                             |
  39. //+------------------------------------------------------------------+
  40. void OnTick()
  41.   {
  42. //---
  43.    if(ddsl(0)==0) //多单
  44.      {
  45.       double mad_1=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,1);
  46.       double mac_1=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,1);
  47.       double mad_2=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,2);
  48.       double mac_2=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,2);
  49.       if(mad_1>mac_1 && mad_2<mac_2)
  50.       {
  51.          //开仓多单
  52.       }
  53.      }
  54.      
  55.    if(ddsl(1)==0) //空单
  56.      {
  57.       double mad_1=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,1);
  58.       double mac_1=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,1);
  59.       double mad_2=ma(_Symbol,PERIOD_CURRENT,短均线,0,MODE_EMA,PRICE_CLOSE,2);
  60.       double mac_2=ma(_Symbol,PERIOD_CURRENT,长均线,0,MODE_EMA,PRICE_CLOSE,2);
  61.       if(mad_1<mac_1 && mad_2>mac_2)
  62.       {
  63.          //开仓空单
  64.       }
  65.      }
  66.   }
  67. //+------------------------------------------------------------------+
  68. //| Timer function                                                   |
  69. //+------------------------------------------------------------------+
  70. void OnTimer()
  71.   {
  72. //---

  73.   }
  74. //+------------------------------------------------------------------+
  75. double ma(string sym,ENUM_TIMEFRAMES zhouqi,int zhi,int bias,ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int k) //获取均线
  76.   {
  77.    double buf[];
  78.    ArraySetAsSeries(buf,true);
  79.    int a=iMA(sym,zhouqi,zhi,bias,method,price);
  80.    int copied=CopyBuffer(a,0,0,k+1,buf);
  81.    return(buf[k]);
  82.   }
  83. //+------------------------------------------------------------------+
  84. //|                                                                  |
  85. //+------------------------------------------------------------------+
  86. int ddsl(int path)
  87.   {
  88.    int a=0;
  89.    int ddzs=mt5ctp::MT5PositionsTotal();
  90.    for(int i=0; i<ddzs; i++)
  91.      {
  92.       ulong ticket = 0;
  93.       mt5ctp::MT5PositionGetTicket(i,ticket);
  94.       MT5CTPOrders order_mt5;
  95.       ZeroMemory(order_mt5);
  96.       if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
  97.          continue;
  98.       string pos_symbol = ::CharArrayToString(order_mt5.symbol);
  99.       int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);

  100.       if(order_mt5.type==path)
  101.         {
  102.          a++;
  103.         }
  104.       //Print("编号=",i);
  105.       //Print("品种=",pos_symbol);
  106.       //Print("订单号=",order_mt5.ticket);
  107.       //Print("开仓时间=",order_mt5.time);
  108.       //Print("持仓方向=",order_mt5.type);
  109.       //Print("开仓量=",order_mt5.volume);
  110.       //Print("开仓价=",order_mt5.price,digit_symbol);
  111.       //Print("止损价=",order_mt5.sl,digit_symbol);
  112.       //Print("止盈价=",order_mt5.tp,digit_symbol);
  113.       //Print("盈亏=",order_mt5.profit);
  114.       //Print("魔术码=",order_mt5.magic);
  115.       //Print("注释=",CharArrayToString(order_mt5.comment));
  116.      }

  117.    return(a);
  118.   }
  119. //+------------------------------------------------------------------+
复制代码







欢迎光临 EA邦程序化交易论坛 (https://www.eabang.com/bbs/) Powered by Discuz! X5.0