OrderClose not working - page 5

 
Thanks GumRai
 

Hi,


I attach EA to few different currency charts. The EA will open many orders in a single chart if conditions met. What coding to ensure only one order per chart?

 
chuale:

Hi,


I attach EA to few different currency charts. The EA will open many orders in a single chart if conditions met. What coding to ensure only one order per chart?


Loop through open orders and check for magic number and symbol.

If there is already an order open, don't open any more.

PS. You should really have started a new thread, as your question is not in any way related to the thread title :)

 
chuale:

Hi thrdel,

Is Time[0] refering to the time of bar 0? Is there Time[1], Time [2] etc? I tried googled it but not much information in the internet. Also, if I attach this EA to two currency pairs, does it mean that OrderSelect only select the orders in the chart itself or all the orders in the terminal?




Time[0] is the time current bar has started. Yes, Time[1] is the time when the previous bar started and so on.

If you want to run the same EA on more than one chart, you need to make sure that "MagicMunber " variable is different for each EA even if you have same symbol on different charts.

Then EA's will not interfere with each other's orders.

OrderSelect() doesn't get the value from chart but from the server.

Information about your account and orders is stored on the server and that's why when you turn of your computer, your order stop or tp is still executed.

 
chuale:

Hi thrdel,


Why I get this warning message "implicit conversion from 'number' to 'string' postin forum.mq4 69 61" for the coding " if(ticket<0)Print("Error OP_BUY order failed, error:"+GetLastError());"?


If you want to display a double variable with Comment() or if you want to Print with Print(), you have to convert it to string with DoubleToString() function.

For int variables the conversion is obvious so if you don't use IntToString () to do so, it will still compile and work since it is so obvious.

 
chuale:

Hi,


I attach EA to few different currency charts. The EA will open many orders in a single chart if conditions met. What coding to ensure only one order per chart?


Hi Chuale,

I've made a couple of changes to your EA to get it working .

MagicNumber is a variable you have to make sure it is different on every chart !

If you want me to explain to you why and how the changes work pm me and I'm happy to help if I can.

You can run some tests yourself and see how it works, I tested it on USDJPY.

The Max and Min variable are replacing the value 1 and -1 in your condition so you can optimize and see if other values work better .


if(bar2 <-1) //changed to 
if(bar2 < Min
if(bar2 > 1) //changed to 
if(bar2 > Max

Do you know how to optimize parameters ?

If you have any questions let me know or pm.

Here is the code:

//+------------------------------------------------------------------+
//|                                                 chuale_test1.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.abc.com"
#property version   "1.00"
#property strict

extern double   TakeProfit=150;
extern double   Lots=0.1;
extern double   StopLoss=300;
extern int      Max = 6;
extern int      Min = -9;
extern int      MagicNumber = 12345;
extern int      Slip  = 3;
double     bar1;
double     bar2;
double     bar3;
double     bar3max,bar3min;
int        myTrades,ticket;
datetime   previousTime;
int        xMultiply;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//---
if(Digits==3) xMultiply=100;
if(Digits==5) xMultiply=10000;
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
 
   myTrades = CountTrades();
//
   bar1=(iClose(Symbol(),0,2)-iClose(Symbol(),0,3))*xMultiply;
   bar2=(iClose(Symbol(),0,1)-iClose(Symbol(),0,2))*xMultiply;
   bar3=(iClose(Symbol(),0,0)-iClose(Symbol(),0,1))*xMultiply;
   //
   if(myTrades>0)
     {
      if(bar3<Min) CloseThisSymbolAll("OP_BUY");
      if(bar3>Max) CloseThisSymbolAll("OP_SELL");
     }
  //
   if(Time[0]==previousTime) return(0);    
   previousTime=Time[0];          
   //
   if(myTrades<1)
     {
      if(bar2>1)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Chua EA",MagicNumber,0,Green);
         if(ticket<0)Print("Error OP_BUY order failed. error : "+GetLastError()); 
         return(0);
        }
      if(bar2<-1)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid+-TakeProfit*Point,"Chua EA",MagicNumber,0,Red);
         if(ticket<0)Print("Error OP_SELL order failed. error : "+GetLastError());
         return(0); 
        }
     }
   return(0);
  }
//========================================================================
int CountTrades()
{
int count=0;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)
   continue;
   if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
   if(OrderType()==OP_SELL || OrderType()==OP_BUY)
   count++;
   }
return(count);
}
//========================================================================
void CloseThisSymbolAll(string direction)
  {
  string dir = direction;
   int trade;
   for(trade=OrdersTotal();trade>=0;trade--)
     {
      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY && dir == "OP_BUY")OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
         if(OrderType()==OP_SELL&& dir == "OP_SELL")OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
        }
     }
  }
//========================================================================
 

Hi thrdel,


Thank you very much. you are very a kind and helpful. i will go through your coding first. Thanks again.

 

Hi Thrdel,

 

That means I have to try with different value of Max and Min for each backtest in order to determine which is the most optimun value?

 

Thanks