Put in some { } where they are required.
- Keith Watford: Put in some { } where they are required.Correct
if((OrderType() == OP_SELL || OrderType() == OP_BUY) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic_number) OrderClose(OrderTicket(),OrderLots(),Bid,300,clrNONE);
You buy at the Ask and sell at the Bid.- Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
-
Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at
a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)
-
Check your return codes for errors, report them
and you would know why.
Don't just silence the compiler, it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after. -
In the presence of multiple orders (one EA
multiple charts, multiple EAs, manual
trading,) while you are waiting for the current operation (closing, deleting,
modifying) to complete, any number of other operations on other orders could
have concurrently happened and changed the position indexing:
-
For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you
can simply count down in a position loop, and you won't miss
orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.) -
and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
-
For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you
can simply count down in a position loop, and you won't miss
orders. Get in the habit of always counting down.
You should use the RefreshRates() function
Sleep(40000); RefreshRates();
You should use the OrderClosePrice() function to set the closing price.
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,clrNONE);
Put in some { } where they are required.
for(cnt=TotalNumberOrders-1;cnt>=0;cnt-- ) { if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue; . . . }
You should not sleep your algo at all. That's asking for trouble. Instead use a timer.
Drew Clayman:
Hi All,
Thank you all so much for you input, it's really very much appreciated. The issue was no curly brackets on the for loop. Everyone's comments are very helpful in general though.
Thank you guys so much again.
You likely wouldn't have missed that if you properly indented your code...
nicholi shen:
You likely wouldn't have missed that if you properly indented your code...
True.
Or puritanically use { } for conditionals, always.
https://softwareengineering.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi, I'm trying to open a buy and a sell order then delete them if they don't open in 40 seconds. The loop is decrementing, but only the sell stop order deletes. I can't figure out what I'm doing wrong. Here's my code:
I would very much appreciate any help anyone can provide.
Thank you