EA working as expected in Strategy Tester but not in Real Market?

 

When I am testing this EA at strategy tester it's working as expected (Except it's opening 4 orders at the starting instead of 3).

The expectation is:

It'll Open 3 trades at first. Then if all these trades got closed and the same condition is still valid, it'll open 1 trade only.

When it'll get the opposite condition, it'll again open 3 trades and then continue to open 1 trade if that condition remains valid.

But when I am attaching it to real chart, it's not working. Then I'm getting 'Buy_Ticket_3=0'.

Buy_Ticket_3 will store the Ticket number of the 3rd order opened very first when a new condition occurred.

Regards

Files:
history_ea.mq4  20 kb
 
Arav007:

When I am testing this EA at strategy tester it's working as expected (Except it's opening 4 orders at the starting instead of 3).

The expectation is:

It'll Open 3 trades at first. Then if all these trades got closed and the same condition is still valid, it'll open 1 trade only.

When it'll get the opposite condition, it'll again open 3 trades and then continue to open 1 trade if that condition remains valid.

But when I am attaching it to real chart, it's not working. Then I'm getting 'Buy_Ticket_3=0'.

Buy_Ticket_3 will store the Ticket number of the 3rd order opened very first when a new condition occurred.

Regards


You may want to check a few things in your code bellow :

int History_Calling(string symbol,int magic)
{
int i=0;
int hstTotal=OrdersHistoryTotal();
int counter = 0;
int ticketNumber = 0; 
 for(i=0;i<hstTotal;i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))              // what is the value of ticket number if OrderSelect() fails ?
      if(OrderType() < 2)// this weeds out any pending orders.  // what is the value of ticket number if this is wrong ?
       if(OrderSymbol()== symbol)
        if(OrderMagicNumber() == magic)
         if(OrderCloseTime() > counter)
           {
            counter=OrderCloseTime();    //each time it finds a greater closing time it
            ticketNumber = OrderTicket();//overwrites the counter and the ticket number. 
            Print("The Oder Ticket:",ticketNumber);   
           }
    }
     //now it has the ticket number of the highest closing time in seconds since 1970
     //you can get whatever info you want about that particular ticket.
     
    OrderSelect(ticketNumber,SELECT_BY_TICKET);// when you select by ticket you do not need to specify mode trades or mode history.
    int type = OrderType();// 0 is a buy and 1 is a sell
    string closeTime= TimeToString(OrderCloseTime());
    double closePrice = OrderClosePrice();     
    Print("Ticket number ",ticketNumber," of type ",type," was closed at ",closeTime," at a price of: ",closePrice);
    return(ticketNumber);

}

If OrderSelect fails you're stuck with ticketNumber=0 and no error message to tell you about it.

Variable counter is of int type and you compare datetime from OrderCloseTime with it. Apples with apples, remember ?

So every time your loop fails you're stuck at ticketNumber=0;

You need to rethink your loop.

If you need help with that, let us know.

Hope it helps.

Cheers

 
thrdel:


You may want to check a few things in your code bellow :

If OrderSelect fails you're stuck with ticketNumber=0 and no error message to tell you about it.

Variable counter is of int type and you compare datetime from OrderCloseTime with it. Apples with apples, remember ?

So every time your loop fails you're stuck at ticketNumber=0;

You need to rethink your loop.

If you need help with that, let us know.

Hope it helps.

Cheers


Hello,

Thanks a lot for responding. Actually I have copied it from another thread of mine where someone posted this.

And honestly, I hadn't thought about the failure of 'OrderSelect'.

I have been testing this EA in demo for a while and it's acting strangely!

Sometime it is opening 'Three' Orders altogether. But sometime it is opening just One or Two orders and showing 'Error' in opening the rest of the order(s)!

Not understanding why such is happening because if there was problem with 'OrderSend' then it'd had never sent 'Three' Orders successfully.

Yes, your Help will be really appreciated.

Regards

 
Arav007:


Hello,

Sometime it is opening 'Three' Orders altogether. But sometime it is opening just One or Two orders and showing 'Error' in opening the rest of the order(s)!

