Closing position on the opposite signal. My code just keeps accepting new signals and doesn't close them.
There are many things to finish in this EA, I'm new on programming also.
As you see its a trend following system, with filter and , after I fix the closing positions issue, a partial close at 1 atr distance.
Thing is, as I searched and learned, I set the code to store to information about the position on "posTicket" so that i could identify it when there is a open position, then close when the confirmation indicator(hilo) gives the opposite signal.
If some of you could point out where is the error on this, I would really appreciate it. It does not recognize any open positions, because it keeps opening new positions when the criteria is meet.
I followed until where the code was going and it jumps the yellow part that I put there.
Thanks reading and the MQL5 forum support.
void OnTick() { //--- total_positions=PositionsTotal(); if((total_positions>0) && (/*media corta condition*/) && (/*rates etc. condition*/)) for(int position_ticket=total_positions-1 ; i>=0 ; i++) //it looks more complicated but it makes the oldest positions //come first, latest has zero index { //enter code here (position type to check). BTW why are you making posTicket>0 a condition? //because the latest posTicket will always be 0 and you don't want to exclude that from processing //after all necessary checks enter PositionClose statement here. } // and the same for the other direction. } //+------------------------------------------------------------------+ // Later you can simplify the code by reusing the functions and only changing // the parameters you feed them with. Then you can have only one function that // is depending on different input conditions which you can store inside bool variablesI hope you can work with that although it will take some time to understand loops and if you have some questions do not hesitate to ask.
You need to go through the existing Positions in a for/while loop.
void OnTick() { //--- total_positions=PositionsTotal(); if((total_positions>0) && (/*media corta condition*/) && (/*rates etc. condition*/)) for(int position_ticket=total_positions-1 ; i>=0 ; i++) //it looks more complicated but it makes the oldest positions //come first, latest has zero index { //enter code here (position type to check). BTW why are you making posTicket>0 a condition? //because the latest posTicket will always be 0 and you don't want to exclude that from processing //after all necessary checks enter PositionClose statement here. } // and the same for the other direction. } //+------------------------------------------------------------------+ // Later you can simplify the code by reusing the functions and only changing // the parameters you feed them with. Then you can have only one function that // is depending on different input conditions which you can store inside bool variables
Ok, Sorry if I'm kind of dummie on it, but in trying to get it as max as I can. Bought some MQL5 courses on Udemy, doing all the free tutorial and classes I can find on YouTube and etc. But god damn, it's far from easy. OK, know that I stopped whining about it, LOL.
In purple I store the information about the any open position on the variable total_position, right?
Then on yellow its checked if there is any open one and the criteria to be checked on the direction that i want to close the trade. ( sell criteria to be met in order to close a buy position)
On green is the for loop, to keep repeating the the process until "i" is 0.
The blue part I organize that logic of getting the ticket and closing it.
Please, correct me if my assumption was wrong.
Questions;
what value and how will i give ''i''? something just like int i =0; ?
I thought about using "posTicket >0" to send the information that I have a open position.
BDW. a SUPER HUGE thanks for reading and taking the effort and patience to read and reply with your knowledge.
Have a great week.
Ok, Sorry if I'm kind of dummie on it, but in trying to get it as max as I can. Bought some MQL5 courses on Udemy, doing all the free tutorial and classes I can find on YouTube and etc. But god damn, it's far from easy. OK, know that I stopped whining about it, LOL.
In purple I store the information about the any open position on the variable total_position, right?
Then on yellow its checked if there is any open one and the criteria to be checked on the direction that i want to close the trade. ( sell criteria to be met in order to close a buy position)
On green is the for loop, to keep repeating the the process until "i" is 0.
The blue part I organize that logic of getting the ticket and closing it.
Please, correct me if my assumption was wrong.
Questions;
what value and how will i give ''i''? something just like int i =0; ?
I thought about using "posTicket >0" to send the information that I have a open position.
BDW. a SUPER HUGE thanks for reading and taking the effort and patience to read and reply with your knowledge.
Have a great week.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
There are many things to finish in this EA, I'm new on programming also.
As you see its a trend following system, with filter and , after I fix the closing positions issue, a partial close at 1 atr distance.
Thing is, as I searched and learned, I set the code to store to information about the position on "posTicket" so that i could identify it when there is a open position, then close when the confirmation indicator(hilo) gives the opposite signal.
If some of you could point out where is the error on this, I would really appreciate it. It does not recognize any open positions, because it keeps opening new positions when the criteria is meet.
I followed until where the code was going and it jumps the yellow part that I put there.
Thanks reading and the MQL5 forum support.