2

I tried to create an EA in mql4 which opens and close the trade position as per condition given, but it is not opening the trade after mating the conditions, EA Works till signal which shows buy and sell and after that nothing happens. How can I debug this?

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();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • each time you send `OrderSend`, same as modification and close/cancel requests, you need to check the result (success or not). If it failed - log the error to see the problem. Many different problems may happen here like requote, invalid volume, and much more. In this specific case - invalid stop loss/take profit, it must be real price not just the distance in pips – Daniel Kniaz Jan 05 '20 at 10:57

1 Answers1

0

Step 0:
check, whether your EA was launched inside MetaTrader4 Terminal with an active permission to actually do trades.

Step 1:
check the code, for having at least some elementary self-debugging tools ( GetLastError() and Print() are way better than intermittent and self-destroying, i.e. having Zero-depth of history GUI-text inside but a last visible Comment()

Step 2:
Analyze the logs, where all printed details will help you trace the root cause ( Broker-rejections, closed Market, flawed price-levels and many of the possible reasons for an OrderSend()-call to get rejected )

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
                                                              )
                                               );
                                        // */
        }
halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92