exitLong(string algo, var price, int lots)

exitShort(string algo, var price, int lots)

Exits all long or all short trades with the current asset that match the given algo identifier, at market or at a given price limit, until the given number of lots is closed.

exitTrade(TRADE*,var price, int lots): int

Exits a particular trade completely or partially at market or at a given price limit. Returns 0 when the order failed, otherwise nonzero.

cancelTrade(int id)

cancelTrade(TRADE*)

Cancels an open trade with a particular identifier
or TRADE* pointer without sending a close order to the broker and without
modifying the statistics
or the balance. Useful for removing an externally closed position that was not transmitted to Zorro (f.i. on an NFA account).

Parameters:

algo

0 for closing all trades with the current algo/asset combination. Alternatively, an algorithm identifier, or the asset name when no algo is used in the script, or "*" for closing all trades with the current asset.

Optional price or distance to current price for selling the position, or 0 for selling at market. A positive price or price distance constitutes an exit stop, a negative price is an exit limit (similar to Entry). An exit stop closes the position when the asset price is at or worse than the given price, like a stop loss; an exit limit closes the position when the asset price is at or better than the given price, like a profit target.

lots

Optional number of lots to be closed, or 0 for
closing all open lots. Partially closing trades is not available
with all brokers.

id

Identifier of the trade to be cancelled. Either the full number as assigned by the broker, or the last 4 digits as displayed in the Zorro window.

Remarks:

Before closing a trade, check the business hours of the broker. Not all assets can be traded 24 hours per day. Trading is automatically disabled during weekends, during the LookBack period, or in the inactive period of SKIP or DataSplit.
Preventing trades outside market hours must be handled by the script.

Optional parameters can be omitted. F.i. exitLong() exits all long positions with the current asset and algorithm identifier at market.

If an exit stop or exit limit is given, the position is closed as soon as the target price is reached, no matter how long it takes. An exit stop overrides a more distant
stop loss, an exit limit overrides a previous
profit target. The exitLong/Short function can thus be used to tighten the stop loss or to change the profit target of open trades
(see example).

If closing a trade is rejected by the broker, the reason - such as "Outside market hours" - is printed to the log and the message window (with the
MTR4 bridge the reason can be seen in the MTR4 Experts Log). The number of rejected orders is stored in the
NumRejected variable. There can be several reasons for
the broker API to refuse closing a trade, for instance NFA compliance,
FIFO compliance, a wrong asset name, an invalid trade size, or the
market is closed or has no liquidity. Check the requirements of your
account and set the NFA flag and
Hedge mode accordingly.

If an exit order is rejected by the broker API, Zorro will repeat it in increasing time intervals until it is
eventually executed (except for options or when
OrderLimit is used). If the position can not be closed after 2 working days, Zorro will assume an issue with the broker server and remove the trade from its internal list. If the market is not open due to holidays or weekend, a message
"Can't exit at weekends"
is printed and the position is closed as soon as the market opens
again.

When no price was given, pending positions - positions either immediately entered before, or positions with an
entry stop or limit - are also closed.

The price at which the trade is closed in the backtest can be affected with the
Fill variable.

If a order function is defined in the script, it is called with
2 for the first argument and the TRADE* pointer for the second argument at the moment when the sell order is sent to the broker. This function can f.i. open a message box for manually exiting the trade, or control another broker's user interface.

Example:

exitShort(0,1.234); // exits all short trades with the current Algo/Asset as soon as the price is at or above 1.234 (exit stop).