Not understanding why such is happening because if there was problem with 'OrderSend' then it'd had never sent 'Three' Orders successfully.

Yes, your Help will be really appreciated.

Regards

Here is how the magic works and if you follow the code with me you'll understand it :

1. Here are your global variables declared :

int Buy_Ticket_3,Sell_Ticket_3,Buy_Ticket_4,Sell_Ticket_4;

You have 1 guess to tell me what value is stored at this point in each of this variables ?

Exactly, it's zero. Now looking down into the code,EA works out indicators and signals and comes up with a buy signal.

Next thing is to close any sell orders, right ? After closing all sell orders, it comes to this :

int ticketNumber=History_Calling(Symbol(), MagicNumber);

In History_Calling, ticketNumber=0; thats the start.

When OrdersSelect fails in History_Calling, next thing it does is to, guess what, Select order by ticketNumber which is still zero .

So History_Calling is passing zero to global ticketNumber ! Then EA compares ticketNumber =0 with Buy_Ticket_3 = 0 :

if(ticketNumber==Buy_Ticket_3 || ticketNumber==Buy_Ticket_4)

Remember the variable Buy_Ticket_3 && 4 didn't change so far from the start so they are still zero.

So, by the power of magic, zero = zero, so we have condition for :

  Print("The Buy Condition is Still Valid, so time to Open a Single Buy Order");
   One_Buy_Orders(LotSize,MagicNumber,TakeProfit_1,TakeProfit_2,TakeProfit_3,StopLoss,Slippage);

Exactly, a buy order. Not only that but since ticket number is still zero and Sell_Ticket_3 is still zero

if(ticketNumber==Sell_Ticket_3 || ticketNumber==Sell_Ticket_4)

Bingo, we have conditions for

Three_Buy_Orders(LotSize ,MagicNumber,TakeProfit_1,TakeProfit_2,TakeProfit_3,StopLoss,Slippage);

3 more buy orders order as well.

Now, Buy_Ticket4 & Buy_Ticket3 are different from zero and if History_Calling isn't working, ticket Number stays at zero and no more new buying is going to happen.

But when the indicators change, same scenario goes for sell orders.

So you see, you can have some orders when OrderSelect fails in History_Calling !

Hope it helps.

 
thrdel:

Here is how the magic works and if you follow the code with me you'll understand it :


So you see, you can have some orders when OrderSelect fails in History_Calling !

Hope it helps.


Thanks a Ton for such a Greatly Explained answer!

You are nothing but just a 'Great' teacher because you have said it all within a 'Single' post

which the others take at least 'Three' posts (Though most of them have the intention to teach me!).

Yes, now I have understood the reason how I'm getting orders even though there is something Wrong!

So what do you think if I add these conditions before, would it help to remove this 'Error' or you have a

better and reliable way to deploy this 'History Calling' strategy?

if (ticketNumber>0 &&  (Buy_Ticket_3>0||Buy_Ticket_4>0))

{

 if(ticketNumber==Buy_Ticket_3 || ticketNumber==Buy_Ticket_4)
{

...

}

}

Regards

 
Arav007:


Thanks a Ton for such a Greatly Explained answer!

Regards

|First of all,I'm not a teacher and I have no intention to become one. We all get some and give some if you know what I mean.

About the History_Calling, here is what I would change it to :

int History_Calling()
  {
   if(OrdersHistoryTotal()<1)
      {
      Print("No orders in history");
      return(0); // if no orders in history return ticket=zero
      }
   int i=0,hstTotal=OrdersHistoryTotal();
   recentClosedOrderTime=0;
   recentClosedTicket=-1;
   for(i=0;i<hstTotal;i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) // if order select fails , print to let me know and take it from the top
        {
         Print("Orders select failed in History_Calling with error : "+IntegerToString(GetLastError(),0,0));
         break;
        }
      if(OrderType()==OP_BUY || OrderType()==OP_SELL) 
         {
         if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber)
            {
            if(OrderCloseTime()>recentClosedOrderTime)
               { 
               recentClosedOrderTime=OrderCloseTime();                               //each time it finds a greater closing time 
               recentClosedTicket=OrderTicket();                                    //overwrites the counter and the ticket number. 
               }
            }
         }
      else continue;
     }
