Download MetaTrader 5
To add comments, please log in or register
Unable to find the answer to your question? Use the search!
djmarak
9
djmarak 2014.04.18 18:52 

I made my own EA and Customer Indicator. The EA uses iCustom to call the Custom Indicator. As far as I can tell, both programs are working fine. If I set the Use Date in the strategy tester to just be for the last couple months, the EA opens trades no problem, but if I set it to a longer period of time, like the last year, it no longer finds those same trades it found before. Does anyone know why this happens? Does it have something to do with how the Strategy Tester pulls in historical data? I attached my files.

Thanks,
David

Keith Watford
Moderator
7990
Keith Watford 2014.04.18 19:53  

I don't know why it wouldn't open the trades, but you need to correct your code in the EA

You have no error reporting for the OrderSends and OrderModify - and there will be a lot of them

#define MAGICMA  20131111

input double Lots          =0.1;
input double MaximumRisk   =0.02;
double TakeProfit1;
double TakeProfit2;
double StopLoss;
double Spread;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
   if(lot<0.1) lot=0.1;
   return(lot);
  }

void OnTick()
  {
   //if(IsTradeAllowed()==false || Volume[0]>1)
      //return;
   if(CalculateCurrentOrders(Symbol())==0)CheckForOpen();
   else ManageOrder();
   }

void CheckForOpen()
  {
   TakeProfit1 = iCustom(NULL,1440,"Harmonic EA Indicator",3,0);
   TakeProfit2 = iCustom(NULL,1440,"Harmonic EA Indicator",4,0);
   StopLoss = iCustom(NULL,1440,"Harmonic EA Indicator",5,0);
   Spread = Ask - Bid;
      if(TakeProfit1 != 0 && TakeProfit1 < Bid) //Sell
     {
      OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,StopLoss+Spread,TakeProfit1+Spread,"",MAGICMA,0,Red);
      return;
     }
   if(TakeProfit1 != 0 && TakeProfit1 > Ask) //Buy
     {
      double position = LotsOptimized();
      OrderSend(Symbol(),OP_BUY,position,Ask,3,StopLoss-Spread,TakeProfit2-Spread,"",MAGICMA,0,Blue);
      return;
     }
  }
  
  // Move the stop and take partial profit if first target is reached
  void ManageOrder()
  {
   if(OrderType()==OP_BUY)
     {
      if(Bid > TakeProfit1-Spread)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TakeProfit2-Spread,3,White);
            OrderClose(OrderTicket(),OrderLots()/2,Bid,3,White);
         }
     }
   if(OrderType()==OP_SELL)
     {
      if(Ask < TakeProfit1+Spread)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TakeProfit2+Spread,3,White);
            OrderClose(OrderTicket(),OrderLots()/2,Bid,3,White);
         }
     }
  }

Why are you checking TakeProfit1 and using TakeProfit2 in the Order

     if(TakeProfit1 != 0 && TakeProfit1 > Ask) //Buy
     {
      double position = LotsOptimized();
      OrderSend(Symbol(),OP_BUY,position,Ask,3,StopLoss-Spread,TakeProfit2-Spread,"",MAGICMA,0,Blue);
      return;
     }

You don't do the same with the Sell

      if(TakeProfit1 != 0 && TakeProfit1 < Bid) //Sell
     {
      OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,StopLoss+Spread,TakeProfit1+Spread,"",MAGICMA,0,Red);
      return;
     }

Then later you check the position of the sell, too late, the TP will already have been hit and the order closed

   if(OrderType()==OP_SELL)
     {
      if(Ask < TakeProfit1+Spread)

But do you actually check the correct order? You don't select an order, so if the last order selected was not placed by the EA, you will be managing the wrong order

  void ManageOrder()
  {
   if(OrderType()==OP_BUY)
     {
     }
   if(OrderType()==OP_SELL)
     {
     }

You can't simply close half the lots.

What if the OrderLots is 0.5 and the minimum lot size is 0.1 and lot step 0.1. If you try to close half, you will be trying to close 0.25 lots and that will be rejected

OrderClose(OrderTicket(),OrderLots()/2,Bid,3,White);
djmarak
9
djmarak 2014.04.19 18:40  

Yeah, I noticed the mistake where I had TakeProfit1 instead of TakeProfit2. The idea is to close half the position when TakeProfit1 is hit. You are right about the Lot size too. I also had to add something to make sure it didn't keep trying to close half the position while price was past T1. I changed it to this:

      if(Bid > TakeProfit1-Spread && OrderStopLoss() != OrderOpenPrice() && OrderLots() >= 0.2)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TakeProfit2-Spread,3,White);
            OrderClose(OrderTicket(),NormalizeDouble(OrderLots()/2,1),Bid,3,White);
         }

I haven't had a chance to put in any error recovery yet, but the problem isn't that the EA isn't opening trades, the problem is with the Custom Indicator. It will find a pattern when the strategy tester is set to only use the last month, but it won't find the same pattern if the strategy tester is set to use the last year. I suspect it has something to do with my ZigZag routine, which I didn't write entirely myself. Thanks for the help!

/
To add comments, please log in or register