這個EA為何沒產生交易?求救

Kevin Z  

下面這段EA 是我從 MT5書籍(外匯王) 下載的

 但我把這段EA 做策略測試時 卻發現沒有一次交易

請問題是出在哪邊呢?

(這段EA交易策略是當 收盤漲上某MA就買進,當收盤跌破某MA就賣出)


//+------------------------------------------------------------------+
//|                                                                                                               Simple Expert Advisor |
//+------------------------------------------------------------------+

// Input variables
input double TradeVolume=0.1;
input int StopLoss=100;
input int TakeProfit=325;
input int MAPeriod=10;


// Global variables 
bool glBuyPlaced, glSellPlaced;


// OnTick() event handler
void OnTick()
{
        bool blOrder;//儲存OrderSend()函數的傳回值
        
// Trade structures
        MqlTradeRequest request;
        MqlTradeResult result;
        ZeroMemory(request);

        
        // Moving average
        double ma[];
        ArraySetAsSeries(ma,true);
        
        int maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle,0,0,1,ma);
   
   
   // Close price
   double close[];
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,0,0,1,close);
   
   
   // Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
   
   double currentVolume = 0;
   if(openPosition == true) currentVolume = PositionGetDouble(POSITION_VOLUME);
   
   
   // Open buy market order
   if(close[0] > ma[0] && glBuyPlaced == false && (positionType != POSITION_TYPE_BUY || openPosition == false))
   {
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_BUY;
                request.symbol = _Symbol;
                request.volume = TradeVolume + currentVolume;
                request.type_filling = ORDER_FILLING_FOK;
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                blOrder = OrderSend(request,result);
                
                // Modify SL/TP
                if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
                {
                        request.action = TRADE_ACTION_SLTP;
                        
                        do Sleep(100); while(PositionSelect(_Symbol) == false);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        
                        if(StopLoss > 0) request.sl = positionOpenPrice - (StopLoss * _Point);
                        if(TakeProfit > 0) request.tp = positionOpenPrice + (TakeProfit * _Point);
                        
                        if(request.sl > 0 && request.tp > 0) blOrder = OrderSend(request,result);
                        
                        glBuyPlaced = true;
                        glSellPlaced = false;
                } 
   }
   
   
   // Open sell market order
   else if(close[0] < ma[0] && glSellPlaced == false && positionType != POSITION_TYPE_SELL)
   {
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_SELL;
                request.symbol = _Symbol;
                request.volume = TradeVolume + currentVolume;
                request.type_filling = ORDER_FILLING_FOK;
                request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
                request.sl = 0;
                request.tp = 0;
                request.deviation = 50;
                
                blOrder = OrderSend(request,result);
                
                // Modify SL/TP
                if((result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) && (StopLoss > 0 || TakeProfit > 0))
                {
                        request.action = TRADE_ACTION_SLTP;
                        
                        do Sleep(100); while(PositionSelect(_Symbol) == false);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
        
                        if(StopLoss > 0) request.sl = positionOpenPrice + (StopLoss * _Point);
                        if(TakeProfit > 0) request.tp = positionOpenPrice - (TakeProfit * _Point);
                        
                        if(request.sl > 0 && request.tp > 0) blOrder = OrderSend(request,result);
                        
                        glBuyPlaced = false;
                        glSellPlaced = true;
                } 
   } 
}

陳柏昌  

要看你的專家 與 日誌 那邊的訊息 才能推敲狀況與原因

Sleep 放大到1000試試看 有些模擬伺服器 反應速度很慢

Kevin Z  

謝謝告知日誌可以查問題

我看了他說 

"unsuported trade filling mode" 於是我把

request.type_filling = ORDER_FILLING_FOK; 改成 request.type_filling = ORDER_FILLING_IOC;


就能有交易執行了
我模擬帳戶用 IC Markets 他們客服人員說 他們是有FOK 但MT5平台可能不能用FOK

想問一下大家都是用交易類型FOK 還是 ICO呢??
Ziheng Zhuang  

unsuported trade filling mode  这个问题很麻烦。

如何自动匹配symbol filling ,order filling ,以及trade execution之间的关系? 非常复杂。

幸亏标准库已经解决了,用标准库中的开仓函数

#include <Trade\Trade.mqh>

不懂怎么用,参考code base 的别人的EA。

Kevin Z  
Ziheng Zhuang:

unsuported trade filling mode  这个问题很麻烦。

如何自动匹配symbol filling ,order filling ,以及trade execution之间的关系? 非常复杂。

幸亏标准库已经解决了,用标准库中的开仓函数

不懂怎么用,参考code base 的别人的EA。

謝謝提供方向

研究一下還是不太懂

目前可能暫時用ICO模式

等到程式功力提升再回來看

謝謝