//now it has the ticket number of the highest closing time in seconds since 1970
//you can get whatever info you want about that particular ticket.
   if(hstTotal>0 && recentClosedTicket==-1)
     {
      Print("No order selected in History_Calling,recentClosedTicket is"+IntegerToString(recentClosedTicket,0,0));
      return(recentClosedTicket);
     }
   if(recentClosedTicket>-1)
     {
      if(OrderSelect(recentClosedTicket,SELECT_BY_TICKET))// when you select by ticket you do not need to specify mode trades or mode history.
        {
         string type= "sell";
         if(OrderType()==OP_BUY)type="buy";// 0 is a buy and 1 is a sell
         string closeTime=TimeToString(OrderCloseTime());
         double closePrice=OrderClosePrice();
         Print("Ticket number ",recentClosedTicket," of type ",type," was closed at ",closeTime," at a price of: ",closePrice);
        }
      else Print("Orders select failed in History_Calling (2)with error : "+IntegerToString(GetLastError(),0,0));
     }
   return(recentClosedTicket);

  }

And make this 2 global variables :

//---
datetime recentClosedOrderTime=0;
int recentClosedTicket=-1;

And then you may want to add a check here:

         int ticketNumber=History_Calling(Symbol(),MagicNumber);
         
         if(ticketNumber<0)// check for errors on order selected from history
            {
            Print("No orders in history or no order selected !");
            return;
            }

         if(ticketNumber==Buy_Ticket_3 || ticketNumber==Buy_Ticket_4)

and here :

         int ticketNumber=History_Calling(Symbol(),MagicNumber);
         
         if(ticketNumber<0)// check for errors on order selected from history
            {
            Print("No orders in history or no order selected !");
            return;
            }
         if(ticketNumber==Sell_Ticket_3 || ticketNumber==Sell_Ticket_4)

because you don't want it to continue if there are no orders in history or there was a problem selecting the orders, right ?

Also, since magic number is global (available to all functions ) you can exclude it from History_Calling call and you can also use Symbol() to make it simple.

Something like this :

int History_Calling()

if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)continue; // if not this EA's order, continue from the top

There were other errors and warnings throughout the code and I attached the modified one that compiles with no errors.

You can compare it with yours and see the changes .

Hope it helps

Cheers

Files:
 
thrdel:

because you don't want it to continue if there are no orders in history or there was a problem selecting the orders, right ?


Hello, Thanks a lot for the corrections.

Actually it's not like that if there is no order in the History, the EA wont continue. Because when I'll attach it for the first time,

it has to open trades. Yes, when it fails to select order from history correctly, it should try again.

I have tested your given one but the same problem with opening orders. As there is no order in the History initially,

it's not opening any order. So I placed the opening order function at the starting and it opened orders. Then I removed it again.

But when those opened orders got closed, it's not opening any other orders. This is the problem with my EA attached in OP.

I hope you'll take a look into it.

Regards

 
Arav007:

Hello, Thanks a lot for the corrections.

I hope you'll take a look into it.

Regards

Hi,

I Haven't got the time to actually test it until now and, yes, I can see some stuff that may create problems.

For instance, when you place the 3 orders and they hit the stop, the terminal might close them in reverse order 3,2,1 instead of 1,2,3 and then, even if they were closed at the same time, the last ticket closed isn't Buy_Ticket_3 but Buy_Ticket_1.

Since the One_Buy wasn't triggered yet, the last ticket will not match Buy_Ticket_3 or 4, them EA will not place more orders

