How to prevent my expert to open new order if other order was already closed on the same candle

Back to topics list To post a new topic, please log in or register
avatar
10
L0rd1 2015.08.13 20:15
 

Hello,

just for your information first : my Expert can have only one open position at the same time by this:

      total=OrdersTotal();
      if(total<1)
        {
          .....
        }


And if my algo enables to open position on current candle, than it can happen, that after reaching TakeProfit or StopLoss, the position will be opened again(because as I wrote before: algo enables).

Or with other words: expert should not open position, if on current candle, was some position closed before.

So please help me how can I check if on current candle, was some position closed before ?


Thanks !

avatar
Moderator
3406
GumRai 2015.08.13 21:46 #
 

Loop through Order history

Find relevant trades

Check iBarShift of OrderCloseTime(), if it ==0, do not open another trade

avatar
10
L0rd1 2015.08.14 14:00 #
 

Thanks GumRai !

I created here some code, its correct like this ? It looks for right magic number and if iBarShift for Orderopentime not zero is.

bool InHistory(int magic) 
{
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
     
         if(OrderMagicNumber() == magic && iBarShift(0,0,OrderCloseTime()) !=0) {
                return(TRUE); // if shift not "0" than is true
     }
    }
  return(FALSE)
}

and function call is like this

  total=OrdersTotal();
      if(total<1 && InHistory(int magic)==TRUE)
        {
          ..buy..
          ..sell..
        }

im not sure if syntax is ok


avatar
12985
WHRoeder 2015.08.14 14:18 #
 
L0rd1:
bool InHistory(int magic) 
{
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
     
         if(OrderMagicNumber() == magic && iBarShift(0,0,OrderCloseTime()) !=0) {
                return(TRUE); // if shift not "0" than is true
     }
    }
  return(FALSE)
}

  1. That won't work, it returns true if any order was closed an a previous bar, not if all orders were. The first argument to iBarShift must be a string (or NULL). Zero is wrong. No need to iBarShift, Just compare OCT to Time[0];
  2. InHistory is a bad name, you are checking if any orders closed this bar, not if any orders exist (in history.)
    Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  3. Alternative is a new bar approach.
    static datetime time0OpenOrder=0;
    for(int iPos = OrdersTotal(); iPos >0;){ --iPos;
       if(OrderSelect(iPos, SELECT_BY_POS, MODE_TRADES)
       && OrderMagicNumber() == magic 
       && OrderSymbol()      == _Symbol
       && ...
       ){ Time0OpenOrder = Time[0]; // Remember open order this bar.
          // Trail();
       }
    }
    bool wasTradeThisBar = Time0OpenOrder == Time[0];
    :
avatar
10
L0rd1 2015.08.14 15:24 #
 

Thx !

I trade current symbol and timeframe.

I think is better to check orderClosedTime for current bar, if order opened i cant open new because of this

      total=OrdersTotal();
      if(total<1)
        {
          .....
        }

and I dont understand this part of your code:

   && ...
   ){ Time0OpenOrder = Time[0]; // Remember open order this bar.
      // Trail();
   }

how it works together with the loop ? because no order was chosen


avatar
12985
WHRoeder 2015.08.15 14:13 #
 

L0rd1:

I think is better to check orderClosedTime for current bar, if order opened i cant open new because of this

and I dont understand this part of your code: how it works together with the loop ? because no order was chosen

  1.       total=OrdersTotal();
          if(total<1)
    Not filtering by magic number makes EA incompatible with all others (including itself on other TFs,) and manual trading Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  2. Look again
    static datetime time0OpenOrder=0;
    for(int iPos = OrdersTotal(); iPos >0;){ --iPos;
       if(OrderSelect(iPos, SELECT_BY_POS, MODE_TRADES)
     :
avatar
10
L0rd1 2015.08.17 20:27 #
 

WHRoeder I dont understad logic in your new bar approach. What should be here in Trail() ?

// Trail();


Here is my firs version modyfied. But Im allways "true" also there is something wrong. I added

if (hstTotal !=0)

it helps to get true if I have no history at very begining.


   bool WasTradeThisBar() 
{
  int i,hstTotal=OrdersHistoryTotal();
  if (hstTotal !=0)
  {
  for(i=0;i<hstTotal;i++)
    {
     if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderCloseTime() < Time[0]) {
            return(TRUE);
     }
    }
  return(FALSE);
  }
  else 
  {
  return(TRUE);
  }
}







Back to topics list  

To add comments, please log in or register