Some strategies, especially grid traders or systems with a portfolio of algorithms,
can hold simultaneous positions of the same asset in opposite directions. This
violates the NFA and FIFO compliance required by international brokers and
can cause rejection of orders. Even if orders are accepted, holding long and
short positions simultaneously increases risk, margin, and trading costs. It is
always preferable to fully or partially close a position, rather
than opening a new position in opposite direction.
Zorro's virtual hedging
mechanism guarantees FIFO compliant closing of trades (oldest are closed first)
and prevents positions in opposite directions, even with complex portfolio
systems. This happens in a completely transparent way; the script needs no
special code. The mechanism uses two layers of trades, phantom trades and
pool trades. The pool trades hold the net amount of the phantom trades. The strategy script only handles the phantom trades, while the broker only receives
orders for the pool trades. Pool trades are opened or closed when phantom trades open or close
(but not necessarily in that order). Phantom trades can be open in both directions at the same time, but
the resulting pool trades
can be only open in one direction per asset, either short or long.
Systems with virtual hedging are always superior to systems with 'real
hedging' that hold opposite positions. Aside from NFA and FIFO compliance,
virtual hedging increases profit by lower transaction costs, and reduces risk because trades are usually closed earlier
and are less exposed to price shocks or market crashes. For activating virtual hedging, only the Hedge variable (see
below) needs to be set. All trades are then automatically entered in phantom
mode. When the net amount - the difference of long and short open lots -
changes, Zorro automatically opens or closes pool trades in a way that the
market exposure is minimized. Example: several long positions are open with a
total amount of 100 lots. Now a short trade of 40 lots is entered. The net
amount is now 60 lots (100 long lots minus 40 short lots). Zorro closes the oldest long pool trades fully or partially until the sum of open positions is at 60 lots.
If partial closing is not supported, the oldest long pool trades are fully closed until the remaining position is at or below 60 lots. If it's less than 60 lots, a new long pool trade is opened at the difference. In both cases the net amount ends up at exactly 60 lots.
Virtual hedging is used in test and trade mode only, not in training mode. The performance statistics (Long/Short/Total) are
generated from the pool trades, not from the phantom trades. Only exception is NumPendingTotal,
which is affected by pending phantom trades (pool trades are never pending).
Phantom trades can also be manually entered for "equity curve trading"; those trades contribute to the Long/Short statistics, but do not trigger pool trades.
Virtual hedging affects the system performance parameters. Although the equity curves of a system with
or without virtual hedging are relatively similar, the number of trades, the profit factor, the win rate, and the average trade duration can be very different.
Although the performance with virtual hedging is better due to smaller trading costs
and smaller risk, the win rate and profit factor can be lower
because they are calculated from the pool trades. Thus, win rate and profit
factor can have limited relevance for the overall performance of a system. Here's an example of the same grid trading system without and with virtual hedging:
EUR/CHF grid trader, real hedging, win rate ~95%, profit factor ~10,
total profit ~$16000
EUR/CHF grid trader, same algorithm, virtual hedging, win rate ~65%,
profit factor ~3, total profit ~$16500
Hedging is controlled by the following variable:
Hedging behavior; determines how simultaneous long and short positions with the same asset are handled.
0 = no hedging; automatically close opposite positions with the same asset when a new position is opened (default for
1 = hedging across algos; automatically close opposite positions with the same algo when a new position is opened (default for unspecified accounts).
2 = full hedging; long and short positions can be open at the same time.
4 = virtual hedging without partial closing; enter long and short positions simultaneously, but send only the net amount to the broker.
5 = virtual hedging with partial closing; open positions are partially closed to match the net amount.
- Hedge = 5 is preferable to Hedge = 4 since it opens
fewer trades and thus causes less transaction costs. Hedge = 4, on the other hand, is required when trades must be closed in FIFO compliant manner (oldest trades are closed first),
but partial closing is not supported by the broker or account. This is often
not documented, but you can find out with the
TradeTest script. Start a session, set Lots to 10, open a
position, then set Lots to 5 and close the position. If this fails, your
account does not support partial closing.
- Hedge >= 2 enables full hedging and thus does not
automatically close opposite positions. If this is required, it must be
handled by script.
- Dependent on open trades, closing a phantom trade can cause closing of several pool trades and/or opening of a new pool trade, especially in Hedge = 4 mode. A phantom trade can close with a loss and the subsequent pool trade with a win, or vice versa.
This is perfectly normal and not a bug.
- Hedging is prohibited for US based accounts due to NFA Compliance Rule 2-43(b). Such accounts
normally require the NFA flag. The NFA flag does not affect phantom trades, which can be simultaneously open in both directions.
- Pool trades have no profit target and no TMF, but - for protection against large price shocks - a very distant stop loss at about 20%..30% difference to the current price. They are normally only controlled by opening and closing phantom trades. However they appear in trade enumeration loops and can be identified by TradeIsPool.
- The number of open net lots of pool and phantom trades can be evaluated with the LotsPool and LotsPhantom variables. They can get temporarily out of sync when pool trades are externally or manually closed. In that case the difference remains until the next phantom trade with the same asset synchronizes the variables again. When Lots is 0 and no phantom trades are opened or closed, pool trades won't be opened even when LotsPool and LotsPhantom are different.
- Pool trades that were rejected by the broker will be re-entered at every bar until LotsPool and LotsPhantom are in sync again.
- In virtual hedging mode, the result window displays only open pool trades and pending phantom trades. When algo identifiers are used, pool trades are displayed either with the identifier of the triggering trade, or with a "NET" identifier.
Hedge = 5; // virtual hedging in trade mode
Hedge = 2; // full hedging in test mode
NFA, enterLong/Short, LotsPool,
► latest version online