philipvln:
I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?
if(OrderProfit()>0) { if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White)) Print("OrderClose error ",GetLastError()); } break; } if(OrderType()==OP_SELL) { if(OrderProfit()>0) { if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White)) Print("OrderClose error ",GetLastError()); }
- philipvln: I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?The code looks fine.
- OrderSelect and OrderClose: 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. - Use the debugger or print out your variables, including _LastError and find out why.
- OrderSelect and OrderClose: 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.
- Frank Vang:Use OrderClosePrice and you don't have to distinguish between buy/sells Bid/Ask.
philipvln:
I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?
Slippage? Commissions? OrderProfit doesn't take commissions or rollover into account. If you have $0.01 commission charge and close at the price where OrderProfit returns 0, then you'll see a $0.01 loss at least.
And in the case of sell orders, you pay the spread when you close.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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
I made an EA so that if the profit is above 0 the order is closed. But the order is still being closed even if the profit is negative. Why is this happening?