打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
《MQL4实用编程》读书笔记(3)

本章解决2个编程问题:现价交易和挂单交易。

原文讲得很详细,但我只译出两个例程。我觉得,这是高效率学习编程的最好办法。

先看几个相关函数:

函数OrderSend

  1. int OrderSend (string symbol, int cmd, double volume, double price,   
  2.                int slippage, double stoploss,double takeprofit,   
  3.                string comment=NULL, int magic=0, datetime expiration=0,   
  4.                color arrow_color=CLR_NONE)  

OrderSend 返回定单的编号。编号由交易服务器给出,独一无二。若编号为-1,则定单被服务器或客户端拒绝。可通过函数GetLastError()查看拒绝的原因。

symbol 交易对象的名称,用字符串,如"EURUSD",表示“欧元/美元”货币对。在“操盘手”中,可用函数 Symbol()得到交易对象的名称。

cmd 交易操作的类型,分别以内建常量表示。

volume 交易手数。现价交易,必须检查帐户资金是否充足;挂单交易,手数不受限制。

price 建仓(开单)价格。它由定单特点和交易规则确定。

slippage 滑点。能够接受的建仓报价与成交价之间的最大点差。挂单不处理这一参数。

stoploss 止损价(点位)。

takeprofit 止盈价(点位)。

comment 对定单的文字说明。

magic 汇客自定义的定单标识。

expiration 定单期限。

arrow_color 在主图中,标示建仓位置的箭头颜色。若无此参数或其值为 CLR_NONE,则不显示该箭头。

函数MarketInfo

  1. double MarketInfo(string symbol, int type)  

它返回的信息,是MT4终端窗口"Market Watch"中的数据。而当前交易对象的另外一些信息,则保存在预定义变量中。

参数:

symbol - 交易对象的名称;

type - 所要返回的信息类别代号。(参见Function MarketInfo Identifier)。

为了程序运行稳定,最大限度减少交易请求被拒绝,在执行函数OrderSend()之前,应当先用函数MarketInfo()和RefreshRates(),更新相关交易数据。

函数AccountFreeMargin

  1. double  AccountFreeMargin();  

返回当前帐户可用的保证金数额。

函数MathFloor

  1. double  MathFloor(  
  2.    double  val     // 数字  
  3.    );  

参数

val 数值

返回值 小于或等于val的最大整数

注意 可用函数floor()代替MathFloor()

函数RefreshRates

  1. bool  RefreshRates();  

返回值 数据得到更新返回True,否则,返回false。

说明 在“操盘手”或脚本中,刷新预定义变量和时序数组。

函数GetLastError

  1. int GetLastError();  

返回值 返回MQL4程序运行时,最新发生的错误。

说明 本函数被调用后,内建变量 _LastError 没有重设。若需重设,调用函数ResetLastError()。

函数WindowPriceOnDropped

  1. double  WindowPriceOnDropped();  

返回值 “操盘手”或脚本运行的主图窗口价格点位。只有用鼠标将它俩拖拉到主图中,数值才为有效。

说明 外建指标没有此值。

