Menu

Taking a partial close at the first target in MQL5 without doing it twice
📰

Taking a partial close at the first target in MQL5 without doing it twice

DEV Community: tutorial·Omais Ali·14 days ago
#eejVjGwB
#devtotutorial#devto#dev#include#partial
Reading 0:00
15s threshold

A common exit plan is to close half the position at a first target and let the rest run. It sounds like three lines of code. In practice the first version usually has at least one of these bugs:

It closes half again on the next tick, and the next, until the position is gone.

It forgets that the partial was taken after a terminal restart and takes it again.

It sends a volume the broker rejects, such as 0.05 when the step is 0.1.

It leaves a remainder below the minimum volume, which can never be closed partially again.

This tutorial builds a partial close that avoids all four, for a hedging account.

Step 1: the inputs

#include <Trade\Trade.mqh>

input long MagicNumber = 20261102; input int FirstTargetPoints = 300; // profit distance for the partial close, in points input double ClosePercent = 50.0; // percent of the original volume to close

CTrade trade;

int OnInit() { if(FirstTargetPoints <= 0 || ClosePercent <= 0.0 || ClosePercent >= 100.0) return INIT_PARAMETERS_INCORRECT;…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.