On the internet, I got the sample code where I can enter and close orders everytime there is a crossover of Moving Averagea.
When I tested the sample code, it worked. When I analyzed the sample code, I didn't get how it worked.
I do not understand how this sample code could close orders at crossover.
I mean, don't you need this kind of code when you close at crossover too? ↓↓↓
How is closing orders at the next crossover even possible with this sample code...
I have attached the sample code bellow. If anybody could help, THANKS!!!
if previous ma10 of last 2 bar is greater than previous ma5 of last 2 bar ( ma10 coming from upside going downside)
and previous ma10 of last 1 bar is smaller than previous ma5 of last 1 bar (confirmation of crossover, ma5 is going up)
close any existing and open long position based on ma5 period
the opposite logic goes for short position. hope this helps.
basically the code does not care regardless any position type as long as there's a crossover, the code execute trend riding position although there's no validation whether it's really a new trend
The signal to open a trade in one direction is also the signal to close a trade in the opposite direction.
if(prev_ma10>prev_ma5 && ma10<ma5){ if(OrdersTotal()!=0) closeexisting(); // //send the order check = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 10, 0,0,NULL, magic);
It is very poorly coded though, so I would look for a better example in the codebase if I were you.
if previous ma10 of last 2 bar is greater than previous ma5 of last 2 bar ( ma10 coming from upside going downside)
and previous ma10 of last 1 bar is smaller than previous ma5 of last 1 bar (confirmation of crossover, ma5 is going up)
close any existing and open long position based on ma5 period
the opposite logic goes for short position. hope this helps.
basically the code does not care regardless any position type as long as there's a crossover, the code execute trend riding position although there's no validation whether it's really a new trend
now, it totally makes sense! thank you so much!
just one more thing that I'm stuck with.
Why did he put MODE_BID+OrderType()
as a closing price instead of just MODE_BID or MODE_ASK? what is the definition behind it..
check = OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);
The signal to open a trade in one direction is also the signal to close a trade in the opposite direction.
It is very poorly coded though, so I would look for a better example in the codebase if I were you.
thank you!!
Why did he put MODE_BID+OrderType()
as a closing price instead of just MODE_BID or MODE_ASK? what is the definition behind it..
As I said, poorly coded.
Most codes are like this | if(OrderType() == OP_Buy) … OrderClose(… Bid, …); else … OrderClose(… Ask, …); |
This code tried to be direction independent by mapping OP_BUY…OP_SELL to MODE_BID…MODE_ASK and then getting the closing price. | … OrderClose(… MarketInfo(_Symbol, MODE_BID+OrderType() ), … |
You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. | OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10); |
You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect. | OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10); |
I already said you this is not exact, if you don't believe me did you check ? You obviously didn't.
Forum on trading, automated trading systems and testing trading strategies
Alain Verleyen, 2019.08.09 15:32
So it seems I have to repeat again as you don't listen. (and by the way you explicitly wrote "before the next OrderSelect" and it's plain wrong concerning OrderClosePrice()).
RefreshRates() has nothing to do with OrderClosePrice(). RefreshRates() will NOT update OrderClosePrice(). OCP is NOT a Predefined Variables or series arrays.
OrderSelect() will refresh OrderClosePrice() and nothing else.
Please check it before arguing more.
Fix your answer and stop providing this wrong advice please.

- 2016.04.16
- www.mql5.com
Checked and verified.
Thank you.
I've lost track of who's agreeing with who, so...
- EA in an infinite loop within OnTick()
- Open buy trade for the same symbol as the chart (so, close price = bid)
- Displays:
- A: close price
- B: bid, via predefined variable
- C: bid, via MarketInfo
void OnTick() { OrderSelect(0, SELECT_BY_POS); while (!IsStopped()) { // See below... RefreshRates(); Comment("A: ", DoubleToString(OrderClosePrice(), 5), " B: " , DoubleToString(Bid, 5), " C: " , DoubleToString(MarketInfo(OrderSymbol(), MODE_BID), 5)); ChartRedraw(); // To make sure that a lack of updates isn't a purely visual artifact } }
With the code as above, with no use of RefreshRates(), then C updates but A and B do not.
If RefreshRates() is added, then B also updates but A does not.
To make A update, the OrderSelect() needs to be moved inside the loop.
Removing the RefreshRates() doesn't then prevent A from updating. So, updating the close price does require a new OrderSelect() and does not require a RefreshRates().
Or, putting that another way:
- MarketInfo() always updates
- Predefined variables such as Bid require RefreshRates() in order to update
- Order values such as close price require a new OrderSelect() in order to update

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
On the internet, I got the sample code where I can enter and close orders everytime there is a crossover of Moving Averagea.
When I tested the sample code, it worked. When I analyzed the sample code, I didn't get how it worked.
I do not understand how this sample code could close orders at crossover.
I mean, don't you need this kind of code when you close at crossover too? ↓↓↓
if( prev_ma10 < prev_ma5 && ma10>ma5)
How is closing orders at the next crossover even possible with this sample code...
I have attached the sample code bellow. If anybody could help, THANKS!!!