电竞比分网-中国电竞赛事及体育赛事平台

分享

《MQL4實用編程》讀書筆記(3)

 分界交易 2016-05-08

本章解決2個編程問題:現(xiàn)價交易和掛單交易。

原文講得很詳細(xì),但我只譯出兩個例程。我覺得,這是高效率學(xué)習(xí)編程的最好辦法。

先看幾個相關(guān)函數(shù):

函數(shù)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 返回定單的編號。編號由交易服務(wù)器給出,獨一無二。若編號為-1,則定單被服務(wù)器或客戶端拒絕??赏ㄟ^函數(shù)GetLastError()查看拒絕的原因。

symbol 交易對象的名稱,用字符串,如"EURUSD",表示“歐元/美元”貨幣對。在“操盤手”中,可用函數(shù) Symbol()得到交易對象的名稱。

cmd 交易操作的類型,分別以內(nèi)建常量表示。

volume 交易手?jǐn)?shù)?,F(xiàn)價交易,必須檢查帳戶資金是否充足;掛單交易,手?jǐn)?shù)不受限制。

price 建倉(開單)價格。它由定單特點和交易規(guī)則確定。

slippage 滑點。能夠接受的建倉報價與成交價之間的最大點差。掛單不處理這一參數(shù)。

stoploss 止損價(點位)。

takeprofit 止盈價(點位)。

comment 對定單的文字說明。

magic 匯客自定義的定單標(biāo)識。

expiration 定單期限。

arrow_color 在主圖中,標(biāo)示建倉位置的箭頭顏色。若無此參數(shù)或其值為 CLR_NONE,則不顯示該箭頭。

函數(shù)MarketInfo

  1. double MarketInfo(string symbol, int type)  

它返回的信息,是MT4終端窗口"Market Watch"中的數(shù)據(jù)。而當(dāng)前交易對象的另外一些信息,則保存在預(yù)定義變量中。

參數(shù):

symbol - 交易對象的名稱;

type - 所要返回的信息類別代號。(參見Function MarketInfo Identifier)。

為了程序運行穩(wěn)定,最大限度減少交易請求被拒絕,在執(zhí)行函數(shù)OrderSend()之前,應(yīng)當(dāng)先用函數(shù)MarketInfo()和RefreshRates(),更新相關(guān)交易數(shù)據(jù)。

函數(shù)AccountFreeMargin

  1. double  AccountFreeMargin();  

返回當(dāng)前帳戶可用的保證金數(shù)額。

函數(shù)MathFloor

  1. double  MathFloor(  
  2.    double  val     // 數(shù)字  
  3.    );  

參數(shù)

val 數(shù)值

返回值 小于或等于val的最大整數(shù)

注意 可用函數(shù)floor()代替MathFloor()

函數(shù)RefreshRates

  1. bool  RefreshRates();  

返回值 數(shù)據(jù)得到更新返回True,否則,返回false。

說明 在“操盤手”或腳本中,刷新預(yù)定義變量和時序數(shù)組。

函數(shù)GetLastError

  1. int GetLastError();  

返回值 返回MQL4程序運行時,最新發(fā)生的錯誤。

說明 本函數(shù)被調(diào)用后,內(nèi)建變量 _LastError 沒有重設(shè)。若需重設(shè),調(diào)用函數(shù)ResetLastError()。

函數(shù)WindowPriceOnDropped

  1. double  WindowPriceOnDropped();  

返回值 “操盤手”或腳本運行的主圖窗口價格點位。只有用鼠標(biāo)將它倆拖拉到主圖中,數(shù)值才為有效。

說明 外建指標(biāo)沒有此值。

