OrderOpenPrice() does not work correctly

 

Hey guys,

in the following code-block I created an order via OrderSend and tried to modify it.

   if (RiskHedge == true) {
      OrderSelect(0,SELECT_BY_POS);
      if (total == 1 && OrderType() == OP_SELL && aktiv_2 == true && Ask > (Preis+3*spread)) {
         OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Preis+spread,0,"RiskHedge",0,0,CLR_NONE); //====> Placed Order (Position 1)
         aktiv_RiskHedge = true;
      }
   }
 
   if (aktiv_RiskHedge == true && aktiv_RiskHedgeSL == false) {
      Print ("aktiv_RiskHedgeSL wurde aktiviert");
      OrderSelect(1,SELECT_BY_POS); //=====> Selecting Position 1
      if (OrderType() == OP_BUY && Bid > OrderOpenPrice()) {     //===> Using OrderOpenPrice(), the value is for some unknown reason 0
         double sl;
         sl = OrderOpenPrice() - spread;
         NormalizeDouble(sl,Digits);
         aktiv_RiskHedgeSL=OrderModify(OrderTicket(),0,sl,0,0,CLR_NONE);
      }
   }

As you can see in the notes the OrderOpenPrice()- value equals for some reason 0.

Obviously this is a problem; it should equal the real open-price, but I cannot figure out why it doesn't work. Maybe I've looked far too long at these lines to see it...


Therefore, I have to ask for help now, since I'm unable to find any useful solution. I'd be grateful for every hint I can get! Thank you in anticipation!

 
xwdx: As you can see in the notes the OrderOpenPrice()- value equals for some reason 0.
OrderSelect(0,SELECT_BY_POS);
OrderSelect(1,SELECT_BY_POS);

What are Function return values ? How do I use them ? - MQL4 forum

If you have no open positions the first fails. If you don't have 2 open positions the second fails. If you have more than 2 you are not modifying the new one.

 
xwdx:

OrderSelect(0,SELECT_BY_POS) is always available and an OP_SELL value, otherwise the hole expert would stop working.

OrderSelect(1,SELECT_BY_POS) is a hedge. It is activatet if OrdersTotal (by the way, the variable total is OrdersTotal) is 1 and sends the new order.

Since in this code part I posted above the second if-block is only activated when the OrderSend-code succeeded, there are only these two orders active and pos 0 is OP_SELL/ pos 1 OP_BUY.


Additionally, the program enters the line

Therefore, the selection-order has to be correct (I checked that via a print-message and the journal).


Hopefully I could provide you with the information needed.


I can only say to you it is Markus who has to learn what his code means

Go open manually 5 trades on a demo account and try to modify all your open trades automatically

with a working program so you modify them all with a stoploss of 100 pips

Show me a code how to do that.... 

 

@ deVries

The problem is not to know what my code does in general (this is only a small part of a 300lines ea), the problem is that OrderOpenPrice() does somehow not work in this case and I cannot figure out why that doesn't work.

If I replace OrderOpenPrice with a constant value everything does work, but since this is not in my interest I am trying to use this variable and in that case, I really do not know why it does not work and asked for help.

Especially because the OrderSelect()-order does work...

 
xwdx:

@ deVries

The problem is not to know what my code does in general (this is only a small part of a 300lines ea), the problem is that OrderOpenPrice() does somehow not work in this case and I cannot figure out why that doesn't work.

If I replace OrderOpenPrice with a constant value everything does work, but since this is not in my interest I am trying to use this variable and in that case, I really do not know why it does not work and asked for help.

Especially because the OrderSelect()-order does work...

If you have one order what is it's position in the order pool ?  1 ?  or 0 ?
 
The EA is set to send a sell-order in its int init(). Therefore, this order takes pos 0. However, if this position is closed and pos0 becomes anything else than an OP_SELL-position, the ea finishes its work.
 
xwdx:
The EA is set to send a sell-order in its int init(). Therefore, this order takes pos 0. However, if this position is closed and pos0 becomes anything else than an OP_SELL-position, the ea finishes its work.

And what happens if you place this 'EA'  on 5 different pairs in same account ??
 

Than it is not going to work. It is not designed to completly trade for itself and since i don't do multiple trades at the same time I saved the time I would need to integrate a magic-number system.

I know that this doesn't suit the needs for a lot of traders, but it suits my trading-technique.

However, I can assure you that the OrderSelect-order is not the problem in this case. And this is also why I am unable to detect my failure..


Update:

I tested the following lines:

if (RiskHedge == true) {
      OrderSelect(0,SELECT_BY_POS);
      if (total == 1 && OrderType() == OP_SELL && aktiv_2 == true && Ask > (Preis+3*spread)) {
         OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Preis+spread,0,"RiskHedge",0,0,CLR_NONE);
         Print ("OrdersTotal(): ", OrdersTotal());
         OrderSelect(1,SELECT_BY_POS);
         Print (OrderOpenPrice());
         aktiv_RiskHedge = true;
      }
   }

The "prints" in the journal give me the right value for OrderOpenPrice in this case, so it has to be the second code-block..

 

Ok, I could solve the problem, I made an arithmetical error and put a sign in the wrong direction.. seems that I got blinded after all the time I starred at this.

Thanks for the time you invested to help me though!

 
xwdx:

Than it is not going to work. It is not designed to completly trade for itself and since i don't do multiple trades at the same time I saved the time I would need to integrate a magic-number system.

I know that this doesn't suit the needs for a lot of traders, but it suits my trading-technique.

However, I can assure you that the OrderSelect-order is not the problem in this case. And this is also why I am unable to detect my failure..


Update:

I tested the following lines:

The "prints" in the journal give me the right value for OrderOpenPrice in this case, so it has to be the second code-block..


Try this out on a ECN account

By the way is it timesaving not to integrate a magic-number system. ??

or is it you don't know how to do ?? 

 

I decided to upload my EA to the Code Base. If you are interested in what I am trying to do, feel free to read the description posted there.

Of course it will take its time to get the post published, but I will post a link here when its published.

But the lines I programmed here are disabled in the uploaded version so far.


Edit: It isn't as long as I don't need it, but I will change that in the future.

Reason: