Thanks for the input! According to manual, it: Gets the price of a pending order. Did I get it wrong and it works only for stop limits? I also tried PriceOpen - then my code stops working even for sell stops.
My logical condition idea is: if the absolute value between pending stop order price and current price is bigger than the absolute value of stop size (i.e., difference between order price and stop loss) - it means price's moved past stop, and then I want EA to cancel pending stop order.
I would be grateful for an advise on which function should I use instead to get the pending stop order price, dear Alain!
I was able re-assess and write another code that works for sell stops but not for buy stops
for(int i = OrdersTotal()-1; i>=0; i--) // count all currency pair positions if(m_order.SelectByIndex(i)) // access order properties by index if(m_order.Symbol() == Symbol()) if(m_order.OrderType() == ORDER_TYPE_SELL_STOP) if(MathAbs(m_deal.Price() - m_order.PriceCurrent()) > 1*MathAbs(m_deal.Price() - m_order.StopLoss())) m_trade.OrderDelete(m_order.Ticket());
I also tried m_order.PriceOpen instead of m_deal.Price() - no success neither.
I realize that your help is fully voluntary, yet I would be grateful if you might help me with why the above snippet does not work when I add order type buy stop. Or any advices on how to make it work. For me, as for a novice in mql5, it would have been a dealbreaker.
Thanks for the input! According to manual, it: Gets the price of a pending order. Did I get it wrong and it works only for stop limits? I also tried PriceOpen - then my code stops working even for sell stops.
My logical condition idea is: if the absolute value between pending stop order price and current price is bigger than the absolute value of stop size (i.e., difference between order price and stop loss) - it means price's moved past stop, and then I want EA to cancel pending stop order.
I would be grateful for an advise on which function should I use instead to get the pending stop order price, dear Alain!
You need to understand what you are doing rather than trying all kind of senseless things.
PriceStopLimit() is for stop limit orders, not useful in your case. It's written nowhere that's it's the "price of a pending order".
if(MathAbs(m_deal.Price() - m_order.PriceCurrent()) > 1*MathAbs(m_deal.Price() - m_order.StopLoss()))
Using a deal price doesn't make sense here as your order is pending, there is no deal at all !
if( MathAbs(my_order.PriceStopLimit() - my_order.PriceCurrent()) > 1*MathAbs(my_order.PriceStopLimit() - my_order.StopLoss()) ) { trade.OrderDelete(my_order.Ticket()); }// End if
What is the above supposed to do ? When do you want to delete your order ?
I also tried PriceOpen - then my code stops working even for sell stops.
Do you check why ?
Alain Verleyen #:
You need to understand what you are doing rather than trying all kind of senseless things.
Sure, just learning by doing - yes, sometimes I am doing irrelevant things.
Got my mistake. Even though documentation say that explicitly - screenshot attached. But I've realized that this is implied for stop limit orders only.
Whenever current price moves past stop loss level (i.e., the distance between an open price and a current price is greater than that between open price and stop loss level).
That's what I actually struggle with - this seems like a proper option, which does not work. Hence, I can't get whether I made a mistake in a logical condition, variables, syntax or maybe there is a problem with a machine setup - can't locate the issue on my own. In short, I am not sure how to properly check why PriceOpen does not work given the previous statement (should it?). That's why asking for community input.
Anyway, I remember the help is discretionary and grateful for your time, Alain!
Whenever current price moves past stop loss level (i.e., the distance between an open price and a current price is greater than that between open price and stop loss level).
Then code that, PriceOpen(), PriceCurrent(), Stoploss().
If it doesn't work then print the values in the Experts log to check why, or use the debugger. I would also suggest you to code BUY STOP and SELL STOP logic independently, it will be easier for you.
Then code that, PriceOpen(), PriceCurrent(), Stoploss().
If it doesn't work then print the values in the Experts log to check why, or use the debugger. I would also suggest you to code BUY STOP and SELL STOP logic independently, it will be easier for you.
Thanks for the insight, Alain!
I printed values to the journal and found that PriceCurrent() function does not return correct value (while all other functions work correctly). It returns 0.0 as a current price. I wonder why it is happening (I'm not sure how to use debugger yet)? I have read through documentation of this function (https://www.mql5.com/en/docs/standardlibrary/tradeclasses/corderinfo/corderinfopricecurrent) and I don't see any errors in the function use.
Any hints what did I miss?
Code:
// close pending orders that moved past stop for(int i = OrdersTotal()-1; i>=0; i--) // returns the number of current orders if(m_order.SelectByIndex(i)) // selects the pending order by index for further access to its properties if(m_order.OrderType() == ORDER_TYPE_BUY_STOP) if(m_order.Symbol() == Symbol()) Print("Price open:", m_order.PriceOpen()); Print("Price current:", m_order.PriceCurrent()); Print("Stop loss:", m_order.StopLoss()); Print("Symbol:", m_order.Symbol()); if(m_order.PriceCurrent() < m_order.StopLoss()) m_trade.OrderDelete(m_order.Ticket());
Screenshot from the experts log is attached.
Grateful for your time!
Screenshot from the experts log is attached.
Grateful for your time!
Added missing screenshot
By the way, are there any other way to access current price by current symbol for pending stop order?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
My goal is to write a simple EA that cancels pending buy stops or sell stops whenever current price moves past original stop price.
The EA - code attached below - works flawlessly for test sell stops (commented code for test sell stop). Yet, for test buy stops, the EA cancels pending order always in the next tick. To sum up, in strategy tester, no issues for sell stops; but for buy stops, it creates a test pending order, close it in the next tick, then creates another test order, close it immediately - and this process repeats indefinitely.
I am kindly asking for your input on what I missing here, since the code seems "symmetrical" for two order types from my standpoint and I can't understand such a behavior. Maybe something obvious is missing or typo is present?
Code:
Testing history sample screenshot for buy stops is attached as well.
My gratitude for your time, folks!