返回顶部
  • 发帖数2764
  • 粉丝0

愿您早日建立自己的稳定盈利的交易系统!

  • 最佳新人

    注册账号后积极发帖的会员
  • 活跃会员

    经常参与各类话题的讨论,发帖内容较有主见
  • 热心会员

    经常帮助其他会员答疑
  • 推广达人

    积极宣传本站,为本站带来更多注册会员
  • 宣传达人

    积极宣传本站,为本站带来更多的用户访问量
  • 灌水之王

    经常在论坛发帖,且发帖量较大
  • 突出贡献

    长期对论坛的繁荣而不断努力,或多次提出建设性意见
  • 优秀版主

    活跃且尽责职守的版主
  • EA邦VIP

    EA邦vip会员
  • 论坛元老

    为论坛做出突出贡献的会员

MT5CTP程序化交易开发20:用注释区分订单

[复制链接]
唐老师Lv.9 显示全部楼层 发表于 2021-10-16 17:25:56 |阅读模式 打印 上一主题 下一主题
MT5CTP的视频教学可以到网站上关于我们栏目的EA邦的各视频平台里观看。
这是第20课的代码: 双均线交叉_EA_v1.0.mq5 (19.39 KB, 下载次数: 6)
  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. input string   区分订单="a1";

  20. CTrade    m_trade;
  21. //+------------------------------------------------------------------+
  22. //| Expert initialization function                                   |
  23. //+------------------------------------------------------------------+
  24. int OnInit()
  25.   {
  26. //--- create timer
  27.    EventSetTimer(60);

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

  38.   }
  39. //+------------------------------------------------------------------+
  40. //| Expert tick function                                             |
  41. //+------------------------------------------------------------------+
  42. void OnTick()
  43.   {
  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.       if(ddsl(0)==0) //多单开仓
  52.         {
  53.          bool buyKc=m_trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,开仓量,SymbolInfoDouble(_Symbol,SYMBOL_SESSION_PRICE_LIMIT_MAX),0,0,区分订单);//开仓多单
  54.          if(buyKc==true)
  55.            {
  56.             Alert("多单开仓成功!");
  57.            }
  58.          else
  59.            {
  60.             Alert("多单开仓失败!"+IntegerToString(GetLastError()));
  61.            }
  62.         }

  63.       if(ddsl(1)>0) //空单平仓
  64.         {
  65.          bool cSell=closeAll(1);
  66.          if(cSell==true)
  67.            {
  68.             Alert("空单平仓成功!");
  69.            }
  70.          else
  71.            {
  72.             Alert("空单平仓失败!"+IntegerToString(GetLastError()));
  73.            }
  74.         }
  75.      }

  76.    if(mad_1<mac_1 && mad_2>mac_2)
  77.      {
  78.       if(ddsl(1)==0) //空单开仓
  79.         {
  80.          bool sellKc=m_trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,开仓量,SymbolInfoDouble(_Symbol,SYMBOL_SESSION_PRICE_LIMIT_MIN),0,0,区分订单);//开仓空单
  81.          if(sellKc==true)
  82.            {
  83.             Alert("空单开仓成功!");
  84.            }
  85.          else
  86.            {
  87.             Alert("空单开仓失败!"+IntegerToString(GetLastError()));
  88.            }
  89.         }

  90.       if(ddsl(0)>0) //多单平仓
  91.         {
  92.          bool cBuy=closeAll(0);
  93.          if(cBuy==true)
  94.            {
  95.             Alert("多单平仓成功!");
  96.            }
  97.          else
  98.            {
  99.             Alert("多单平仓失败!"+IntegerToString(GetLastError()));
  100.            }
  101.         }
  102.      }

  103.    if(ddsl(0)>0) //多单止盈止损
  104.      {
  105.       if(止盈>0)
  106.         {
  107.          if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-openJ(0)>止盈*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
  108.            {
  109.             bool cBuy=closeAll(0);
  110.             if(cBuy==true)
  111.               {
  112.                Alert("多单止盈平仓成功!");
  113.               }
  114.             else
  115.               {
  116.                Alert("多单止盈平仓失败!"+IntegerToString(GetLastError()));
  117.               }
  118.            }
  119.         }

  120.       if(止损>0)
  121.         {
  122.          if(openJ(0)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>止损*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
  123.            {
  124.             bool cBuy=closeAll(0);
  125.             if(cBuy==true)
  126.               {
  127.                Alert("多单止损平仓成功!");
  128.               }
  129.             else
  130.               {
  131.                Alert("多单止损平仓失败!"+IntegerToString(GetLastError()));
  132.               }
  133.            }
  134.         }
  135.      }

  136.    if(ddsl(1)>0) //空单止盈止损
  137.      {
  138.       if(止盈>0)
  139.         {
  140.          if(openJ(1)-SymbolInfoDouble(_Symbol,SYMBOL_ASK)>止盈*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
  141.            {
  142.             bool cSell=closeAll(1);
  143.             if(cSell==true)
  144.               {
  145.                Alert("空单止盈平仓成功!");
  146.               }
  147.             else
  148.               {
  149.                Alert("空单止盈平仓失败!"+IntegerToString(GetLastError()));
  150.               }
  151.            }
  152.         }

  153.       if(止损>0)
  154.         {
  155.          if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-openJ(1)>止损*SymbolInfoDouble(_Symbol,SYMBOL_POINT))
  156.            {
  157.             bool cSell=closeAll(1);
  158.             if(cSell==true)
  159.               {
  160.                Alert("空单止损平仓成功!");
  161.               }
  162.             else
  163.               {
  164.                Alert("空单止损平仓失败!"+IntegerToString(GetLastError()));
  165.               }
  166.            }
  167.         }
  168.      }
  169.   }
  170. //+------------------------------------------------------------------+
  171. //| Timer function                                                   |
  172. //+------------------------------------------------------------------+
  173. void OnTimer()
  174.   {
  175. //---

  176.   }
  177. //+------------------------------------------------------------------+
  178. bool closeAll(int path)
  179.   {
  180.    bool a=true;
  181.    int ddzs=mt5ctp::MT5PositionsTotal();
  182.    for(int i=0; i<ddzs; i++)
  183.      {
  184.       ulong ticket = 0;
  185.       mt5ctp::MT5PositionGetTicket(i,ticket);
  186.       MT5CTPOrders order_mt5;
  187.       ZeroMemory(order_mt5);
  188.       if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
  189.          continue;
  190.       string pos_symbol = ::CharArrayToString(order_mt5.symbol);
  191.       int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);

  192.       if(CharArrayToString(order_mt5.comment)==区分订单)
  193.         {
  194.          if(order_mt5.type==path)
  195.            {
  196.             if(m_trade.PositionClose(ticket,0)==false)
  197.                a=false;
  198.            }
  199.         }
  200.       //Print("编号=",i);
  201.       //Print("品种=",pos_symbol);
  202.       //Print("订单号=",order_mt5.ticket);
  203.       //Print("开仓时间=",order_mt5.time);
  204.       //Print("持仓方向=",order_mt5.type);
  205.       //Print("开仓量=",order_mt5.volume);
  206.       //Print("开仓价=",order_mt5.price,digit_symbol);
  207.       //Print("止损价=",order_mt5.sl,digit_symbol);
  208.       //Print("止盈价=",order_mt5.tp,digit_symbol);
  209.       //Print("盈亏=",order_mt5.profit);
  210.       //Print("魔术码=",order_mt5.magic);
  211.       //Print("注释=",CharArrayToString(order_mt5.comment));
  212.      }

  213.    return(a);
  214.   }

  215. //+------------------------------------------------------------------+
  216. //|                                                                  |
  217. //+------------------------------------------------------------------+
  218. double ma(string sym,ENUM_TIMEFRAMES zhouqi,int zhi,int bias,ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int k) //获取均线
  219.   {
  220.    double buf[];
  221.    ArraySetAsSeries(buf,true);
  222.    int a=iMA(sym,zhouqi,zhi,bias,method,price);
  223.    int copied=CopyBuffer(a,0,0,k+1,buf);
  224.    return(buf[k]);
  225.   }
  226. //+------------------------------------------------------------------+
  227. //|                                                                  |
  228. //+------------------------------------------------------------------+
  229. int ddsl(int path)
  230.   {
  231.    int a=0;
  232.    int ddzs=mt5ctp::MT5PositionsTotal();
  233.    for(int i=0; i<ddzs; i++)
  234.      {
  235.       ulong ticket = 0;
  236.       mt5ctp::MT5PositionGetTicket(i,ticket);
  237.       MT5CTPOrders order_mt5;
  238.       ZeroMemory(order_mt5);
  239.       if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
  240.          continue;
  241.       string pos_symbol = ::CharArrayToString(order_mt5.symbol);
  242.       int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);

  243.       if(CharArrayToString(order_mt5.comment)==区分订单)
  244.         {
  245.          if(order_mt5.type==path)
  246.            {
  247.             a++;
  248.            }
  249.         }
  250.       //Print("编号=",i);
  251.       //Print("品种=",pos_symbol);
  252.       //Print("订单号=",order_mt5.ticket);
  253.       //Print("开仓时间=",order_mt5.time);
  254.       //Print("持仓方向=",order_mt5.type);
  255.       //Print("开仓量=",order_mt5.volume);
  256.       //Print("开仓价=",order_mt5.price,digit_symbol);
  257.       //Print("止损价=",order_mt5.sl,digit_symbol);
  258.       //Print("止盈价=",order_mt5.tp,digit_symbol);
  259.       //Print("盈亏=",order_mt5.profit);
  260.       //Print("魔术码=",order_mt5.magic);
  261.       //Print("注释=",CharArrayToString(order_mt5.comment));
  262.      }

  263.    return(a);
  264.   }
  265. //+------------------------------------------------------------------+
  266. double openJ(int path) //获取开仓价
  267.   {
  268.    double a=0;
  269.    int ddzs=mt5ctp::MT5PositionsTotal();
  270.    for(int i=0; i<ddzs; i++)
  271.      {
  272.       ulong ticket = 0;
  273.       mt5ctp::MT5PositionGetTicket(i,ticket);
  274.       MT5CTPOrders order_mt5;
  275.       ZeroMemory(order_mt5);
  276.       if(!mt5ctp::MT5PositionSelectByTicket(ticket,order_mt5))
  277.          continue;
  278.       string pos_symbol = ::CharArrayToString(order_mt5.symbol);
  279.       int digit_symbol = (int)::SymbolInfoInteger(pos_symbol,SYMBOL_DIGITS);

  280.       if(CharArrayToString(order_mt5.comment)==区分订单)
  281.         {
  282.          if(order_mt5.type==path)
  283.            {
  284.             a=order_mt5.price;
  285.            }
  286.         }
  287.       //Print("编号=",i);
  288.       //Print("品种=",pos_symbol);
  289.       //Print("订单号=",order_mt5.ticket);
  290.       //Print("开仓时间=",order_mt5.time);
  291.       //Print("持仓方向=",order_mt5.type);
  292.       //Print("开仓量=",order_mt5.volume);
  293.       //Print("开仓价=",order_mt5.price,digit_symbol);
  294.       //Print("止损价=",order_mt5.sl,digit_symbol);
  295.       //Print("止盈价=",order_mt5.tp,digit_symbol);
  296.       //Print("盈亏=",order_mt5.profit);
  297.       //Print("魔术码=",order_mt5.magic);
  298.       //Print("注释=",CharArrayToString(order_mt5.comment));
  299.      }

  300.    return(a);
  301.   }
  302. //+------------------------------------------------------------------+
