Multiple orders problem

 

Hi all,

I've an issue with my code. When an order (buy or sell) open others come in a row.

 

What I'm try to do is;

if last order is a sell then open a buy order when price come above the order Openprice plus some pips(ReturnDist). 

if last order is a buy then open a sell order when price come under the Openprice minus some pips.

Could someone give a help here ? 

Thanks in advance

Luis 

Files:
 
luisneves:

Hi all,

I've an issue with my code. When an order (buy or sell) open others come in a row.

 

What I'm try to do is;

if last order is a sell then open a buy order when price come above the order Openprice plus some pips(ReturnDist). 

if last order is a buy then open a sell order when price come under the Openprice minus some pips.

Could someone give a help here ? 

Thanks in advance

Luis 

 

What is your plan to restrict it?

You initialize OpenOpposite with 1. 

int    Ticket,total,OpenOpposite=1,UseSlippage,i;

 On every tick, you call OpenOppositeOrder()

   int start()
     {
      if (OrdersTotal()==0)GoToOpen();
       else GoToClose();      
      if(OpenOpposite>0)OpenOppositeOrder(); //<-- here 
      if(OrdersTotal()>0)OpPendingDel();
      
     }

I cannot see any other reference to OpenOpposite.

The for loop in OpenOppositeOrder doesn't any restriction from what I have seen so far... should it?

Where in your code do you think the error is?

 

Hi Kronin,

Before all thanks for your prompt response.

Sorry for the mess but what I've try to do was to have independent working blocks of code.

So, I've clean up the line you marked and the top part that says void OpenOpposite ()  and now comes some errors  as;

expression on global scope not allowed,etc.

If I'm not taking much of your time could you help me here?

Thanks

Luis 


 


 
luisneves:

Hi Kronin,

Before all thanks for your prompt response.

Sorry for the mess but what I've try to do was to have independent working blocks of code.

So, I've clean up the line you marked and the top part that says void OpenOpposite ()  and now comes some errors  as;

expression on global scope not allowed,etc.

If I'm not taking much of your time could you help me here?

Thanks

Luis 

I haven't said you have to clean up anything. I just wanted to know, where you try to restrict the EA from opening more than one order.

Try this (I haven't tested it):

Change the start function to (Maybe you have to filter for open market orders only -> depending on your strategy):

   int start()
     {
      if(OrdersTotal()==0){GoToOpen();OpenOpposite=0;} //<-- set OpenOpposite to 0 if no order is open (pending and market)
      else GoToClose();      
      if(OpenOpposite==0)OpenOppositeOrder();          //<-- only run OpenOpositeOrder, if OpenOpposite is 0.
      if(OrdersTotal()>0)OpPendingDel();      
     }

 

Add an OpenOpposite counter to the OpenOppositeOrder() function:

         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {
            Op = OrderType();

            if(Op == OP_BUY  && Ask < (OrderOpenPrice()- ReturnDist*pt))
               {                
               Ticket = OrderSend(Symbol(), OP_SELL, mlots, SellPrice, UseSlippage, 0, 0, "Sell Order", MagicNumber, 0, Red);
               if(Ticket > 0) 
                  {
                  Print("Opposite Sell order placed # ", Ticket);
                  AddLimitsSell();
                  OpenOpposite++;
                  }
               else
                  {
                  Print("Order Send failed, error # ", GetLastError());
                  }
               }
               
            if(Op == OP_SELL && Bid > (OrderOpenPrice()+ ReturnDist*pt))
               {               
               Ticket = OrderSend(Symbol(), OP_BUY, mlots, BuyPrice, UseSlippage, 0, 0, "Buy Order", MagicNumber, 0, Green);
               if(Ticket > 0)
                  {
                  Print("Opposite Buy order placed # ", Ticket);
                  AddLimitsBuy();
                  OpenOpposite++;
                  }
               else
                  {  
                  Print("Order Send failed, error # ", GetLastError());
                  }   
               }
            }

 

This should solve the issue with multiple orders open in a row. It restricts the EA to open more than one (opposite)order.

 
luisneves:

Hi all,

I've an issue with my code. When an order (buy or sell) open others come in a row.

Why do you need a second thread about the same piece of code ?

 

RaptorUK:

Looking at your code I find it very hard to follow what you are trying to do,  I see very few comments to help me,  your start() function does not show me what you are trying to do on each tick,  you have no consistent layout of indentation.


 From this thread:  https://www.mql5.com/en/forum/142629    page 3

 
RaptorUK:

Looking at your code I find it very hard to follow what you are trying to do, I see very few comments to help me, your start() function does not show me what you are trying to do on each tick, you have no consistent layout of indentation.

Agree with that. I haven't checked anything else, than the multiple orders issue, because the code is not easy to read. It looks like parts of the code are copy and pasted from other code, written by somebody else. Better write your own functions with your own (consistent) style.

 
kronin:

I haven't said you have to clean up anything. I just wanted to know, where you try to restrict the EA from opening more than one order.

Try this (I haven't tested it):

Change the start function to (Maybe you have to filter for open market orders only -> depending on your strategy):

 

Add an OpenOpposite counter to the OpenOppositeOrder() function:

 

This should solve the issue with multiple orders open in a row. It restricts the EA to open more than one (opposite)order.


Hi kronin,

Thank you for your prompt response to my issue. What i'm try to get is a condition to open orders like this;

First open by mean of an indicator is a buy (could be a sell of course) and then a buy order is open, if price bounce down to a level under the open price from the buy order, a sell order will open, but if price goes up again no new open buy should happen. Here a new buy order should only happen when price bounces back from the sell order. All the same if a sell order is the first to happen.
I've tried to get that with the following code attached but doesn't work. Here what is working is the first order to open is a buy, then and as expected a sell order will open, but from here no other orders open...

I've these two blocks of code and seems that the first one is doing what is expected to do; Open a buy or sell order if conditions defined by indicator are there. Once an order ticket is received then an order modification is in place to send order limits.

I am using BuyTicket and SellTicket  in the code to avoid the opening of  multiple orders but seems that is not the right method to go...

// Buy Order Condition to Open

  if(Volume[0] > volume_previous + OpenDistance*pt && BuyTicket == 0)                       
          {//5  
          while(IsTradeContextBusy()) Sleep(10);
          RefreshRates();       
                       
     BuyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,RealSlippage,0,0,"Buy Order",MagicNumber,0,Green);
     if(BuyTicket > -1)
        {//6
         Print("Opposite Buy order placed # ",BuyTicket);
         AddLimitsBuy();        
         }//6
      else
         {//7 
         Print("Order Send failed, error # ", GetLastError());
         }//7 
         
      return(0);
      }//5
  //-----------------------------------------------------------------------------+      
  
  // Sell Order Condition to Open

  if(Volume[0] < volume_previous - OpenDistance*pt && SellTicket == 0)                   
     {//8                                 
          while(IsTradeContextBusy()) Sleep(10);
          RefreshRates();       
                                                
          SellTicket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,RealSlippage,0,0,"Sell Order",MagicNumber,0,Red);
          if(SellTicket > -1)
                  {//9
        Print("Opposite Sell order placed # ", SellTicket);
        AddLimitsSell();       
        }//9
     else
        {//10
        Print("Order Send failed, error # ", GetLastError());
        }//10      
     return(0);
     }//8                       


The following code identifies open orders and filter them buy symbol, magic number and order type. Once it detects  the last open order in the pool and if the condition to open (considering that the last order is a buy) and confirm that a sell ticket is not present, then a sell order is open. Same if the last order is a sell. And here the code is working.

A problem comes when a condition to open a third order comes and so on. Here nevertheless the conditions to open are there and the code have deal fine with the second open why doesn't the code work here ?