if(ticketNumber==Buy_Ticket_3 || ticketNumber==Buy_Ticket_4)
           {
            Print("The Buy Condition is Still Valid, so time to Open a Single Buy Order");

Then it goes to the next condition :

if(ticketNumber==Sell_Ticket_3 || ticketNumber==Sell_Ticket_4)
           {
            Print("Last order was Sell and Now Buy condition is met, so time to Open a Three Buy Order");

and finds that the ticket number doesn't match either of them, so it can't place the one order.

As I said, I didn't run a test until now and indeed there are a couple of things needed to be worked around to get this baby going.

I will post the code bellow for you to have a look.

The code compile with no problem and hopefully places the orders as you want it to. Now you can test, optimize, improve on it or whatever you want to do.

If I changed something that seems important to you, let me know.

You may want to check the conditions for buy and sell that I changed to :

if((recentClosedOrderType==1 || Buy_Ticket_3==0) && iOpenOrders_Buy<3)//last closed order was sell or EA just started

What this says is : if the most recent closed order in history was a sell or if Buy_Ticket_3 is zero (the EA just started and no buy orders have been placed yet) and you don't have the 3 orders open yet, place the first set of 3 orders.

The second condition is similar :

 if(recentClosedOrderType==0 && iOpenOrders_Buy<1)//last open order was buy and no open orders 

Kind of the same for sell orders as well.

Anyways, check the code, there may be other things that need some work in there but for now I guess this should be enough info for one step.

Just want to mention, when you test it, you will have a lot of single orders between EMA ans SMA crosses, right ? Three orders only when a new cross occurs and continue with one order after that.

My test on AUDUSD / M30 from July 2013 until today April 12 2014 looks like this :

Let me know if you find better settings, maybe on different pair.

Hope it helps and good luck.

Files:
 
thrdel:

Hope it helps and good luck.


Hello,

There is no word that I can use for Thanking you.

You have taken your precious time out for solving my problem! You are really a Kind Man.

I have tested it on strategy tester and it's working as Expected. Though I want to report

you finally after observing it on a Live market. So please be there till then. I hope it'll

come out positively.

BTW, did you use the same EA (HistorykEAg3) for testing purpose? Because my testing

isn't giving the same result as like you. Probably my settings for Strategy Tester isn't

optimized enough to get the best possible accurate result. The modeling quality was 48.45% Only!

King Regards

 
Arav007:


Hello,

King Regards

You're welcome.

One can only hope that there will eventually be a community where people will come together and share their successes and knowledge more actively.

Something like most open source projects that are taking up now.

Yes,I used the same EA with the following parameters :

extern double LotSize       =0.1;
extern int    MagicNumber   =198912;
extern int    TakeProfit_1  = 15;
extern int    TakeProfit_2  = 25;
extern int    TakeProfit_3  = 60;
extern int    StopLoss      = 40;
extern double TrailingStart =10;
extern double TrailingStep  =2;
extern double TrailingStop  =10;
extern double BreakEvenStart=5;
extern double BreakEven     =1;
extern int    Slippage      =3;
extern double Value_Diff    = 0.0003;
extern int    Fast_MA_Value =21;
extern int    Slow_MA_Value =50;

on 30 min chart. There may be some small differences depending on the broker location but shouldn't be major.

Try those settings and see if you get similar results.

If you do, then it may be worth spending some time and find the best settings for EMA and SMA, break even, trailing stop, etc.

The hardest part wasn't the coding but finding the most effecting pair, time frame and settings for it.

Your job is to do the hard part and maybe get others involved to help.

You do realize it's a time consuming enterprise, right ?

Wish you best of luck and hope to hear good news from you soon.

Cheers

 
thrdel:

Wish you best of luck and hope to hear good news from you soon.

Cheers


Hello,

I'm agree that we should come up with ideas and help one other to make a better trading community.

I'm always there for that.

Anyway, it's the Local New Year here. So I couldn't have a deeper look into everything. Though ran the

updated EA for a while and got a problem. Couldn't analyze it for time so posting the screenshots.

It has opened 4 trades at a time in EURUSD! So it would probably open 4 Buy orders too at time.

And while opening New Buy Orders, it's not closed an existing Sell Order! There was an error:6.

Please have a look. Hopefully I'd be able to come with a decent report by tomorrow.


Regards