Skip to content
All library documents

Implementing Conditional Requests for Full and Partial Position Closures

Article MQL5 articles

Summary

This installment extends a MetaTrader trading library so pending requests can close positions fully, close them in parts, or close one position against another. Its central reliability issue is detecting whether a request has actually executed. Comparing a request with the account’s latest trading event can mistakenly treat a previous partial close as the current one when the remaining volume matches. The proposed fix is to compare against the latest event only after the library confirms that a new account event has occurred.

The implementation also initializes every field in the trade request structure before copying request data. This prevents leftover values, such as a nonzero opposing-position ticket, from changing a simple close into a close-by request. The excerpt shows the new public methods and revised relevance checks, but much of the implementation and testing discussion is omitted. It focuses on order-management correctness rather than presenting a trading signal or performance evidence, and its behavior depends on the library’s event tracking and account state handling.

Key ideas

  • Pending trading requests can be extended to handle full closes, partial closes, and close-by operations.
  • A previous partial-close event can be mistaken for execution of a later request with matching details.
  • Check the latest trading event only after confirming that a new account event has occurred.
  • Initialize the trade request structure to prevent stale fields from changing the intended operation.
  • The article describes library implementation details and provides no strategy performance results.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.