Orders are in the air(Backtesting) / EA opens an order, while the conditions are not true!

 

Hello,

iam starting to write my first EA. But since the beginning, there's a strange behavior of my EA.


Ok my first problem is, that orders are opened in the air, while there is no trend..



and my second problem is, the order is opened while the conditions are FALSE!!

void TechnicalAnalysis2x13()
{
    if ((iSAR(NULL, PERIOD_M15,0.02,0.02,1) < Ask) && (iRSI(NULL, PERIOD_M15,7,PRICE_CLOSE,1) < 30))
    {
        IfOrderDoesNotExist8();
        
    }
}

void IfOrderDoesNotExist8()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        BuyOrder5();
        
    }
}

void BuyOrder5()
{
    double SL = Ask - BuyStoploss5*PipValue*Point;
    if (BuyStoploss5 == 0) SL = 0;
    double TP = Ask + BuyTakeprofit5*PipValue*Point;
    if (BuyTakeprofit5 == 0) TP = 0;
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, BuyLots5, Ask, 4, 0, 0, "My Expert", 1, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, BuyLots5, Ask, 4, SL, TP, "My Expert", 1, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
            
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}

The value of

(iSAR(NULL, PERIOD_M15,0.02,0.02,1) < Ask)

is round about 1.377~ and higher than the Ask price.


Hope you can help me!


Thank you

 
(iSAR(NULL, PERIOD_M15,0.02,0.02,1) > Ask)

Have you tried reversing the logic? Sometimes it helps.


You could try other part also since I think it is just logic problems and nothing much.

What I see is just common logic mistakes.

 

Iam sitting here for hours and can't find a mistake..

I have attached the SourceCode, so maybe someone can have a look inside and tell me what's wrong with it.

The Function is very easy.

Open a buy position if the SAR Indicator is below the market price and RSI is below 30.

Open a sell position if the SAR Indicator is above the market price and RSI is above 70.


Thank you!


DoJo
Files:
dojo_1.mq4  5 kb
 

The code is too complicated, symplify it


if( Buy == 0 )
  {
 if ((iRSI(0,15,7,0,1) < 30)&& (iRSI(0,15,7,0,0) < 30) &&        
      (iSAR(0,15,0.02,0.02,1) < Ask)  && (iSAR(0,15,0.02,0.02,0) < Ask))
     {
      int ticket = -1;
    if ( ticket == -1) ticket = OrderSend(Symbol(), OP_BUY, BuyLots5, Ask, 4, 0, 0,   "My Expert", 1, 0, Blue);
    else               ticket = OrderSend(Symbol(), OP_BUY, BuyLots5, Ask, 4, 0, 0, "My Expert", 1, 0, Blue);
      } } 
 

Simplification usually will not do any good unless the code is working like it should.

Once it is working, then you can start optimizing the code to make it more efficient. :)

Always happen to me by the way.

 

I think the EA is working the way it should :

https://charts.mql5.com/4/492/eurusd-m15-fxpro-financial-services.png

It is programmed for 15 Mn indicator, check TF, it should be 15 Mn or below : 5 Mn or 1Mn.

I agree deymacro, but often, one has to symplify the code to find where is the problem, and for us, the reader, it's easier to understand if the code is easy to read, be lazy ... :-)

 

Make sure that the SAR settings on the chart are the same as in the EA

if ((iSAR(NULL, PERIOD_M15,0.02,0.02,1) < Ask) && (iRSI(NULL, PERIOD_M15,7,PRICE_CLOSE,1) < 30))

I bet the SAR on your chart is 0.02,0.2 .

 
DoJo:

Iam sitting here for hours and can't find a mistake..

I have attached the SourceCode, so maybe someone can have a look inside and tell me what's wrong with it.

The Function is very easy.

Open a buy position if the SAR Indicator is below the market price and RSI is below 30.

Open a sell position if the SAR Indicator is above the market price and RSI is above 70.

Thank you!

DoJo

You should at least attempt basic debugging before you ask others to do it for you. Use Print().

   if ((iSAR(NULL, PERIOD_M15,0.02,0.02,1) < Ask) && (iRSI(NULL, PERIOD_M15,7,PRICE_CLOSE,1) < 30))
    {
     Print("Ask = ",Ask);
     Print("iSAR = ",iSAR(NULL, PERIOD_M15,0.02,0.02,1));
     Print("iRSI = ",iRSI(NULL, PERIOD_M15,7,PRICE_CLOSE,1));

     IfOrderDoesNotExist8();
    }
 
DoJo:

Hello,

iam starting to write my first EA. But since the beginning, there's a strange behavior of my EA.


Ok my first problem is, that orders are opened in the air, while there is no trend..


...

Thank you

Chart is drawn from bid price. Your buy order is open from Ask price. You are running the Strategy Tester with current spread on week end. Which seems to be around 20 pips.
 
angevoyageur:
Chart is drawn from bid price. Your buy order is open from Ask price. You are running the Strategy Tester with current spread on week end. Which seems to be around 20 pips.


I didn't notice the dates on the chart.

I wish that people would actually make it clear when they are referring to a test and not live charts.

 

Come on now GumRai they all know you have psychic powers to mind read them, no point pretending ;)

Reason: