I have this, with help from FXEZ, but it loops throug all orders, and I only want to through the last 2 orders.
If you want to place another sell at a specific distance from a previous OP_SELL order you can select the most recent sell by the OrderOpenTime() and then use the OrderOpenPrice() to calculate your next level of entry.
bool TradePriceAllowed() { datetime prevOrderOpenTime=0; double prevOrderOpenPrice=0.0; for(int k=OrdersTotal()-1;k>=0;k--) { if(!OrderSelect(k,SELECT_BY_POS,MODE_TRADES))//if order select fails { Print("Order select failed with error : "+(string)GetLastError());//tell me the error break; } else { if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) { if(OrderOpenTime()>prevOrderOpenTime) { prevOrderOpenTime=OrderOpenTime(); prevOrderOpenPrice=OrderOpenPrice(); } } } }//end for if(MathAbs(prevOrderOpenPrice-Bid)>=Dist_Pips*Point) { return(true); } return(false); }
Changed around the return true and return false so you don't get return true if OrderSelect() fails.
As you can see , the loop has to go trough all orders to find the most recent one and it will not return true for older orders.
The only thing is that you may need to consider the direction of the order. Are you adding to existing sell position or buy position ?
That's one way I guess of doing it.
If you want to place another sell at a specific distance from a previous OP_SELL order you can select the most recent sell by the OrderOpenTime() and then use the OrderOpenPrice() to calculate your next level of entry.
Changed around the return true and return false so you don't get return true if OrderSelect() fails.
As you can see , the loop has to go trough all orders to find the most recent one and it will not return true for older orders.
The only thing is that you may need to consider the direction of the order. Are you adding to existing sell position or buy position ?
That's one way I guess of doing it.
Hi Thanks, that is nice of you.
Yes I will try it out, and I will add direction for buy and sell.
Now my problem is, that if it closes the order by the Trailingstop before it reaches the distance, it opens an order emidiately after the close.
So I gues I have to ad another rule to prevent that.
I tried it out and it looked correct at the beginning, but
Now my problem is, that if it closes the order by the Trailingstop before it reaches the distance, it opens an order emidiately after the close.
So I gues I have to ad another rule to prevent that.
For me to be more specific, I guess I'd need to see the whole code to make sure you update the result of TradePriceAllowed() before you use it..
What I think you may need to do is to call the TradePriceAllowed() function before you allow EA to place another order.
Don't just rely on the value stored previously when TradePriceAllowed() was last executed.
In other words, you need to update the variable that holds the result of TradePriceAllowed() function every time you rely on that value.
Kind of like if you need to know if you have an open order, you need to update the count after every routine that may close or open an order or
before using that value. Does it make any sense ?
On the other hand, the closed order by trailing is passed to the history pool and the TradePriceAllowed() function will not see it
in the open trades pool so, if you want to find the most recent open time of all orders that are open and that were closed, you need to
loop through open orders and history as well
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),MODE_HISTORY - order selected from history pool (closed and canceled order).
You can make datetime prevOrderOpenTime instead of local variable into a global or a static one but if you lose
connection of the EA goes trough initialization for some reason, you lose the value stored in it
In my view, looping trough all orders (open and history) is the safest way .
If it makes sense and you need help with that, let me know.
Cheers
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have this, with help from FXEZ, but it loops throug all orders, and I only want to through the last 2 orders.