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

【MQL4函数】挂单模块函数  

| 发表于 2021-6-1 14:16:25 | 显示全部楼层 |复制链接
挂单模块一般来说不会存在延迟的问题,所以它只需要解决拆单的问题,但是需要解决价格错误的问题,比如说挂突破买单,当挂单的价格小于当前买价时,挂单是挂不了的,所以要排除这种价格错误的情况。代码如下:
  1. int pendingorder(string sym,string direction,double price,double lot,double sl,
  2. double tp,int mag,string comment)
  3. {
  4.    int check;
  5.    double lot_min=MarketInfo(sym,MODE_MINLOT);
  6.    double lot_max=MarketInfo(sym,MODE_MAXLOT);
  7.    double lot_last=0;
  8.    int huadian=10;
  9.    double amount;
  10.    int i;
  11.    if(lot<lot_min)
  12.    {
  13.       Print("lot is too small");
  14.       return(1);
  15.    }
  16.    if(direction=="BUYSTOP" && price<MarketInfo(sym,MODE_ASK)+
  17. MarketInfo(sym,MODE_SPREAD)*MarketInfo(sym,MODE_POINT))
  18.    {
  19.       Print("Price is too low for BUYSTOP order");
  20.       return(1);
  21.    }
  22.    else if(direction=="BUYLIMIT" && price>MarketInfo(sym,MODE_ASK)-
  23. MarketInfo(sym,MODE_SPREAD)*MarketInfo(sym,MODE_POINT))
  24.    {
  25.       Print("Price is too high for BUYLIMIT order");
  26.       return(1);
  27.    }
  28.    else if(direction=="SELLSTOP" && price>MarketInfo(sym,MODE_BID)-
  29. MarketInfo(sym,MODE_SPREAD)*MarketInfo(sym,MODE_POINT))
  30.    {
  31.       Print("Price is too high for SELLSTOP order");
  32.       return(1);
  33.    }
  34.    else if(direction=="SELLLIMIT" && price<MarketInfo(sym,MODE_BID)+
  35. MarketInfo(sym,MODE_SPREAD)*MarketInfo(sym,MODE_POINT))
  36.    {
  37.       Print("Price is too low for SELLLIMIT order");
  38.       return(1);
  39.    }
  40.    
  41.    if(lot>lot_max)
  42.    {
  43.       amount=MathCeil(lot/lot_max);
  44.       lot_last=lot-(amount-1)*lot_max;
  45.       for(i=(int)amount;i>0;i--)
  46.                 {
  47.               if (i!=1)
  48.               {
  49.                   if(direction=="BUYSTOP")
  50.                    {
  51.               check=OrderSend(sym,OP_BUYSTOP,lot_max,price,huadian,sl,tp,
  52. comment,mag,0,clrBlue);
  53.             }
  54.             else if(direction=="BUYLIMIT")
  55.             {
  56.               check=OrderSend(sym,OP_BUYLIMIT,lot_max,price,huadian,sl,tp,
  57. comment,mag,0,clrBlue);
  58.             }
  59.             else if(direction=="SELLSTOP")
  60.             {
  61.               check=OrderSend(sym,OP_SELLSTOP,lot_max,price,huadian,sl,tp,
  62. comment,mag,0,clrRed);
  63.             }
  64.             else if(direction=="SELLLIMIT")
  65.             {
  66.               check=OrderSend(sym,OP_SELLLIMIT,lot_max,price,huadian,sl,tp,
  67. comment,mag,0,clrRed);
  68.             }
  69.               }
  70.               else
  71.               {
  72.             if(direction=="BUYSTOP")
  73.                          {
  74.               check=OrderSend(sym,OP_BUYSTOP,lot_last,price,huadian,sl,tp,
  75. comment,mag,0,clrBlue);
  76.             }
  77.             else if(direction=="BUYLIMIT")
  78.             {
  79.               check=OrderSend(sym,OP_BUYLIMIT,lot_last,price,huadian,sl,tp,
  80. comment,mag,0,clrBlue);
  81.             }
  82.             else if(direction=="SELLSTOP")
  83.             {
  84.               check=OrderSend(sym,OP_SELLSTOP,lot_last,price,huadian,sl,tp,
  85. comment,mag,0,clrRed);
  86.             }
  87.             else if(direction=="SELLLIMIT")
  88.             {
  89.               check=OrderSend(sym,OP_SELLLIMIT,lot_last,price,huadian,sl,tp,
  90. comment,mag,0,clrRed);
  91.             }
  92.                 }
  93.       }
  94.    }
  95.    else
  96.    {
  97.       if(direction=="BUYSTOP")
  98.       {
  99.            check=OrderSend(sym,OP_BUYSTOP,lot,price,huadian,sl,tp,
  100. comment,mag,0,clrBlue);
  101.       }
  102.       else if(direction=="BUYLIMIT")
  103.       {
  104.            check=OrderSend(sym,OP_BUYLIMIT,lot,price,huadian,sl,tp,
  105. comment,mag,0,clrBlue);
  106.       }
  107.       else if(direction=="SELLSTOP")
  108.       {
  109.            check=OrderSend(sym,OP_SELLSTOP,lot,price,huadian,sl,tp,
  110. comment,mag,0,clrRed);
  111.       }
  112.       else if(direction=="SELLLIMIT")
  113.       {
  114.            check=OrderSend(sym,OP_SELLLIMIT,lot,price,huadian,sl,tp,
  115. comment,mag,0,clrRed);
  116.       }
  117.    }
  118.    return(0);
  119. }
复制代码
如果有帮助,就支持一下我呗
举报

评论 使用道具

精彩评论9

jongjongyoshen
D
| 发表于 2021-6-2 08:18:08 | 显示全部楼层
关注
举报

点赞 评论 使用道具

升龙
DD
| 发表于 2021-6-2 09:04:00 | 显示全部楼层
感谢分享
举报

点赞 评论 使用道具

luo518
C
| 发表于 2021-6-3 22:26:54 | 显示全部楼层
感谢分享
举报

点赞 评论 使用道具

naowei
CCC
| 发表于 2021-10-22 17:03:16 | 显示全部楼层
谢谢分享挂单模块函数,请问是否可以直接加到没有挂单功能的源码中实现具有挂单功能?还要修改什么才能实现吗?
举报

点赞 评论 使用道具

dukeleung
D
| 发表于 2021-10-22 17:12:02 | 显示全部楼层
代码少了挂单距离的判断吧?
举报

点赞 评论 使用道具

a451780267
DD
| 发表于 2021-10-22 20:03:26 | 显示全部楼层
谢谢分享
举报

点赞 评论 使用道具

交易者
DDD
| 发表于 2022-1-11 15:36:06 | 显示全部楼层
感谢分享
举报

点赞 评论 使用道具

nanaliu
CC
| 发表于 2022-5-2 08:58:35 | 显示全部楼层
看看。。感谢分享。。。。
举报

点赞 评论 使用道具

紫竹青荷
CC
| 发表于 2022-6-17 21:20:50 | 显示全部楼层
感谢楼主分享
举报

点赞 评论 使用道具

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

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