一、现价交易

  1. //-------------------------------------------------------------------------------  
  2. // openbuy.mq4   
  3. // 代码仅用于教学  
  4. //-------------------------------------------------------------------------- 1 --  
  5. int start()                                     // 特别函数 start  
  6.   {  
  7.    int Dist_SL =10;                             // 设定止损价位 10 点  
  8.    int Dist_TP =3;                              // 设定止盈价位 3 点  
  9.    double Prots=0.35;                           // 使用 35% 的保证金  
  10.    string Symb=Symbol();                        // 交易对象名称  
  11. //-------------------------------------------------------------------------- 2 --  
  12.    while(true)                                  // 建仓过程的循环  
  13.      {  
  14.       int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 交易允许的止损/止盈最小点位  
  15.       double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 交易允许的最小手数  
  16.       double Step   =MarketInfo(Symb,MODE_LOTSTEP);//交易允许的手数变化幅度  
  17.       double Free   =AccountFreeMargin();       // 保证金  
  18.       double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每买进一手所需保证金  
  19.       //-------------------------------------------------------------------- 3 --  
  20.       double Lot=MathFloor(Free*ProtsOne_LotStep)*Step;// 总手数  
  21.       if (Lot < Min_Lot)                        // 若总手数小于允许的下限  
  22.         {  
  23.          Alert(" Not enough money for ", Min_Lot," lots");  
  24.          break;                                 // 退出循环  
  25.         }  
  26.       //-------------------------------------------------------------------- 4 --  
  27.       if (Dist_SL < Min_Dist)                   // 止损点位小于允许的最小值  
  28.         {  
  29.          Dist_SL=Min_Dist;                      // 设定最小止损位  
  30.          Alert(" Increased the distance of SL = ",Dist_SL," pt");  
  31.         }  
  32.       double SL=Bid - Dist_SL*Point;            // 确定交易的止损价位  
  33.                                                 // Bid 系统内建变量:当前交易品种的最新买价  
  34.                                                 // Point 系统内建变量:报价小数部分的值  
  35.   
  36.       //-------------------------------------------------------------------- 5 --    
  37.       if (Dist_TP < Min_Dist)                   // 止盈点位小于允许的最小值  
  38.       {     
  39.          Dist_TP=Min_Dist;                      // 设定最小止盈位  
  40.          Alert(" Increased the distance of TP = ",Dist_TP," pt");  
  41.       }  
  42.          double TP=Bid + Dist_TP*Point;            // 确定交易的止盈价位                                              
  43.   
  44.                                                 // Bid 系统内建变量:当前交易品种的最新买价  
  45.   
  46.                                                 // Point 系统内建变量:报价小数部分的值  
  47.   
  48.      //-------------------------------------------------------------------- 6 --  
  49.       Alert("The request was sent to the server. Waiting for reply..");        
  50.       int ticket=OrderSend(Symb, OP_BUY, Lot, Ask, 2, SL, TP);           
  51.   
  52.      //-------------------------------------------------------------------- 7 --  
  53.       if (ticket>0)                             // 交易成功! :)  
  54.       {   
  55.         Alert ("Opened order Buy ",ticket);  
  56.         break;                                 // 退出循环  
  57.       }     
  58.       //-------------------------------------------------------------------- 8 --  
  59.       int Error=GetLastError();                 // 交易失败 :(  
  60.       switch(Error)                             // 可以克服的错误  
  61.       {  
  62.          case 135:Alert("The price has changed. Retrying..");               
  63.             RefreshRates();                     // 更新数据  
  64.             continue;                           // 继续循环  
  65.          case 136:  
  66.             Alert("No prices. Waiting for a new tick..");  
  67.             while(RefreshRates()==false)        // 获得新报价  
  68.                Sleep(1);                        // 延迟循环  
  69.             continue;                           // 继续循环  
  70.          case 146:Alert("Trading subsystem is busy. Retrying..");            
  71.             Sleep(500);                         // 简单处理方案  
  72.             RefreshRates();                     // 更新数据  
  73.             continue;                           // 继续循环  
  74.       }  
  75.       switch(Error)                             // 致命错误  
  76.       {  
  77.          case 2 :   
  78.              Alert("Common error.");  
  79.              break;                              // 退出本 'switch'  
  80.          case 5 :   
  81.              Alert("Outdated version of the client terminal.");  
  82.              break;                              // 退出本 'switch'  
  83.          case 64:  
  84.              Alert("The account is blocked.");  
  85.              break;                              // 退出本 'switch'  
  86.          case 133:  
  87.              Alert("Trading forbidden");  
  88.              break;                              // 退出本 'switch'  
  89.          default:  
  90.              Alert("Occurred error ",Error);// 其他错误             
  91.       }  
  92.       break;                                    // 退出循环  
  93.    }  
  94.     //-------------------------------------------------------------------------- 9 --  
  95.    Alert ("The script has completed its operations ---------------------------");  
  96.    return;                                      // 退出 start()  
  97.   }  
  98. //-------------------------------------------------------------------------- 10 --  

