標籤

二元樹 (1) 字串常數池 (1) 投資 (3) 每月損益 (37) 例外處理 (1) 泛型 (2) 股票 (15) 指標 (5) 英文 (8) 基本資料型別 (1) 期貨 (1) 程式交易 (10) 量化投資 (5) 亂亂寫 (3) 概念 (3) 資料結構 (3) 演算法 (3) 數學 (3) 轉型 (1) AMA (1) ArrayList (1) assert (1) BeautifulSoup (1) C/C++ (8) casting (1) ClassCastException (1) classpath (1) Collection (4) Comparable (1) comparTo() (1) constructor (1) database (3) Debian (1) Dropbox (2) EA (2) enum (1) equals() (2) exception (3) extends (1) ffmpeg (1) final (1) Git (1) HashMap (1) HashSet (1) hasNext() (1) HTS (3) instanceof (1) IS-A (1) Iterator (1) JAVA (43) length (1) Linux (31) List (1) Mac (6) Map (1) ML (2) MT4 (6) MySQL (2) next() (1) NullPointerException (1) Number (1) Numpy (2) OpenCart (1) OpenCV (3) OSX (1) overloading (1) overriding (3) pandas (2) PHP (8) PriorityQueue (1) Python (11) Queue (1) random() (1) reverse() (1) Samba (1) SCJP (21) sqrt() (1) synchronized (1) talib (1) ufw (1) uTorrent (1) var-args (2) VHF (1) vim (2) Yhoo知識+ (4)

2016年3月6日 星期日

MT4內建的MACD EA程式註解

//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

input double TakeProfit    =50;   //停利點數
input double Lots          =0.1;  //每次下單0.1口
input double TrailingStop  =30;   //獲利達30點時,啟動移動停利
input double MACDOpenLevel =3;    //MACD快線在0軸上下3點時,才可觸發多空單
input double MACDCloseLevel=2;    //MACD快線在0軸上下2點時,才可觸發平倉
input int    MATrendPeriod =26;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double MacdCurrent,MacdPrevious;
   double SignalCurrent,SignalPrevious;
   double MaCurrent,MaPrevious;
   int    cnt,ticket,total;
//---
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
   if(Bars<100){
      Print("bars less than 100");    //K棒小於100根,程式退出
      return;
     }

   if(TakeProfit<10){
      Print("TakeProfit less than 10");   //輸入停利小於10點,程式退出
      return;
     }
//--- to simplify the coding and speed up access data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);   //抓目前K棒的MACD快線值
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);    //抓上一根K棒的MACD快線值
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);   //抓目前K棒的MACD慢線值
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);    //抓上一根K棒的MACD慢線值
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);      //抓目前K棒的MA值
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);     //抓上一根K棒的MA值

   total=OrdersTotal();   //取得帳戶所有單子數量
 
   if(total<1){
      //--- no opened orders identified
      if(AccountFreeMargin() < (1000*Lots)){    //如果原始保證金小於 1000*交易口數,結束程式(1口*1000 = 下1口的原始保證金)
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
      }

      //--- 偵測觸發buy單條件
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&     //如果MACD快線在0軸下,又與慢線金叉
         MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious){             //而且快線在0軸下3點以上,又均線上升
       
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);   //則下buy單,不停損,50停利
       
         if(ticket>0){    //若下buy單成功,印出buy單價格
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))    Print("BUY order opened : ",OrderOpenPrice());
          }else
            Print("Error opening BUY order : ",GetLastError());   //若失敗則印出錯誤訊息,結束程式
       
         return;
      }


      //--- 偵測觸發sell單條件
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&     //如果MACD快線在0軸上,又與慢線死叉
         MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious){                       //而且快線在0軸上3點以上,又均線下降{
       
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);    //則下sell單,不停損,50停利

         if(ticket>0){    //若下sell單成功,印出sell單價格
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))    Print("SELL order opened : ",OrderOpenPrice());
           }else
            Print("Error opening SELL order : ",GetLastError());    //若失敗則印出錯誤訊息,結束程式
          }
      //--- exit from the "no opened orders" block
      return;
     }


//--- 移動停損及停利
   for(cnt=0;cnt<total;cnt++){    //走訪所有訂單
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))   continue;
   
      if(OrderType()<=OP_SELL &&   // check for opened position
         OrderSymbol()==Symbol()){  // 如果是本商品
     
         //--- 如果是buy單
         if(OrderType()==OP_BUY){
         
            //--- 如果MACD在0軸上死叉,而且快線大於0軸3點以上
            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
               MacdCurrent>(MACDCloseLevel*Point)){
               //--- 平倉buy 單
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))    Print("OrderClose error ",GetLastError());
             
               return;
            }
         
            //--- 移動停利
            if(TrailingStop>0){     //如果有設停利啟動點
               if(Bid-OrderOpenPrice() > Point*TrailingStop){   //如果獲利超過30點
                  if(OrderStopLoss() < Bid-Point*TrailingStop){ //如果獲利大於停利點
                     //--- 動態變更停利點
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                   
                     return;
                  }
                }
            }

          }else{ // 如果是sell單

            //--- 如果MACD在0軸下金叉,且快線小於0軸3點以上
            if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point)){
               //--- 平倉sell單
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))    Print("OrderClose error ",GetLastError());
             
               return;
            }

            //--- 移動停利
            if(TrailingStop>0){   //如果有設停利啟動點
               if((OrderOpenPrice()-Ask) > (Point*TrailingStop)){   //如果獲利超過30點
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)){     //目前點數小於停利點
                     //--- 動態變更停利點
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                   
                     return;
                  }
                }
            }
          }
      }
    }
//---
  }
//+------------------------------------------------------------------+

1 則留言:

  1. Hello Everyone,

    For those of you who are interested in generating profits by autotrading FOREX,
    I would like to recommend a special forex autotrading solution.

    It's called EA Builder and it allows you to create custom Forex Indicators and Trading Strategies.

    The solution includes many features such as:
    * Custom Arrows and Alerts
    * Automated Trading System
    * Trade Just About Anything
    * Alerts
    * Money Management


    LEARN MORE: EA Builder

    回覆刪除