[問題] MQL4問題請教

看板Trading作者 (阿泰)時間5年前 (2019/01/01 00:33), 編輯推噓4(4020)
留言24則, 5人參與, 6年前最新討論串1/1
關於MT4自帶的範例MACD Sample,有些疑問一直想不清楚, google半天也找不到答案,想請教版上的前輩高手 以下為第89行到123行的程式碼 //--- it is important to enter the market correctly, but it is more important to exit it correctly... for(cnt=0;cnt<total;cnt++) { if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) <------(1) continue; if(OrderType()<=OP_SELL && // check for opened position OrderSymbol()==Symbol()) // check for symbol { //--- long position is opened if(OrderType()==OP_BUY) { //--- should it be closed? if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && MacdCurrent>(MACDCloseLevel*Point)) { //--- close order and exit if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) <--(2) Print("OrderClose error ",GetLastError()); return; } //--- check for trailing stop if(TrailingStop>0) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { //--- modify order and exit (3)----> if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green)) Print("OrderModify error ",GetLastError()); return; } } } } 我用括號標註了3個地方(抱歉,不知道怎麼上色...) (1)第92行的!OrderSelect (2)第105行的!OrderClose (3)第117行的!OrderModify 想請問是只要有寫==false的情況,==true的狀況就不用寫了嗎? 另外這樣寫有什麼用意或好處嗎? 希望有好心人能解答我的疑問,先謝謝了... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.115.234.7 ※ 文章網址: https://www.ptt.cc/bbs/Trading/M.1546274003.A.027.html

01/01 00:53, 6年前 , 1F
先提程式技術方面, 這是所謂的防衛子句寫法
01/01 00:53, 1F

01/01 00:54, 6年前 , 2F
這廣泛出現在許多類似 C 與 C++ 的程式語言的程式中
01/01 00:54, 2F

01/01 00:54, 6年前 , 3F
這有點像是站衛兵的意思, 如果符合條件就會被衛兵趕出去
01/01 00:54, 3F

01/01 00:55, 6年前 , 4F
趕出去的方法有 continue 與提早的 return
01/01 00:55, 4F

01/01 00:56, 6年前 , 5F
那麼在程式交易方面, 例如 Easy Lang 或 Power Lang
01/01 00:56, 5F

01/01 00:56, 6年前 , 6F
或是一些 C# Based 的開發環境, 例如 Wealth-Lab 都有把
01/01 00:56, 6F

01/01 00:57, 6年前 , 7F
掛單的流程責任切得很清楚, 基本原則就是每個動作都要查
01/01 00:57, 7F

01/01 00:58, 6年前 , 8F
他不能不給你查, 所以設計上 Order Command 都會有 return
01/01 00:58, 8F

01/01 00:59, 6年前 , 9F
但是共識上會假設程式執行很快, 所以不會幫你鎖單
01/01 00:59, 9F

01/01 01:00, 6年前 , 10F
也就是如果你 OrderSelect 但是其他 EA 也在跑他也能動他
01/01 01:00, 10F

01/01 01:00, 6年前 , 11F
所以你通常都會看到只對 Error Path (出錯的執行路徑)
01/01 01:00, 11F

01/01 01:01, 6年前 , 12F
的處理, 就像是一堆 !OrderSelect, !OrderClose
01/01 01:01, 12F

01/01 01:02, 6年前 , 13F
但是是不是執行成功就不用檢查? 實務上還是要
01/01 01:02, 13F

01/01 01:03, 6年前 , 14F
因為有時候銀行端/伺服器端, 會幫你自動撤單(不知道原因)
01/01 01:03, 14F

01/01 01:03, 6年前 , 15F
又自動幫你補單(感覺像他手殘按到) 銀行端補單的 Magic
01/01 01:03, 15F

01/01 01:04, 6年前 , 16F
Number 會非常詭異, (通常伴隨著錯誤的 lots...)
01/01 01:04, 16F

01/01 01:04, 6年前 , 17F
所以其實還是要做, 但是你在網路上查得的各種資源
01/01 01:04, 17F

01/01 01:05, 6年前 , 18F
原則上還是會假設 Order Cmd 出去就穩了
01/01 01:05, 18F

01/01 01:07, 6年前 , 19F
難得看到 MQL 的文, 推推 QQ!!
01/01 01:07, 19F

01/01 01:31, 6年前 , 20F
想不到這麼快就能有這麼詳盡又專業的回覆
01/01 01:31, 20F

01/01 01:35, 6年前 , 21F
真的非常感謝rcwang大,解答了我大部分的疑惑
01/01 01:35, 21F

01/01 12:17, 6年前 , 22F
好專業!!!
01/01 12:17, 22F

01/02 09:58, 6年前 , 23F
推,專業回
01/02 09:58, 23F

01/02 15:26, 6年前 , 24F
專業 跪
01/02 15:26, 24F
文章代碼(AID): #1SAaJJ0d (Trading)