Download MetaTrader 5
To add comments, please log in or register
Share your knowledge. Become a well-known developer!
Peter
31
Peter 2015.08.13 18: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 !

Keith Watford
Moderator
7985
Keith Watford 2015.08.13 19:46  

Loop through Order history

Find relevant trades

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

Peter
31
Peter 2015.08.14 12: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

whroeder1
13641
whroeder1 2015.08.14 12: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];
    :
Peter
31
Peter 2015.08.14 13: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

whroeder1
13641
whroeder1 2015.08.15 12: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)
     :
Peter
31
Peter 2015.08.17 18: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);
  }
}
/
To add comments, please log in or register