AI自動交易 #1 – 你必須知道的外匯 3 種追賺策略 (1/3)

Forex Forest > 程式交易 > AI自動交易 #1 – 你必須知道的外匯 3 種追賺策略 (1/3)

市場上常常充斥著各種追賺策略,但沒有人會告訴你背後的原理,以下將會簡單介紹 3 種你未必清楚了解的追賺策略。

  1. 以步距追賺 (Trailing in Pips)
  2. 以浮盈追賺 (Trailing in Money)
  3. 以隱密步距追賺 (Hidden Trailing in Pips)
  1. 以步距追賺 (Trailing in Pips)

以步距追賺是最基本的追賺策略,原理是將止損位(SL)置於浮盈單下,隨著價位上升而推高止損位,從而達至最佳利潤。但這種策略如何在MQL4實現呢?

  • 設定2個參數,啟動門檻5 pips,追賺距離2 pips

extern double TrailingStart = 5.0;
extern double TrailingDist = 2.0;

  • 啟動程式前,先計算價位實際距離

int pipAdj;
double tstartPipDelta, tslPipDelta;

int init(){
 //PIPS TO PRICE (50 --> 0.00050)
 pipAdj = (Digits%2==1) ? 10 : 1;
 double M_Point = MarketInfo(OrderSymbol(), MODE_POINT);
 double tstartPipDelta = TrailingStart * M_Point * pipAdj;
 double tslPipDelta = TrailingDist * M_Point * pipAdj;
 return(0);
}

  • 為了方便,設立修改訂單的指令

int ModifySL(double price){
 if (!OrderModify(OrderTicket(), OrderOpenPrice(), price, OrderTakeProfit(), Red))
  Print("Error setting Buy trailing stop: ", GetLastError());
 return(0);
}

  • 每次報價時,檢查現價是否已達到啟動門檻

if (OrderType() == OP_BUY){
 if (Bid - OrderOpenPrice() > tstartPipDelta){
  //START TRAILING
 }
}else if (OrderType() == OP_SELL){
 if (OrderOpenPrice() - Ask > tstartPipDelta){
  //START TRAILING
 }
}

  • 如追賺距離不足,則修改訂單,緊貼價位,保障利潤

//START TRAILING
if(OrderStopLoss() > 0){
 if (Bid - OrderStopLoss() > tslPipDelta){
  ModifySL(Bid - tslPipDelta);
 }
}else{
 ModifySL(Bid - tslPipDelta);
}

  • 注意事項1: 沽單計算方法略有不同

//START TRAILING
if(OrderStopLoss() > 0){
 if (OrderStopLoss() - Ask > tslPipDelta){
  ModifySL(Ask + tslPipDelta);
 }
}else{
 ModifySL(Ask + tslPipDelta);
}

  • 注意事項2: 交易商普遍會有最低止損距離,要小心檢查清楚,否則會完全失效

//CHECK VALID DISTANCE
double MinDist = MarketInfo(Symbol(), MODE_STOPLEVEL) * M_Point;
if(tslPipDelta < MinDist){
 Alert("Trailing Distance must bigger than Broker Limit: "+MinDist);
 ExpertRemove();
}

以下是完整代碼 (點擊下載: T01A_Trailing_in_Pips)

//+------------------------------------------------------------------+
// Tutorial 1a on Trailing in Pips
// Copyright 2019, Forex Forest
// http://www.forexforest.net
//+------------------------------------------------------------------+
#property copyright "Forex Forest 2019"
#property link "http://www.forexforest.net/"
#property strict

extern double TrailingStart = 5;  //Trailing Start in Pips
extern double TrailingDist = 5;  //Trailing Distance in Pips

int pipAdj;
double tstartPipDelta, tslPipDelta;

int init(){
 //PIPS TO PRICE (50 --> 0.00050)
 pipAdj = (Digits%2==1) ? 10 : 1;
 double M_Point = MarketInfo(OrderSymbol(), MODE_POINT);
 tstartPipDelta = TrailingStart * M_Point * pipAdj;
 tslPipDelta = TrailingDist * M_Point * pipAdj;
 //CHECK VALID DISTANCE
 double MinDist = MarketInfo(Symbol(), MODE_STOPLEVEL) * M_Point;
 if(tslPipDelta < MinDist){
  Alert("Trailing Distance must bigger than Broker Limit: "+MinDist);
  ExpertRemove();
 }
 return(0);
}

int deinit(){
 return(0);
}

int ModifySL(double price){
 if (!OrderModify(OrderTicket(), OrderOpenPrice(), price, OrderTakeProfit(), Red))
  Print("Error setting Buy trailing stop: ", GetLastError());
 return(0);
}

int start(){
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;

  //LOGIC
  if (OrderType() == OP_BUY){
   if (Bid - OrderOpenPrice() > tstartPipDelta){
    //START TRAILING
    if(OrderStopLoss() > 0){
     if (Bid - OrderStopLoss() > tslPipDelta){
      ModifySL(Bid - tslPipDelta);
     }
    }else{
     ModifySL(Bid - tslPipDelta);
    }
   }
  }else if (OrderType() == OP_SELL){
   if (OrderOpenPrice() - Ask > tstartPipDelta){
    //START TRAILING
    if(OrderStopLoss() > 0){
     if (OrderStopLoss() - Ask > tslPipDelta){
      ModifySL(Ask + tslPipDelta);
     }
    }else{
     ModifySL(Ask + tslPipDelta);
    }
   }
  }
 }

 return(0);
}

以上純粹興趣分享和介紹,報讀我們的程式交易課程並不需要認識和學習如何編程,我們是教已編好的程式在市場上獲利的方法和技巧,程式的使用方法比編程更重要,歡迎了解我們的簡介活動或入門班。

請即報讀

自動交易程式課程 (全方位課程)