Close Position by profit
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — /zh/forum/172166/page6#comment_49114893
Please consider which section is most appropriate — /zh/forum/172166/page6#comment_49114893
try to use PositionGetDouble and constant POSITION_PROFIT to get the profit of the current position
/zh/docs/trading/positiongetdouble
double TargetLoss = -10; double TargetProfit = 0.50; for (int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if (PositionSelectByTicket(ticket)) { double positionProfit = PositionGetDouble(POSITION_PROFIT); if (positionProfit >= TargetProfit || positionProfit <= TargetLoss) { if (trade.PositionClose(ticket)) { Print(__FUNCTION__, " Closed Successfully"); } else { Print(__FUNCTION__, " Error closing position: ", GetLastError()); } } } }
Don't need to do:
double profit=AccountInfoDouble(ACCOUNT_EQUITY)-AccountInfoDouble(ACCOUNT_BALANCE);
Documentation on MQL5: Trade Functions / PositionGetDouble
- www.mql5.com
PositionGetDouble - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
You are missing trading opportunities:
- Free trading apps
- Free Forex VPS for 24 hours
- 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
Hi
I have this code to close position when reach a specific profit(0.50) e loss(-10) buy when i run strategy tester i get profits like 2,3,4 and losses like -23,-12
Can someone help me,