首页 > 解决方案 > 智能交易系统未开仓

问题描述

我试图在 mql4 中创建一个 EA,它根据给定的条件打开和关闭交易头寸,但在匹配条件后它没有打开交易,EA 一直工作到显示买入和卖出的信号,之后什么都没有发生。我该如何调试呢?

void CloseBuyPosition()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        string Cp = OrderSymbol();

        if (_Symbol == Cp)

            if (OrderType() == OP_BUY)
            {

                OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL);

            }
    }
}
void CloseSellPosition()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        string Cp = OrderSymbol();

        if (_Symbol == Cp)

            if (OrderType() == OP_SELL)
            {

                OrderClose(OrderTicket(), OrderLots(), Ask, 3, NULL);

            }
    }

}
void OnTick()
{

    string signal = "";
    double Sar = iSAR(_Symbol, _Period, 0.02, 0.2, 0);

    if (Sar < Low[1] && Open[1] < Close[1])
    {
        signal = "buy";
    }

    if (Sar > High[1] && Open[1] > Close[1])
    {
        signal = "sell";
    }

    if (signal == "buy" && OrdersTotal() == 0)

        OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, 20, 100, NULL, 0, 0, Green);

    if (signal == "sell" && OrdersTotal() == 0)

        OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red);

    Comment("The Signal is :", signal);

    if (Open[1] > Close[1] && OrdersTotal() > 0)

        CloseBuyPosition();

    if (Open[1] < Close[1] && OrdersTotal() > 0)

        CloseSellPosition();
}

标签: mql4algorithmic-tradingmetatrader4forex

解决方案


第 0 步:
检查您的 EA 是否在 MetaTrader4 终端中启动并具有实际进行交易的有效权限。

第1步:
检查代码,至少有一些基本的自调试工具(GetLastError()并且Print()比间歇性和自毁更好,即内部具有零深度历史GUI文本但最后可见Comment()

第 2 步:
分析日志,其中所有打印的详细信息将帮助您追踪根本原因(经纪人拒绝、关闭市场、有缺陷的价格水平以及许多可能导致OrderSend()-call 被拒绝的原因)

int  OrderSend( string   symbol,              // symbol
                int      cmd,                 // operation
                double   volume,              // volume
                double   price,               // price
                int      slippage,            // slippage
                double   stoploss,            // stop loss <------------ PRICE-DOMAIN levels, not INT-s
                double   takeprofit,          // take profit <---------- PRICE-DOMAIN levels, not INT-s
                string   comment = NULL,      // comment
                int      magic = 0,           // magic number
                datetime expiration = 0,      // pending order expiration
                color    arrow_color = clrNONE// color
                );

void OnTick()
{
 /* string signal = "";
    double Sar = iSAR( _Symbol, _Period, 0.02, 0.2, 0 );

    if ( Sar <  Low[1] && Open[1] < Close[1] )      signal = "buy";
    if ( Sar > High[1] && Open[1] > Close[1] )      signal = "sell";

    if ( signal ==  "buy" && OrdersTotal() == 0 )   OrderSend( _Symbol, OP_BUY,  0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
    if ( signal == "sell" && OrdersTotal() == 0 )   OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );

    Comment( "The Signal is :", signal );

    if ( Open[1] > Close[1] && OrdersTotal() > 0 )  CloseBuyPosition();
    if ( Open[1] < Close[1] && OrdersTotal() > 0 )  CloseSellPosition();
    */

    static int count_of_NOPs =  0;
    if ( OrdersTotal() == 0 )
    {    if (  Open[1] < Close[1]
            &&  Low[1] > iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
               ) {  
                    int rc = OrderSend( _Symbol, OP_BUY,  0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
                    Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_BUY  ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
                                          rc,
                                          GetLastError()
                                          )
                           );
                    count_of_NOPs =  0; //------------------------------ .RESET
                    return; //------------------------------------------^ J.I.T./RET
                    }
         if (   Open[1] > Close[1]
             && High[1] < iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
                ) { 
                    int rc = OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );
                    Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_SELL ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
                                          rc,
                                          GetLastError()
                                          )
                           );
                    if ( )
                    count_of_NOPs =  0; //------------------------------ .RESET
                    return; //------------------------------------------^ J.I.T./RET
                    }

         /* OTHERWISE: */
                    Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID NOP... (for %d time)",
                                        ++count_of_NOPs
                                          )
                           );
                    // */
        }
    else
    {    if ( Open[1] > Close[1] ) {    Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseBuyPosition()...",
                                                              OrdersTotal()
                                                              )
                                               );
                                        CloseBuyPosition();
                                        count_of_NOPs =  0; //---------- .RESET
                                        return; //----------------------^ J.I.T./RET
                                        }
         if ( Open[1] < Close[1] )  {   Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseSellPosition()...",
                                                              OrdersTotal()
                                                              )
                                               );
                                        CloseSellPosition();
                                        count_of_NOPs =  0; //---------- .RESET
                                        return; //----------------------^ J.I.T./RET
                                        }

        /* OTHERWISE: */
                                        Print( StringFormat( "HAVING %d ORDERS ACTIVE... DID NOP... (for %d time)",
                                                              OrdersTotal(),
                                                            ++count_of_NOPs
                                                              )
                                               );
                                        // */
        }

推荐阅读