Need Help In Coding 1 Buy, 1 Sell (So Close But Yet Still So Far)

 

Hello coders,

I am still relatively new to coding and I am writing an EA that does pretty much what i want it to. It opens trades when I want it to, it closes them when I want it to, etc. However, I have run into a wall. I want the EA to have a maximum of 2 open orders at any given time. If there is a buy order open, I want the EA to only open sell orders on sell signals and ignore the buy signals and when there is a sell order open, I want the EA to only open buy orders on buy signals and ignore the sell signals. I have spent many hours on this forum and elsewhere trying many different methods but none have worked so far. Can someone help please?

Here is the relevant portion of my code:

//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+

int OpenTrades = OrdersTotal();
if (OpenTrades == 0)
{
if (Buy1_1 > Buy1_2 && Buy2_1 < Buy2_2) Order = SIGNAL_BUY;
if (Sell1_1 < Sell1_2 && Sell2_1 > Sell2_2) Order = SIGNAL_SELL;


if (OpenTrades > 0) {
if (OrderSelect(OpenTrades - 1, SELECT_BY_POS, MODE_TRADES)==true){
if ((OrderType() == OP_BUY) && (Buy1_1 > Buy1_2 && Buy2_1 < Buy2_2)) return (0);
if ((OrderType() == OP_SELL) && (Sell1_1 < Sell1_2 && Sell2_1 > Sell2_2)) return (0);
if ((OrderType() == OP_SELL) && (Buy1_1 > Buy1_2 && Buy2_1 < Buy2_2)) Order = SIGNAL_BUY;
if ((OrderType() == OP_BUY) && (Sell1_1 < Sell1_2 && Sell2_1 > Sell2_2)) Order = SIGNAL_SELL;
}
}
}

with my code like this, the EA only opens trades when there are no other trades open and does not open any other trades regardless of incoming signals. When I omit the portions in bold, the EA opens more than 1 order but will open a trade on the next incoming signal ignoring whether or not it may be the same as the order that is already opened.

 

Please use this to post code . . . it makes it easier to read.

This line . . .

if (OrderSelect( OpenTrades - 1 , SELECT_BY_POS, MODE_TRADES)==true){

. . just looks at one open trade . . . so if you had a buy and a sell open it would ignore one of them . . . as far as I can see the reason why you only get one order open at a time is not here.

 
  1. Use SRC
  2. int OpenTrades = OrdersTotal();
    if (OpenTrades == 0)...
    This makes the EA incompatible with any other including itself on other charts or with manual trading.
    int OpenTrades=0;
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        ){
          OpenTrades++;
        }
    if (OpenTrades ...
    

 
WHRoeder:
  1. Use SRC
  2. This makes the EA incompatible with any other including itself on other charts or with manual trading.

    Thanks for the help!!!! I used your coding and it works like a dream now
    int OpenTrades = 0;
       int pos = OrdersTotal();
          for (pos = OrdersTotal() - 1; pos >= 0; pos--)
             if (OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)
          && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol())
          {
          OpenTrades ++;
          }   
             
       
       if (OpenTrades.........

    .
     
    J_Cutts:
    WHRoeder:
    1. Use SRC
    2. This makes the EA incompatible with any other including itself on other charts or with manual trading.

      Thanks for the help!!!! I used your coding and it works like a dream now


      There is 1 more thing I would like you to help me with if you would be so kind. How do I code ALL open orders to close 10 minutes before closing time on Friday?

      This is the code I have tried

      for (int y = OrdersTotal() - 1; y >=0; y--) {   
                        OrderSelect(y, SELECT_BY_POS);
                     if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
                        if (DayOfWeek() == 5 && TimeHour() > 23 && TimeMinute() > 50) Order = SIGNAL_CLOSE;
                     continue;
                     }
                  }
      I have tried variations of this coding but none have worked. Or is it that day and time do not work in backtesting?
       

      The problem is TimeHour . . .

      TimeHour() > 23   //  <----- never happens,  midnight is 00 not 24
      Reason: