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

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

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

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

2. 以浮盈追賺 (Trailing in Money)

以浮盈追賺是稍為複雜的追賺策略,需要適當計算出盈利,並在此之上再作追賺。原理是計算止損盈利目標,隨著盈利上升而推高止損目標,從而達至最佳利潤。以MQL4實現的話則會是以下方式:

  • 設定2個參數,啟動門檻$50,追賺距離$20

extern double TrailingStart = 50;
extern double TrailingDist = 20;

  • 先計算當刻的訂單浮盈,並儲存起來
    (浮盈可大概分3種: 全體浮盈、每邊浮盈、每單浮盈;
     這裡為了說明,選擇了每邊浮盈作結算,
     因此儲存格數為2,可因應情況作出調較)

int start(){
 //INIT EVERY TICK
 ArrayInitialize(currDollar, 0);

 //CALCULATE CURRENT GAIN
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() > 2) continue;
  //ACCUMULATE GAIN OF EACH ORDER
  currDollar[OrderType()] += OrderProfit()+OrderSwap()+OrderCommission();
 }
 //...
}

  • 計算好浮盈後,檢查盈利是否已達到啟動門檻
    (這裡有個小秘訣,剛好MQL4的BUY=0而SELL=1
     所以只需要 for loop 0-1 就可以檢查兩邊盈利)

int start(){
 //INIT EVERY TICK
 ...
 //CALCULATE CURRENT GAIN
 ...
 //LOOP PER SIDE
 for(int t = OP_BUY; t <= OP_SELL; t++){
  //CHECK IF START TRAILING
  if(currDollar[t] > TrailingStart){
   //START TRAILING
   //CONTINUE TRAILING
  }
  //CHECK IF CLOSE SIDE
 }

 return(0);
}

  • 如追賺距離不足,則增加最低盈利目標,緊貼現價,保障利潤

//START TRAILING
if(tslDollar[t] <= 0){
 tslDollar[t] = currDollar[t] - TrailingDist;
}
//CONTINUE TRAILING
else if(currDollar[t] - tslDollar[t] > TrailingDist){
 tslDollar[t] = currDollar[t] - TrailingDist;
}

  • 最後如市場稍為回調,就作結單,獲取利潤

//CHECK IF CLOSE SIDE
if(currDollar[t] < tslDollar[t]){
 CloseSide(t);
}

  • 而結單大概分為3個步驟: 結單、檢查已結單與否、重置盈利目標
    但結單並非本文重點,就先不多作解釋

int CloseSide(int type){
 //CLOSE SIDE ORDERS
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() != type) continue;
  //CLOSE
  double price = (type==OP_BUY?Bid:Ask);
  OrderClose(OrderTicket(), OrderLots(), price, 30, clrYellow);
 }
 //CHECK IF ANY SIDE ORDERS EXIST
 int count = 0;
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() != type) continue;
  //ADD
  count++;
 }
 //RESET TRAILING IF SIDE CLOSED
 if(count == 0){
  tslDollar[type] = 0;
 }
 return(0);
}

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

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

extern double TrailingStart = 50;  //Trailing Start in Money
extern double TrailingDist = 20;  //Trailing Distance in Money

double currDollar[2], tslDollar[2];

int init(){
 return(0);
}

int deinit(){
 return(0);
}

int CloseSide(int type){
 //CLOSE SIDE ORDERS
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() != type) continue;
  //CLOSE
  double price = (type==OP_BUY?Bid:Ask);
  OrderClose(OrderTicket(), OrderLots(), price, 30, clrYellow);
 }
 //CHECK IF ANY SIDE ORDERS EXIST
 int count = 0;
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() != type) continue;
  //ADD
  count++;
 }
 //RESET TRAILING IF SIDE CLOSED
 if(count == 0){
  tslDollar[type] = 0;
 }
 return(0);
}

int start(){
 //INIT EVERY TICK
 ArrayInitialize(currDollar, 0);

 //CALCULATE CURRENT GAIN
 for (int i = 0; i < OrdersTotal(); i++){
  //SELECT ORDER
  if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
  if(OrderSymbol() != Symbol()) continue;
  if(OrderType() > 2) continue;
  //ACCUMULATE GAIN OF EACH ORDER
  currDollar[OrderType()] += OrderProfit()+OrderSwap()+OrderCommission();
 }

 //LOOP PER SIDE
 for(int t = OP_BUY; t <= OP_SELL; t++){
  //CHECK IF START TRAILING
  if(currDollar[t] > TrailingStart){
   //START TRAILING
   if(tslDollar[t] <= 0){
    tslDollar[t] = currDollar[t] - TrailingDist;
   }
   //CONTINUE TRAILING
   else if(currDollar[t] - tslDollar[t] > TrailingDist){
    tslDollar[t] = currDollar[t] - TrailingDist;
   }
  }
  //CHECK IF CLOSE SIDE
  if(currDollar[t] < tslDollar[t]){
   CloseSide(t);
  }
 }

 return(0);
}

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

請即報讀

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