exitLong(string Filter, var Price, int Lots)
exitShort(string Filter, var Price, int Lots)
Exits all long or all short trades that match the given filter condition (see
below), 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.
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. For removing an externally closed position that was not transmitted to Zorro,
for instance on NFA compliant accounts.
|| 0 or "" for closing all
trades with the current algo/asset component (default).
name for closing all trades with the given algo and the current
An asset name for closing all trades with
the given asset and the current algo.
closing all call trades with the current
"p" for closing all
put trades with the current component.
"cp" for closing all
contract trades with the current component.
for closing all underlying trades with the current component.
"*" for closing all trades with the current asset.
"**" for closing all trades.
|| A pointer to the trade to be closed, or ThisTrade for closing the current trade in a trade enumeration loop.
||Optional price or distance to current price for selling the position, or 0
(default) 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.
||Optional number of lots to be closed, or 0
closing all open lots. Partially closing trades is not available
with some brokers.
||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.
- Optional parameters can be omitted. F.i. exitLong() exits all long positions with the current asset and algo 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
- Before closing a trade, check the business hours of the broker. Not all assets can be traded 24 hours per day. Trading is
normally disabled during weekends, during the LookBack period, or in the inactive periods of
TimeFrame, SKIP, or DataSplit.
- If the market was closed when exiting a trade in [Test] or [Train]
mode, the trade will be closed when the market opens again.
- If the close order is rejected by the broker in [Trade]
mode, the reason - such as "Outside market hours" - is printed to the log and the message window.
MT4/5 bridge the reason can be seen in the MT4/5 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, a closed market, or no liquidity. Check the requirements of your
account and set the NFA flag and
Hedge mode accordingly.
repeat a rejected close order 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
4 working days, Zorro will assume that is was already externally
closed 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
- Some cryptocurrency brokers deduct their commission from an open
position, and subsequently reject the close order due to insufficient funds. In that case make
sure to have a small additional amount on the account for covering
- 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
- If an 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.
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).