int Op;  
   
   for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)
      {//14
      if(OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))  
         {//15
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//16
            Op = OrderType();

            if(Op == OP_BUY && Bid < OrderOpenPrice() - (ReturnDist*pt) && SellTicket == 0)
               {//17               
               SellTicket = OrderSend(Symbol(), OP_SELL, MLots, Bid, RealSlippage, 0, 0, "Sell Order", MagicNumber, 0, Red);
               if(SellTicket > -1) 
                  {//18
                  Print("Opposite Sell order placed # ", SellTicket);
                  AddLimitsSell();
                  }//18
               else
                  {//19
                  Print("Order Send failed, error # ", GetLastError() );
                  }//19
                }//17
                           
            if(Op == OP_SELL && Ask > OrderOpenPrice() + (ReturnDist*pt)&& BuyTicket == 0)
               {//20             
               BuyTicket = OrderSend(Symbol(), OP_BUY, MLots, Ask, RealSlippage, 0, 0, "Buy Order", MagicNumber, 0, Green);
               if(BuyTicket > -1)
                  {//21
                  Print("Opposite Buy order placed # ", BuyTicket);
                  AddLimitsBuy();
                  }//21
               else
                  {//22  
                  Print("Order Send failed, error # ", GetLastError());
                  }//22   
               }//20
            }//16
         }//15    
      }//14

The complete code is attached in case you need to see what I'm try to explain.

Please take in attention that still  work out some other issues and some code could appear weird, this is the case for the way I use to open orders because am still to learn how to open orders based on Bid plus an minus Distance. 

I really appreciate if you could help me here.


best regards
Luis

Files:
 
Do you know what Volume[0]  gives you ?  how does adding a Point value to that make any sense ?
 

Hi Raptor,

You are right !

I'm still learn and try several ways to get what I need and some times stupid things comes up.

I've try to make use of an object to put it at Bid level and then get that position to open orders, seems better than this one. One of the main issues is the way to limit new opens while the open order is not closed.

I know that have a lot to learn and try by error is a way that I found to better understand how this run in practice. While I'm doing my up most to understand information from book and documentation  some times I feel lost and try to get help from others. Here in Portugal is very hard to get tuition around this subject and so am all dependent from the good will, patience and assertivity from others to get help and advise. When that help arrives I do my best to understand, but sometimes I can't cop with that.

Anyway thank you to show me that volume is not the way to get that kind of indicator.

Best Regards

Luis 

 
luisneves:

Hi Raptor,

You are right !

I'm still learn and try several ways to get what I need and some times stupid things comes up.

I appreciate that you are trying and trying to learn also,  perhaps you should first try to create a simpler EA and work with that until you understand how to get it to do what you need it,  try a few things,  add some Print statements,  see what happens,  this way you will learn and understand,  then when you learn more you can add more to your EA.   You need to have a clear idea of what you want your EA to do before you can code it,  too much functionality at once will not make that clear thinking process any easier. 

One other thing that will help,  try to break tasks down into modules that you can re-use with other EAs,  then you can code that module,  as a user defined Function,  on it's own as part of a script or simple EA to test it and make sure it works and is robust . . .  then you can use that function with confidence in an EA knowing that it works.
 

Hi RaptorUK,

Thank for your honesty response. I already try to break tasks and other  kind of problems start to show up, that's why start to put all together (and other problems starts....)

I already try to do another EAs and worked fine, but now using this approach without the use of indicators (standard ones) the things get complicated. 

The thing is right now am completely lost. I though that the way am see the code work tick by tick in theory that should work and then try to get limits to avoid multiple openings brings the things worse.

So now have some blocks of code that are working fine others that need some attention to refine performance and others (two) that don't work, and here I do not know how to get from here...simple  as that and I am totally defendant from help  that people that knows how to deal with that. You are one of the persons that have dedicated more time to m issues, but even so I still to not cop with some problems. If I had the time to learn all mql code and then start to write based in a lot of theory and experience acquired in past perhaps I wasn't here.

I saw an EA (attach) then try to understand how it works in parts of code that should bring me what I'm looking for but was unsuccessful in that. What I saw was that it make use of a code to get openings based on price plus and minus some distance. Here when I try to get this a problem with limits comes and then nothing...can't purely to put that working... and then cant't to see why the alternate orders don't work as in theory (at least the way I'm seen it). Pure disgrace.......

Anyway these are some just words from a limited guy...

best Regards

Luis

Files: