【MT4 程式(Program)專業代寫服務】
LMAX Exchange – FX trading




免責聲明 »
凡本網站註明來源網絡或其他網站,均為轉載稿,本網轉載出於傳遞更多信息之目的,並不意味著贊同其觀點或證實其內容的真實性,也不構成任何投資建議。對於訪問者根據本網站提供的信息所做出的一切行為,本網站不承擔任何形式的責任。本網站僅提供經濟信息,並僅供參考;亦不提供證券、基金、銀行、保險、金融任何業務與服務;不推薦任何相關商品和服務;不與任何人簽署任何海外證券投資協議,不進行海外金融產品交易,不接受任何人投資資金。

2013/10/08

EA自動智能交易系統(範例說明)

對於程式語言一直抱持排斥的心態,所以無法好好地靜下心學好

交易已經進展到『程式自動交易』非走不可的階段,看看如何能快速又簡單進入

先來學習別人的,多看並利用一些好工具,來簡化學習的曲線

//+------------------------------------------------------------------+
//|
                        Moving Average.mq4 |                                                                      
//|                           http://www.imt4.com |
//+------------------------------------------------------------------+

#define MAGICMA  20050610
extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;
extern double MovingPeriod       = 10;
extern double MovingShift        =3;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma) 
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma) 
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
以下是註解
//+------------------------------------------------------------------+
//|                                      Moving Average.mq4 |                    
//|                                       http://www.imt4.com |
//+------------------------------------------------------------------+
#define MAGICMA  20050610 //定義本EA操作的訂單的唯一標識號碼
extern double Lots               = 0.1;//每單的交易量
extern double MaximumRisk        = 0.02;//作者定義的最大風險參數
extern double DecreaseFactor     = 3;//作者定義的參數,作用要看程序中的用法
extern double MovingPeriod       = 10;//EA中使用的均線的周期
extern double MovingShift        =3;//EA中使用的均線向左的K線偏移量
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)//函數作用,計算當前持倉訂單的數量
  {
   int buys=0,sells=0;//定義兩個臨時變量,準備用於後面的多空訂單的個數計算
//----
   for(int i=0;i<OrdersTotal();i++)//循環檢測當前的訂單隊列
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持倉單的每一個訂單位置
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根據訂單位置,比較是否是當前K線商品以及訂單唯一標識號是否和本程序設置的一致(用於避免EA誤操作其他程序控制的持倉單)
        {
         if(OrderType()==OP_BUY)  buys++;//找到符合條件的持倉單後,如果是多單,則臨時變量buys增加1
         if(OrderType()==OP_SELL) sells++;//找到符合條件的持倉單後,如果是空單,則臨時變量sells增加1
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);//本函數返回查詢計算結束時的持倉單的個數。
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()//函數目的,根據要求 計算出訂單交易量
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total 歷史出場訂單的個數
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通過風險系數的計算獲得當前入場單應該采用的交易量
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循環查詢出場單隊列
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;//
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;//循環計算所有出場虧損單的虧損總和
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//如果虧損額大於1,則下一入場單的交易量修正為新的計算結果。
     }
//---- return lot size
   if(lot<0.1) lot=0.1;//如果計算出的交易量小於帳戶最小手數0.1,則下一入場單的交易手數使用0.1作為交易量
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()//檢查入場條件的情況並作處理
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;//如果當前K線持倉量大於1,說明不是K線的開盤時間點,則直接返回 否則是K線第一個價格,則繼續下面的過程
//---- get Moving Average
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//獲得當前的均線數值
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma)  //如當前K開盤價大於均線,而前一K收盤價小於均線,則發出入場多單
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma)  //如當前K開盤價小於均線,而前一K收盤價大於均線,則發出入場空單
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()//檢查出場條件的情況並作處理
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average
   ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);//如果持倉是多單,則當當前K開盤價小於均 線,而前一K收盤價大於均線,則發出平倉指令
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White););//如果持倉是空單,則當當前K開盤價大於 均線,而前一K收盤價小於均線,則發出平倉指令
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()//主循環過程
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+