二、挂单交易

  1. //------------------------------------------------------------------------------------  
  2. // openbuystop.mq4   
  3. // 代码仅用于教学  
  4. //------------------------------------------------------------------------------- 1 --  
  5. int start()                                     // 特别函数 start  
  6.   {  
  7.    int Dist_SL =10;                             // 设定止损位 10 个点  
  8.    int Dist_TP =3;                              // 设定止盈位 3 个点  
  9.    double Prots=0.35;                           // 交易使用35%的保证金  
  10.    string Symb=Symbol();                        // 交易对象名称  
  11.    double Win_Price=WindowPriceOnDropped();     // 脚本被拖拉进的窗口,价格点位  
  12.    Alert("The price is set by the mouse as Price = ",Win_Price);// 点击鼠标设定的价格  
  13. //------------------------------------------------------------------------------- 2 --  
  14.    while(true)                                  // 建仓过程循环  
  15.      {  
  16.       int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 最小止损/止盈点位  
  17.       double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 最小交易手数  
  18.       double Free   =AccountFreeMargin();       // 保证金  
  19.       double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每手所需保证金  
  20.       double Lot=MathFloor(Free*ProtsOne_LotMin_Lot)*Min_Lot;// 总手数  
  21.       //------------------------------------------------------------------------- 3 --  
  22.       double Price=Win_Price;                   // 点击鼠标设定的价格  
  23.       if (NormalizeDouble(Price,Digits)<        // 若小于下限  
  24.          NormalizeDouble(Ask+Min_Dist*Point,Digits))  
  25.         {                                       // 仅可 BuyStop 挂单!  
  26.          Price=Ask+Min_Dist*Point;              // 没有平仓  
  27.          Alert("Changed the requested price: Price = ",Price);  
  28.         }  
  29.       //------------------------------------------------------------------------- 4 --  
  30.       double SL=Price - Dist_SL*Point;          // 止损报价  
  31.       if (Dist_SL < Min_Dist)                   // 若低于下限  
  32.         {  
  33.          SL=Price - Min_Dist*Point;             // 以下限为止损点位  
  34.          Alert(" Increased the distance of SL = ",Min_Dist," pt");  
  35.         }  
  36.       //------------------------------------------------------------------------- 5 --  
  37.       double TP=Price + Dist_TP*Point;          // 止盈报价  
  38.       if (Dist_TP < Min_Dist)                   // 若低于下限  
  39.         {  
  40.          TP=Price + Min_Dist*Point;             // 以下限为止盈点位  
  41.          Alert(" Increased the distance of TP = ",Min_Dist," pt");  
  42.         }  
  43.       //------------------------------------------------------------------------- 6 --  
  44.       Alert("The request was sent to the server. Waiting for reply..");  
  45.       int ticket=OrderSend(Symb, OP_BUYSTOP, Lot, Price, 0, SL, TP);  
  46.       //------------------------------------------------------------------------- 7 --  
  47.       if (ticket>0)                             // 服务器接受挂单!:)  
  48.         {  
  49.          Alert ("Placed order BuyStop ",ticket);  
  50.          break;                                 // 退出挂单  
  51.         }  
  52.       //------------------------------------------------------------------------- 8 --  
  53.       int Error=GetLastError();                 // 挂单被拒绝 :(  
  54.       switch(Error)                             // 可克服的错误  
  55.         {  
  56.          case 129:Alert("Invalid price. Retrying..");  
  57.             RefreshRates();                     // 更新数据  
  58.             continue;                           // 继续  
  59.          case 135:Alert("The price has changed. Retrying..");  
  60.             RefreshRates();                     // 更新数据  
  61.             continue;                           // 继续  
  62.          case 146:Alert("Trading subsystem is busy. Retrying..");  
  63.             Sleep(500);                         // 简单处理方案  
  64.             RefreshRates();                     // 更新数据  
  65.             continue;                           // 继续  
  66.         }  
  67.       switch(Error)                             // 致命错误  
  68.         {  
  69.          case 2 : Alert("Common error.");  
  70.             break;                              // 退出本 'switch'  
  71.          case 5 : Alert("Outdated version of the client terminal.");  
  72.             break;                              // 退出本 'switch'  
  73.          case 64: Alert("The account is blocked.");  
  74.             break;                              // 退出本 'switch'  
  75.          case 133:Alert("Trading fobidden");  
  76.             break;                              // 退出本 'switch'     
  77.          default: Alert("Occurred error ",Error);// 其他错误  
  78.         }  
  79.       break;                                    // 退出挂单  
  80.      }  
  81. //------------------------------------------------------------------------------- 9 --  
  82.    Alert ("The script has completed its operations -----------------------------");  
  83.    return;                                      // 退出 start()  
  84.   }  
  85. //------------------------------------------------------------------------------- 10 -- 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MQL4课程——模块编写
修改止损、止赢源码
doll11交易系统(最新汉化版)
MT4智能交易 (OrderModify()函数)
MQL4常规函数(一)|MQL4编程参考文档|myEAtrade
'EA 交易中的限制和验证
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服