Download MetaTrader 5
To add comments, please log in or register
MQL5 Cloud Network speeds up calculations. Try it!
danilodn
24
danilodn 2013.11.26 12:30 

Hello,

I am willing to open an opposite order (hedge) when some opened order hits some condition, but I am in trouble getting to do it with array.

What i got so far is:

int order[];
int i;

   for(int b = OrdersTotal()-1; b>=0; b--)                        // check for the opened orders
   {
      if (OrderSelect(b, SELECT_BY_POS, MODE_TRADES))
      {
            if (Bid - OrderOpenPrice() > (condition) )            // check for the condition to open the opposite order
            {   
               for (int n = ArrayMaximum(order); n >= 0; n--)     
               {
                  if (order[n] != OrderSelect(b, SELECT_BY_POS))  // check whether the order already have an opposite order opened (flawed)
                  {
                  order[i] = OrderSelect(b,SELECT_BY_POS);        // would store the current order for future check about having already an opposite order opened
                  i ++;
                  OpenOpposite();
                  }

Well, I think i am not getting to register the opened orders in the array and check whether there is already an opposite order opened.

Please help.

Thanks.

whroeder1
13602
whroeder1 2013.11.26 13:22  
danilodn: I am willing to open an opposite order (hedge)
Hedging isn't allowed in US. ERR_TRADE_HEDGE_PROHIBITED You do realize you pay the spread twice but can not profit?
danilodn
24
danilodn 2013.11.26 17:05  
RaptorUK:

I removed your code . . . .


Please edit your post above and re-insert your code using the SRC button . . . please use the SRC button to post code: How to use the SRC button.

To edit your post . . .




Ok, thanks.
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2013.11.26 17:07  
danilodn:

Ok, thanks.
Thank you.
danilodn
24
danilodn 2013.11.26 17:15  
WHRoeder:
Hedging isn't allowed in US. ERR_TRADE_HEDGE_PROHIBITED You do realize you pay the spread twice but can not profit?


I am sorry to know about it isn't allowed in US.

But as I can do it on my broker and MT4 allows it as well, I guess it is ok to discuss it here.

Yes, I would pay the spread twice, but it would diminish my drawdown and/or the negative equity from some negative orders, right? It is part of the strategy I am testing.

Anyway, I don't use it, let's assume it is just for programming learning purposes, ok? ;-)

I've just updated the code that was removed from the first post.

Can someone help?

Thanks.

whroeder1
13602
whroeder1 2013.11.26 17:26  
danilodn: Yes, I would pay the spread twice, but it would diminish my drawdown and/or the negative equity from some negative orders, right?
  1. Closing a negative order does the same without two spreads.
  2. order[i] = OrderSelect(b,SELECT_BY_POS); 
    
    RTFM what does OS() return?
danilodn
24
danilodn 2013.11.26 21:44  
WHRoeder:
  1. Closing a negative order does the same without two spreads.
  2. RTFM what does OS() return?


Of course, terrible mistake.

I wonder if it would work using:

if (order[n] != OrderTicket())         

But, i am not getting to run through all array values to check for existing order because of error:

"Incorrect start position 0 for ArrayMaximum function".

for (int n = ArrayMaximum(order); n >= 0; n--)     
               {
                  if (order[n] != OrderTicket() )      // check whether the order already have an opposite order opened (flawed)

Any tips?

How about my logic? (saving the order ticket in an array for posterior checking)

Would it work? Thanks.

Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2013.11.26 22:06  
danilodn:


Of course, terrible mistake.

I wonder if it would work using:

But, i am not getting to run through all array values to check for existing order because of error:

"Incorrect start position 0 for ArrayMaximum function".

Does your array still have a size of zero elements or did you change it ?

int order[];
danilodn
24
danilodn 2013.11.28 22:17  

Hi,

Just wanted to give you some feedback.

I am still improving the code as I have a full time job and have little time to code, but i got to do the array part to check for opened orders.

Thank you WHRoeder and RaptorUK. Much appreciated.

Regards.

/
To add comments, please log in or register