设为首页 收藏本站 切换语言

均线趋势交易,带移动止损功能  

| 发表于 2024-1-21 23:59:20 | 显示全部楼层 |复制链接
  1.                  
  2.                                        
  3. #property copyright "dzsq"
  4. #property link      "https://www.mql5.com"
  5. #property version   "1.00"
  6. #property strict
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Security.Cryptography;
  10. using System.Threading.Tasks;
  11. using System;
  12. extern int longMa = 20;   //长均线
  13. extern int shortMa = 10;  //短均线
  14. extern int callBachMa = 30;  //回踩线
  15. extern int stop = 300; //止损
  16. extern int limit = 900; //止盈
  17. extern double lots = 1;  //手数
  18. extern double movestop = 500; //移动止损
  19. extern int magic = 12345;
  20. bool isCrossBuy = false; //是否交叉
  21. bool isCrossSell = false;
  22. //是否会调到支撑线
  23. bool isSellCallback = false;
  24. bool isBuyCallback = false;
  25. //是否开仓
  26. bool isBuyOpened = false;
  27. bool isSellOpened = false;
  28. datetime buytime = 0;
  29. datetime selltime = 0;
  30. //+------------------------------------------------------------------+
  31. //| Expert initialization function                                   |
  32. //+------------------------------------------------------------------+
  33. int OnInit()
  34. {
  35.     //---
  36.     //---
  37.     return (INIT_SUCCEEDED);
  38. }
  39. //+------------------------------------------------------------------+
  40. //| Expert deinitialization function                                 |
  41. //+------------------------------------------------------------------+
  42. void OnDeinit(const int reason)
  43. {
  44.     //---
  45. }
  46. //+------------------------------------------------------------------+
  47. //| Expert tick function                                             |
  48. //+------------------------------------------------------------------+
  49. void OnTick()
  50. {
  51.     //向下取整
  52.     //lots = MathFloor(AccountBalance()/10000);
  53.     //获取所有的均线价格,长期,短期,回调
  54.     double longmaPrice1 = iMA(NULL, 0, longMa, 0, MODE_SMA, PRICE_CLOSE, 1);
  55.     double longmaPrice2 = iMA(NULL, 0, longMa, 0, MODE_SMA, PRICE_CLOSE, 2);
  56.     double shortmaPrice1 = iMA(NULL, 0, shortMa, 0, MODE_SMA, PRICE_CLOSE, 1);
  57.     double shortmaPrice2 = iMA(NULL, 0, shortMa, 0, MODE_SMA, PRICE_CLOSE, 2);
  58.     double callBackPrice = iMA(NULL, 0, callBachMa, 0, MODE_SMA, PRICE_CLOSE, 0);
  59.     //均线交叉多头
  60.     if (shortmaPrice1 > longmaPrice1 && shortmaPrice2 < longmaPrice2)
  61.     {
  62.         isCrossBuy = true;
  63.         isCrossSell = false;
  64.         isSellOpened = false;
  65.         isSellCallback = false;
  66.         closesell(Symbol() + "sell", magic);
  67.     }
  68.     //均线交叉空头
  69.     if (shortmaPrice1 < longmaPrice1 && shortmaPrice2 > longmaPrice2)
  70.     {
  71.         isCrossBuy = false;
  72.         isCrossSell = true;
  73.         isBuyOpened = false;
  74.         isBuyCallback = false;
  75.         closebuy(Symbol() + "buy", magic);
  76.     }
  77.     double lowPrice = Low[0];
  78.     double highPrice = High[0];
  79.     double openPrice = Open[0];
  80.     double closePrice = Close[0];
  81.     //符合做多的条件
  82.     if (!isBuyOpened && isCrossBuy && lowPrice < callBackPrice)
  83.     {
  84.         //time[0]  表示当前时间
  85.         if (buytime != Time[0])
  86.         {   //解决当前K线重复下单的问题
  87.             if (buy(lots, stop, limit, Symbol() + "buy", magic))
  88.             {
  89.                 buytime = Time[0];
  90.                 isBuyOpened = true;
  91.             }
  92.         }
  93.     }
  94.     //符合做空的条件
  95.     if (!isSellOpened && isCrossSell && highPrice > callBackPrice)
  96.     {
  97.         //time[0]  表示当前时间
  98.         if (selltime != Time[0])
  99.         {   //解决当前K线重复下单的问题
  100.             if (sell(lots, stop, limit, Symbol() + "sell", magic))
  101.             {
  102.                 selltime = Time[0];
  103.                 isSellOpened = true;
  104.             }
  105.         }
  106.     }
  107.     yidongzhisun();
  108. }
  109. //下单函数 针对一个单子
  110. int buy(double lots, double stop, double limit, string comm, int buymagic)
  111. {  //手数,止损,止盈,注释,magic
  112.     int ticket = 0;   //订单号
  113.     bool zhaodan = false;  //判断有没有重复下单,其实上面已经写过了,这里不写也没关系
  114.     for (int i = 0; i < OrdersTotal(); i++)
  115.     {
  116.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
  117.         {
  118.             string zhushi = OrderComment();
  119.             int magicNumber = OrderMagicNumber();
  120.             if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && zhushi == comm && magicNumber == buymagic)
  121.             {
  122.                 zhaodan = true;
  123.                 break;
  124.             }
  125.         }
  126.     }
  127.     if (zhaodan == false)
  128.     {
  129.         if (stop != 0 && limit == 0)
  130.         {
  131.             ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, Ask - stop * Point, 0, comm, buymagic, 0, White);
  132.         }
  133.         if (stop == 0 && limit != 0)
  134.             ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, 0, Ask + limit * Point, comm, buymagic, 0, White);
  135.         if (stop == 0 && limit == 0)
  136.             ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, 0, 0, comm, buymagic, 0, White);
  137.         if (stop != 0 && limit != 0)
  138.             ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 50, Ask - stop * Point, Ask + limit * Point, comm, buymagic, 0, White);
  139.     }
  140.     return (ticket);
  141. }
  142. //下单函数 针对一个单子
  143. int sell(double lots, double stop, double limit, string comm, int sellmagic)
  144. {  //手数,止损,止盈,注释,magic
  145.     int ticket = 0;   //订单号
  146.     bool zhaodan = false;  //判断有没有重复下单,其实上面已经写过了,这里不写也没关系
  147.     for (int i = 0; i < OrdersTotal(); i++)
  148.     {
  149.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
  150.         {
  151.             string zhushi = OrderComment();
  152.             int magicNumber = OrderMagicNumber();
  153.             if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && zhushi == comm && magicNumber == sellmagic)
  154.             {
  155.                 zhaodan = true;
  156.                 break;
  157.             }
  158.         }
  159.     }
  160.     if (zhaodan == false)
  161.     {
  162.         if (stop != 0 && limit == 0)
  163.         {
  164.             ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, Bid + stop * Point, 0, comm, sellmagic, 0, White);
  165.         }
  166.         if (stop == 0 && limit != 0)
  167.             ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, 0, Bid - limit * Point, comm, sellmagic, 0, White);
  168.         if (stop == 0 && limit == 0)
  169.             ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, 0, 0, comm, sellmagic, 0, White);
  170.         if (stop != 0 && limit != 0)
  171.             ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 50, Bid + stop * Point, Bid - limit * Point, comm, sellmagic, 0, White);
  172.     }
  173.     return (ticket);
  174. }
  175. // 平货币
  176. void closebuy(string comm, int magic)
  177. {
  178.     for (int i = OrdersTotal() - 1; i >= 0; i--)
  179.     {
  180.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
  181.         {
  182.             if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderComment() == comm && OrderMagicNumber() == magic)
  183.             {
  184.                 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 300, Green);
  185.             }
  186.         }
  187.     }
  188. }
  189. // 平货币
  190. void closesell(string comm, int magic)
  191. {
  192.     for (int i = OrdersTotal() - 1; i >= 0; i--)
  193.     {
  194.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
  195.         {
  196.             if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderComment() == comm && OrderMagicNumber() == magic)
  197.             {
  198.                 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 300, Green);
  199.             }
  200.         }
  201.     }
  202. }
  203. //设置移动止损
  204. void yidongzhisun()
  205. {
  206.     for (int i = 0; i < OrdersTotal(); i++)
  207.     {
  208.         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
  209.         {
  210.             if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == magic)
  211.             {
  212.                 if ((Bid - OrderOpenPrice()) >= Point * movestop)
  213.                 {
  214.                     if (OrderStopLoss() < (Bid - Point * movestop) || OrderStopLoss() == 0)
  215.                     {
  216.                         OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * movestop, OrderTakeProfit(), 0, Green);
  217.                     }
  218.                 }
  219.             }
  220.             if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == magic)
  221.             {
  222.                 if ((OrderOpenPrice() - Ask) >= Point * movestop)
  223.                 {
  224.                     if (OrderStopLoss() < (Ask + Point * movestop) || OrderStopLoss() == 0)
  225.                     {
  226.                         OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * movestop, OrderTakeProfit(), 0, Green);
  227.                     }
  228.                 }
  229.             }
  230.         }
  231.     }
  232. }
  233. //+------------------------------------------------------------------+
复制代码
11.png
22.png
举报

评论 使用道具

热门主题

精彩评论20

ken138888
B
| 发表于 2024-1-23 20:10:05 | 显示全部楼层
慢慢学习
举报

点赞 评论 使用道具

ken138888
B
| 发表于 2024-1-25 13:10:42 | 显示全部楼层
Mq5的应该怎样写
举报

点赞 评论 使用道具

hwensen
DD
| 发表于 2024-1-30 08:54:22 | 显示全部楼层
看不懂1111
举报

点赞 评论 使用道具

石头1968
DD
| 发表于 2024-1-30 13:41:56 | 显示全部楼层
均线趋势交易,带移动止损功能,。很不错的源代码。感谢分享!
举报

点赞 评论 使用道具

静观明
DD
| 发表于 2024-2-2 14:38:59 | 显示全部楼层
很不错的源代码。感谢分享
举报

点赞 评论 使用道具

甘草糖
DD
| 发表于 2024-2-8 22:53:19 | 显示全部楼层
很不错的源码,谢谢楼主的慷慨分享。
举报

点赞 评论 使用道具

shllwk
DDD
| 发表于 2024-2-9 22:35:16 | 显示全部楼层
很不错的源代码。感谢分享!
举报

点赞 评论 使用道具

ken138888
B
| 发表于 2024-2-10 13:03:02 | 显示全部楼层
有空我也来写一下
举报

