How to Prevent EA From Opening Another Position if One Already Exists for Same Currency Pair

 

Hi All,

My EA is not closing previous order before opening a new one.  I don't want to have multiple buy or sell orders on the same currency pair at a time.  How can I make it so it only has either a buy or sell order open at a time and prevent it from opening new position even if buy or sell condition are met?  Thanks!

Files:
HANNormal3.mq4  18 kb
 
seddie800:

Hi All,

My EA is not closing previous order before opening a new one.  I don't want to have multiple buy or sell orders on the same currency pair at a time.  How can I make it so it only has either a buy or sell order open at a time and prevent it from opening new position even if buy or sell condition are met?  Thanks!


Documentation
https://docs.mql4.com/

Tutorial book

https://book.mql4.com/


Freelance

https://www.mql5.com/en/job/new

MQL4 Reference
MQL4 Reference
  • docs.mql4.com
MetaQuotes Language 4 (MQL4) is a built-in language for programming trading strategies. This language is developed by MetaQuotes Software Corp. based on their long experience in the creation of online trading platforms. Using this language, you can create your own Expert Advisors that make trading management automated and are perfectly suitable...
 
seddie800: How can I make it
  1. You fix your bugs. In GetPositionStates, you only look at position zero. What happens that is not your pair/MN? What do you set your flags to, if you have no open orders? You would know the answers had you used the debugger or printed your variables values.

  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. 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:
    1. 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
    2. 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.)
    3. 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
    4. 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.
Reason: