|
本章解決2個編程問題:現(xiàn)價交易和掛單交易。
原文講得很詳細(xì),但我只譯出兩個例程。我覺得,這是高效率學(xué)習(xí)編程的最好辦法。
先看幾個相關(guān)函數(shù):
函數(shù)OrderSend
- int OrderSend (string symbol, int cmd, double volume, double price,
- int slippage, double stoploss,double takeprofit,
- string comment=NULL, int magic=0, datetime expiration=0,
- 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
- 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
- double AccountFreeMargin();
返回當(dāng)前帳戶可用的保證金數(shù)額。
函數(shù)MathFloor
- double MathFloor(
- double val // 數(shù)字
- );
參數(shù)
val 數(shù)值
返回值 小于或等于val的最大整數(shù)
注意 可用函數(shù)floor()代替MathFloor()
函數(shù)RefreshRates
返回值 數(shù)據(jù)得到更新返回True,否則,返回false。
說明 在“操盤手”或腳本中,刷新預(yù)定義變量和時序數(shù)組。
函數(shù)GetLastError
返回值 返回MQL4程序運行時,最新發(fā)生的錯誤。
說明 本函數(shù)被調(diào)用后,內(nèi)建變量 _LastError 沒有重設(shè)。若需重設(shè),調(diào)用函數(shù)ResetLastError()。
函數(shù)WindowPriceOnDropped
- double WindowPriceOnDropped();
返回值 “操盤手”或腳本運行的主圖窗口價格點位。只有用鼠標(biāo)將它倆拖拉到主圖中,數(shù)值才為有效。
說明 外建指標(biāo)沒有此值。
一、現(xiàn)價交易
- //-------------------------------------------------------------------------------
- // openbuy.mq4
- // 代碼僅用于教學(xué)
- //-------------------------------------------------------------------------- 1 --
- int start() // 特別函數(shù) start
- {
- int Dist_SL =10; // 設(shè)定止損價位 10 點
- int Dist_TP =3; // 設(shè)定止盈價位 3 點
- double Prots=0.35; // 使用 35% 的保證金
- string Symb=Symbol(); // 交易對象名稱
- //-------------------------------------------------------------------------- 2 --
- while(true) // 建倉過程的循環(huán)
- {
- int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 交易允許的止損/止盈最小點位
- double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 交易允許的最小手?jǐn)?shù)
- double Step =MarketInfo(Symb,MODE_LOTSTEP);//交易允許的手?jǐn)?shù)變化幅度
- double Free =AccountFreeMargin(); // 保證金
- double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每買進(jìn)一手所需保證金
- //-------------------------------------------------------------------- 3 --
- double Lot=MathFloor(Free*ProtsOne_LotStep)*Step;// 總手?jǐn)?shù)
- if (Lot < Min_Lot) // 若總手?jǐn)?shù)小于允許的下限
- {
- Alert(" Not enough money for ", Min_Lot," lots");
- break; // 退出循環(huán)
- }
- //-------------------------------------------------------------------- 4 --
- if (Dist_SL < Min_Dist) // 止損點位小于允許的最小值
- {
- Dist_SL=Min_Dist; // 設(shè)定最小止損位
- Alert(" Increased the distance of SL = ",Dist_SL," pt");
- }
- double SL=Bid - Dist_SL*Point; // 確定交易的止損價位
- // Bid 系統(tǒng)內(nèi)建變量:當(dāng)前交易品種的最新買價
- // Point 系統(tǒng)內(nèi)建變量:報價小數(shù)部分的值
-
- //-------------------------------------------------------------------- 5 --
- if (Dist_TP < Min_Dist) // 止盈點位小于允許的最小值
- {
- Dist_TP=Min_Dist; // 設(shè)定最小止盈位
- Alert(" Increased the distance of TP = ",Dist_TP," pt");
- }
- double TP=Bid + Dist_TP*Point; // 確定交易的止盈價位
-
- // Bid 系統(tǒng)內(nèi)建變量:當(dāng)前交易品種的最新買價
-
- // Point 系統(tǒng)內(nèi)建變量:報價小數(shù)部分的值
-
- //-------------------------------------------------------------------- 6 --
- Alert("The request was sent to the server. Waiting for reply..");
- int ticket=OrderSend(Symb, OP_BUY, Lot, Ask, 2, SL, TP);
-
- //-------------------------------------------------------------------- 7 --
- if (ticket>0) // 交易成功! :)
- {
- Alert ("Opened order Buy ",ticket);
- break; // 退出循環(huán)
- }
- //-------------------------------------------------------------------- 8 --
- int Error=GetLastError(); // 交易失敗 :(
- switch(Error) // 可以克服的錯誤
- {
- case 135:Alert("The price has changed. Retrying..");
- RefreshRates(); // 更新數(shù)據(jù)
- continue; // 繼續(xù)循環(huán)
- case 136:
- Alert("No prices. Waiting for a new tick..");
- while(RefreshRates()==false) // 獲得新報價
- Sleep(1); // 延遲循環(huán)
- continue; // 繼續(xù)循環(huán)
- case 146:Alert("Trading subsystem is busy. Retrying..");
- Sleep(500); // 簡單處理方案
- RefreshRates(); // 更新數(shù)據(jù)
- continue; // 繼續(xù)循環(huán)
- }
- switch(Error) // 致命錯誤
- {
- case 2 :
- Alert("Common error.");
- break; // 退出本 'switch'
- case 5 :
- Alert("Outdated version of the client terminal.");
- break; // 退出本 'switch'
- case 64:
- Alert("The account is blocked.");
- break; // 退出本 'switch'
- case 133:
- Alert("Trading forbidden");
- break; // 退出本 'switch'
- default:
- Alert("Occurred error ",Error);// 其他錯誤
- }
- break; // 退出循環(huán)
- }
- //-------------------------------------------------------------------------- 9 --
- Alert ("The script has completed its operations ---------------------------");
- return; // 退出 start()
- }
- //-------------------------------------------------------------------------- 10 --
二、掛單交易
- //------------------------------------------------------------------------------------
- // openbuystop.mq4
- // 代碼僅用于教學(xué)
- //------------------------------------------------------------------------------- 1 --
- int start() // 特別函數(shù) start
- {
- int Dist_SL =10; // 設(shè)定止損位 10 個點
- int Dist_TP =3; // 設(shè)定止盈位 3 個點
- double Prots=0.35; // 交易使用35%的保證金
- string Symb=Symbol(); // 交易對象名稱
- double Win_Price=WindowPriceOnDropped(); // 腳本被拖拉進(jìn)的窗口,價格點位
- Alert("The price is set by the mouse as Price = ",Win_Price);// 點擊鼠標(biāo)設(shè)定的價格
- //------------------------------------------------------------------------------- 2 --
- while(true) // 建倉過程循環(huán)
- {
- int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// 最小止損/止盈點位
- double Min_Lot=MarketInfo(Symb,MODE_MINLOT);// 最小交易手?jǐn)?shù)
- double Free =AccountFreeMargin(); // 保證金
- double One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED);//每手所需保證金
- double Lot=MathFloor(Free*ProtsOne_LotMin_Lot)*Min_Lot;// 總手?jǐn)?shù)
- //------------------------------------------------------------------------- 3 --
- double Price=Win_Price; // 點擊鼠標(biāo)設(shè)定的價格
- if (NormalizeDouble(Price,Digits)< // 若小于下限
- NormalizeDouble(Ask+Min_Dist*Point,Digits))
- { // 僅可 BuyStop 掛單!
- Price=Ask+Min_Dist*Point; // 沒有平倉
- Alert("Changed the requested price: Price = ",Price);
- }
- //------------------------------------------------------------------------- 4 --
- double SL=Price - Dist_SL*Point; // 止損報價
- if (Dist_SL < Min_Dist) // 若低于下限
- {
- SL=Price - Min_Dist*Point; // 以下限為止損點位
- Alert(" Increased the distance of SL = ",Min_Dist," pt");
- }
- //------------------------------------------------------------------------- 5 --
- double TP=Price + Dist_TP*Point; // 止盈報價
- if (Dist_TP < Min_Dist) // 若低于下限
- {
- TP=Price + Min_Dist*Point; // 以下限為止盈點位
- Alert(" Increased the distance of TP = ",Min_Dist," pt");
- }
- //------------------------------------------------------------------------- 6 --
- Alert("The request was sent to the server. Waiting for reply..");
- int ticket=OrderSend(Symb, OP_BUYSTOP, Lot, Price, 0, SL, TP);
- //------------------------------------------------------------------------- 7 --
- if (ticket>0) // 服務(wù)器接受掛單!:)
- {
- Alert ("Placed order BuyStop ",ticket);
- break; // 退出掛單
- }
- //------------------------------------------------------------------------- 8 --
- int Error=GetLastError(); // 掛單被拒絕 :(
- switch(Error) // 可克服的錯誤
- {
- case 129:Alert("Invalid price. Retrying..");
- RefreshRates(); // 更新數(shù)據(jù)
- continue; // 繼續(xù)
- case 135:Alert("The price has changed. Retrying..");
- RefreshRates(); // 更新數(shù)據(jù)
- continue; // 繼續(xù)
- case 146:Alert("Trading subsystem is busy. Retrying..");
- Sleep(500); // 簡單處理方案
- RefreshRates(); // 更新數(shù)據(jù)
- continue; // 繼續(xù)
- }
- switch(Error) // 致命錯誤
- {
- case 2 : Alert("Common error.");
- break; // 退出本 'switch'
- case 5 : Alert("Outdated version of the client terminal.");
- break; // 退出本 'switch'
- case 64: Alert("The account is blocked.");
- break; // 退出本 'switch'
- case 133:Alert("Trading fobidden");
- break; // 退出本 'switch'
- default: Alert("Occurred error ",Error);// 其他錯誤
- }
- break; // 退出掛單
- }
- //------------------------------------------------------------------------------- 9 --
- Alert ("The script has completed its operations -----------------------------");
- return; // 退出 start()
- }
- //------------------------------------------------------------------------------- 10 --
|