一、現(xiàn)價交易

  1. //-------------------------------------------------------------------------------  
  2. // openbuy.mq4   
  3. // 代碼僅用于教學(xué)  
  4. //-------------------------------------------------------------------------- 1 --  
  5. int start()                                     // 特別函數(shù) start  
  6.   {  
  7.    int Dist_SL =10;                             // 設(shè)定止損價位 10 點  
  8.    int Dist_TP =3;                              // 設(shè)定止盈價位 3 點  
  9.    double Prots=0.35;                           // 使用 35% 的保證金  
  10.    string Symb=Symbol();                        // 交易對象名稱  
  11. //-------------------------------------------------------------------------- 2 --  
  12.    while(true)                                  // 建倉過程的循環(huán)  
  13.      {  
  14.       int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 交易允許的止損/止盈最小點位  
  15.       double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 交易允許的最小手?jǐn)?shù)  
  16.       double Step   =MarketInfo(Symb,MODE_LOTSTEP);//交易允許的手?jǐn)?shù)變化幅度  
  17.       double Free   =AccountFreeMargin();       // 保證金  
  18.       double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每買進(jìn)一手所需保證金  
  19.       //-------------------------------------------------------------------- 3 --  
  20.       double Lot=MathFloor(Free*ProtsOne_LotStep)*Step;// 總手?jǐn)?shù)  
  21.       if (Lot < Min_Lot)                        // 若總手?jǐn)?shù)小于允許的下限  
  22.         {  
  23.          Alert(" Not enough money for ", Min_Lot," lots");  
  24.          break;                                 // 退出循環(huán)  
  25.         }  
  26.       //-------------------------------------------------------------------- 4 --  
  27.       if (Dist_SL < Min_Dist)                   // 止損點位小于允許的最小值  
  28.         {  
  29.          Dist_SL=Min_Dist;                      // 設(shè)定最小止損位  
  30.          Alert(" Increased the distance of SL = ",Dist_SL," pt");  
  31.         }  
  32.       double SL=Bid - Dist_SL*Point;            // 確定交易的止損價位  
  33.                                                 // Bid 系統(tǒng)內(nèi)建變量:當(dāng)前交易品種的最新買價  
  34.                                                 // Point 系統(tǒng)內(nèi)建變量:報價小數(shù)部分的值  
  35.   
  36.       //-------------------------------------------------------------------- 5 --    
  37.       if (Dist_TP < Min_Dist)                   // 止盈點位小于允許的最小值  
  38.       {     
  39.          Dist_TP=Min_Dist;                      // 設(shè)定最小止盈位  
  40.          Alert(" Increased the distance of TP = ",Dist_TP," pt");  
  41.       }  
  42.          double TP=Bid + Dist_TP*Point;            // 確定交易的止盈價位                                              
  43.   
  44.                                                 // Bid 系統(tǒng)內(nèi)建變量:當(dāng)前交易品種的最新買價  
  45.   
  46.                                                 // Point 系統(tǒng)內(nèi)建變量:報價小數(shù)部分的值  
  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;                                 // 退出循環(huán)  
  57.       }     
  58.       //-------------------------------------------------------------------- 8 --  
  59.       int Error=GetLastError();                 // 交易失敗 :(  
  60.       switch(Error)                             // 可以克服的錯誤  
  61.       {  
  62.          case 135:Alert("The price has changed. Retrying..");               
  63.             RefreshRates();                     // 更新數(shù)據(jù)  
  64.             continue;                           // 繼續(xù)循環(huán)  
  65.          case 136:  
  66.             Alert("No prices. Waiting for a new tick..");  
  67.             while(RefreshRates()==false)        // 獲得新報價  
  68.                Sleep(1);                        // 延遲循環(huán)  
  69.             continue;                           // 繼續(xù)循環(huán)  
  70.          case 146:Alert("Trading subsystem is busy. Retrying..");            
  71.             Sleep(500);                         // 簡單處理方案  
  72.             RefreshRates();                     // 更新數(shù)據(jù)  
  73.             continue;                           // 繼續(xù)循環(huán)  
  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;                                    // 退出循環(huán)  
  93.    }  
  94.     //-------------------------------------------------------------------------- 9 --  
  95.    Alert ("The script has completed its operations ---------------------------");  
  96.    return;                                      // 退出 start()  
  97.   }  
  98. //-------------------------------------------------------------------------- 10 --  

二、掛單交易

  1. //------------------------------------------------------------------------------------  
  2. // openbuystop.mq4   
  3. // 代碼僅用于教學(xué)  
  4. //------------------------------------------------------------------------------- 1 --  
  5. int start()                                     // 特別函數(shù) start  
  6.   {  
  7.    int Dist_SL =10;                             // 設(shè)定止損位 10 個點  
  8.    int Dist_TP =3;                              // 設(shè)定止盈位 3 個點  
  9.    double Prots=0.35;                           // 交易使用35%的保證金  
  10.    string Symb=Symbol();                        // 交易對象名稱  
  11.    double Win_Price=WindowPriceOnDropped();     // 腳本被拖拉進(jìn)的窗口,價格點位  
  12.    Alert("The price is set by the mouse as Price = ",Win_Price);// 點擊鼠標(biāo)設(shè)定的價格  
  13. //------------------------------------------------------------------------------- 2 --  
  14.    while(true)                                  // 建倉過程循環(huán)  
  15.      {  
  16.       int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 最小止損/止盈點位  
  17.       double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 最小交易手?jǐn)?shù)  
  18.       double Free   =AccountFreeMargin();       // 保證金  
  19.       double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每手所需保證金  
  20.       double Lot=MathFloor(Free*ProtsOne_LotMin_Lot)*Min_Lot;// 總手?jǐn)?shù)  
  21.       //------------------------------------------------------------------------- 3 --  
  22.       double Price=Win_Price;                   // 點擊鼠標(biāo)設(shè)定的價格  
  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)                             // 服務(wù)器接受掛單!:)  
  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();                     // 更新數(shù)據(jù)  
  58.             continue;                           // 繼續(xù)  
  59.          case 135:Alert("The price has changed. Retrying..");  
  60.             RefreshRates();                     // 更新數(shù)據(jù)  
  61.             continue;                           // 繼續(xù)  
  62.          case 146:Alert("Trading subsystem is busy. Retrying..");  
  63.             Sleep(500);                         // 簡單處理方案  
  64.             RefreshRates();                     // 更新數(shù)據(jù)  
  65.             continue;                           // 繼續(xù)  
  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 -- 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多