点赞 评论 使用道具

andourson
DD
| 发表于 2024-2-15 17:27:29 | 显示全部楼层
想学,但看不懂
举报

点赞 评论 使用道具

wasa
D
| 发表于 2024-2-17 09:50:54 | 显示全部楼层
感谢分享,虽然不是很懂这种交易
举报

点赞 评论 使用道具

xinhua123
DDD
| 发表于 2024-2-18 00:07:19 | 显示全部楼层
谢谢分享代码
举报

点赞 评论 使用道具

xuxiaobai
DD
| 发表于 2024-2-24 14:13:35 | 显示全部楼层
看到代码就头疼,哈哈
举报

点赞 评论 使用道具

ken138888
B
| 发表于 2024-3-7 06:11:34 | 显示全部楼层
带移动止损啊
举报

点赞 评论 使用道具

13564043947
D
| 发表于 2024-3-24 01:45:30 | 显示全部楼层
策略不错
举报

点赞 评论 使用道具

t5530407
DD
| 发表于 2024-3-30 10:47:59 | 显示全部楼层
感谢提供
举报

点赞 评论 使用道具

liubing1986
D
| 发表于 2024-4-8 22:38:30 | 显示全部楼层
非常仔细
举报

点赞 评论 使用道具

631311675
D
| 发表于 2024-4-14 11:22:46 来自手机 | 显示全部楼层
移动止损与市价500点的间距,,但是它每次tick修改一次,是一小时,还是一秒?
举报

点赞 评论 使用道具

ken138888
B
| 发表于 2024-4-14 13:48:46 | 显示全部楼层
就是每个TICK啊
举报

点赞 评论 使用道具

wasesdq2
DD
| 发表于 4 天前 | 显示全部楼层
这个怎么弄?
举报

点赞 评论 使用道具

12下一页
发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)