复制代码


+10
要有能够持续稳定盈利的交易策略,再进行实盘交易,建议先用历史数据回测和模拟盘进行仔细验证。
回复

使用道具 举报

精彩评论6

liuwei001Lv.1 显示全部楼层 发表于 2021-10-16 18:59:20
为什么第20期的视频找不到呢?是还没上传吗?怎么只有代码呢。。。
+10
回复

使用道具 举报

唐老师Lv.9 显示全部楼层 发表于 2021-10-25 15:15:26
liuwei001 发表于 2021-10-16 18:59
为什么第20期的视频找不到呢?是还没上传吗?怎么只有代码呢。。。

代码先发出来了,视频现在已经更新。
+10
要有能够持续稳定盈利的交易策略,再进行实盘交易,建议先用历史数据回测和模拟盘进行仔细验证。
回复

使用道具 举报

tygkLv.3 显示全部楼层 发表于 2021-10-31 16:08:47
ok
+10
回复

使用道具 举报

tygkLv.3 显示全部楼层 发表于 2021-10-31 19:01:25
本帖最后由 tygk 于 2021-10-31 21:34 编辑

这个讲座很好
+10
回复

使用道具 举报

313and320Lv.1 显示全部楼层 发表于 2021-11-22 21:58:10
唐老师,我不知道怎么联系您,所以只能发在评论里,您的那个砖块图指标我的使用效果很好,但是一段时间后显示期限到了,老师能不能发一份没有期限的?
+10
回复

使用道具 举报

pmdzjpLv.3 显示全部楼层 发表于 2022-1-30 19:18:36
唐老师辛苦了
+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

EA邦和EACTP仅为EA程序化交易软件服务供应商,使用EA工具进行交易,在使用前应该清楚的阅读和浏览软件相关的教程,使用软件是一种自发行为,所引发的一切法律后果,包括用户在使用过程中导致的任何损失均与EA软件开发者无关。
  • 微信

  • 微信公众号

  • 微信视频号

  • Powered by Discuz! X3.5 | Copyright © 2017-2024, Tencent Cloud. | EABANG.COM
  • 和仲科技有限公司| 川公网安备 51019002005489号 | 蜀ICP备17026493号