Closing Multiple Trades

 

I rather quickly stumbled across a misconception that I have ignored for a while now, it is finally time to address it. My EA currently fires in a bunch of orders to scale in and out of trades, the problem is:

I thought if I have i.e. 3 BUY orders open and I want to close one, I could simply place a SELL order and I would be left with 2 BUY orders. My EA currently fires in a bunch of buy/sell orders so this quickly compounds. What I would like to do is:

(1) If NoOfBuyOrders > 0 and a sell trigger appears, then

(2) *** Close the oldest BUY trade

(3) else

(4) (Current code used to fire in a SELL order)


*And vice versa if I have >0 sell orders and I have a buy trigger, I would like the oldest sell order to close rather than have these multiple positions that get out of hand and all need to be closed eventually (manually) anyway.

Therefore my question is this. Is there a quick, simple check I can do to see if I am currently long/short to fulfilll (1)? And...

Could somebody type up (2) for me please? I would like the oldest trade to close in this instance. What is the most efficient way of referencing this? Is there a common practice or a simple snippet of code I could be introduced to?

TIA.

CPerry. 

 

Me again. Thought i'd try and add a bit more information since nobody has been able to help me yet.

I'm thinking maybe this problem has something to do with Magic numbers to keep track of multiple orders? The part of my EA that places an order references a magic number but this single number is only stated once in the parameters section at the beginning of my code.

The EA has fired multiple orders in to the market but they are called ‘Positions’ once I export my history in MT5 and these positions all have a 7-digit reference as opposed to the 12-digit number that is my magic number in the code, so maybe i've misunderstand the use case of magic numbers. 

My code has these two variables: m_nCountBuy and m_nCountSell. These can be used for line (1) [from my first message] 

Following this article: https://www.mql5.com/en/articles/112 I have attached a screenshot.

Could Position_Time be used here in line (2)? This is the line(s) I need help with please. If i.e. 3 buy orders were open and I get a sell signal, instead of firing in a sell order, i'd like to manage the OLDEST buy order and close it. I'm hoping Position_Time could be used in this instance? I'm day-trading so my bot resets everyday. Could do something like: trade.close(MINIMUM Position_Time) as the minimum number will be the oldest trade.

Thank you to anyone who can help me out with this.

CPerry.

Files:
 

About getting coding free help.

Welcome,

  • Usually people who cannot code do not receive free help on this forum, although it could happen if you are lucky. Be patient.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.

Good luck.

 
Hi! Did not follow exactly all your code, but I think that could sort all your OpenTime() for orders, into an array, sort the array, choose the oldest, identify that particular  order and close it. 
 
CPerry:

I rather quickly stumbled across a misconception that I have ignored for a while now, it is finally time to address it. My EA currently fires in a bunch of orders to scale in and out of trades, the problem is:

I thought if I have i.e. 3 BUY orders open and I want to close one, I could simply place a SELL order and I would be left with 2 BUY orders. My EA currently fires in a bunch of buy/sell orders so this quickly compounds. What I would like to do is:

(1) If NoOfBuyOrders > 0 and a sell trigger appears, then

(2) *** Close the oldest BUY trade

(3) else

(4) (Current code used to fire in a SELL order)


*And vice versa if I have >0 sell orders and I have a buy trigger, I would like the oldest sell order to close rather than have these multiple positions that get out of hand and all need to be closed eventually (manually) anyway.

Therefore my question is this. Is there a quick, simple check I can do to see if I am currently long/short to fulfilll (1)? And...

Could somebody type up (2) for me please? I would like the oldest trade to close in this instance. What is the most efficient way of referencing this? Is there a common practice or a simple snippet of code I could be introduced to?

TIA.

CPerry. 

Hello,I can help you out with this,you have come up with a good idea..This way by finding the Total position open function and exiting the oldest BUY ORDER and you can proceed with the new SELL ORDER 
 
Daniel Cioca #:
Hi! Did not follow exactly all your code, but I think that could sort all your OpenTime() for orders, into an array, sort the array, choose the oldest, identify that particular  order and close it. 

Thank you for your reply. Yeah, thinking about it over the last couple of days, I think storing the times in an array is the best option but instead of erasing the top value, have the array simply grow to a couple hundred values each day before being wiped and have an integer +1 each time a trade closes to identify which value in the array to use.

 
Arunachalam_94 #:
Hello,I can help you out with this,you have come up with a good idea..This way by finding the Total position open function and exiting the oldest BUY ORDER and you can proceed with the new SELL ORDER 

Hi mate, how exactly can you help?

Reason: