Amount of openorders Question

 

Hello,

I'm trying to code a EA that allows to open buy and sell orders simultaneously, but only one order per type. (open only one BUY order, and open only one SELL order). Could somebody please explain me how I can do this?

At the moment I am using if(OrdersTotal()<1) to block the EA form opening the second buy order. This method will also block the ea when I would like to open a sell. This is my code:

int init()
  {
//----
 
   CurrentTime=Time[0];
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
if (EnterOpenBar == true)
{

if (CurrentTime != Time[0])
{

 
   int total = OrdersTotal(); 
   if (total > 0)  
   for(int n=0; n<total; n++)
  
   
   double candlelow     = Low[iLowest(NULL,0,MODE_LOW,10,0)];
   double candlelowbar  = iLowest(NULL,0,MODE_LOW,10,0);   
   
   double candleopen    = iOpen(NULL,0,2);
   double candleclose   = iClose(NULL,0,2); 
   double candlest      = iLow(NULL,0,2);
   
   if (candlest==candlelow)
   
   double open    = iOpen(NULL,0,1);
   double close   = iClose(NULL,0,1); 
   double lowest  = iLow(NULL,0,1); 
   double stoploss = ((close-open)/2)+open;
    
   double closeperc     = (candleclose-candleopen)/candleclose;
  
   bool doji;
   if(closeperc >= -0.0005 && closeperc <=0.0005)
   doji = true;

   double confirmation  = close-open;
   
   bool upconf;
   if(confirmation >35*Point)
   upconf=true;


   int ticket;
   
  
  // bool openorders;
  // if (OrdersTotal()>0)
  // if (OrdersTotal()==0)
 //  openorders=true;
     


   if (doji==true)
   if (upconf==true)
   if (close>open)   
   if (OrdersTotal()<1)
   //MARKETDIRECTION TO DETERMEN NEXT MOVE ???
   ticket=OrderSend(Symbol(),OP_BUY,0.10,Ask,3,stoploss,candleopen+1200*Point,"order",987654,Green);
   CurrentTime = Time[0];
   }
   }

   //if (doji==true)
 //  if (conf==true)
 //  if (open>close)   
//   ticket=OrderSend(Symbol(),OP_SELL,0.10,Bid,3,candlest,candleopen+1200*Point,"order",987654,Green);  
   

   
      
                 // total is aantal openstaande orders                                                
 int tinknr;                                         // GEEN IDEE IS NIETS MEE GEDAAN
 int trailingstop = 150 ;                        // Pip's voor trailingstop
 
    
 if (total > 0)                                 // als open posities is meer dan 0
   if (trailingstop>0)                           // als trailingstop is meer dan 0
   {
   for(int i=0; i<total; i++)                     // start loop tellen voor het achterhalen vah ordernummer vanaf 0 tot ordernummergevonden is
   {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);       // elke +1 wordt gecontroleerd of het een order is als order gevonden is

   if (OrderType()==OP_BUY)                        // identificatie
   if(OrderStopLoss()<Bid-Point*trailingstop)
   OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*trailingstop,OrderTakeProfit(),0,Blue); // aanpassing
 
   if (OrderType() == OP_SELL)                     // identificatie
   if(OrderStopLoss()>Ask+Point*trailingstop)
   OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*trailingstop,OrderTakeProfit(),0,Blue);    //aanpassing
     }
   return(0);
     }
 
renen:

Hello,

I'm trying to code a EA that allows to open buy and sell orders simultaneously, but only one order per type. (open only one BUY order, and open only one SELL order). Could somebody please explain me how I can do this?

At the moment I am using if(OrdersTotal()<1) to block the EA form opening the second buy order. This method will also block the ea when I would like to open a sell. This is my code:

In a loop where you select the orders one by one count up the number of open Buy orders and the number of open Sell orders, then outside the loop . . . if you already have > 0 Buy orders don't open another Buy, if you already have > 0 Sell orders don't open another Sell. Bear in mind within the loop you can/should check if the orders you are counting are for the correct symbol and Magic number.

Don't forget to check your Trade Function return values and report all errors and relevant variables when they fail . . . What are Function return values ? How do I use them ?
 
renen: I'm trying to code a EA that allows to open buy and sell orders simultaneously,
  1. Can't be done. You can open a buy and THEN a sell, or the other way around. But not simultaneously.
  2. OrderSends take time, you need to refreshRates between the calls
  3. Hedging is not allowed in the US.
  4. Why, one wins, one looses and you pay the spread on both.
  5. Simplify your bools
    //  bool doji;
    //   if(closeperc >= -0.0005 && closeperc <=0.0005)
    //   doji = true;
    
    // bool doji = (closeperc >= -0.0005 && closeperc <=0.0005);
    
     bool doji = MathAbs(closeperc) <=0.0005);
    

 
WHRoeder:
  1. Can't be done.
I have a way of doing it, and saving commission/spread costs ^_^
Reason: