Hedging behavior; determines how simultaneous long and short positions with the same asset are handled.
0 = no hedging; automatically close all opposite positions with the same asset when a new position is opened (default for accounts with no hedging).
1 = hedging across algos; automatically close all opposite positions with the same asset and algo when a new position is opened (default for unspecified accounts).
2 = full hedging; enter and open long and short positions simultaneously.
4 = virtual hedging; enter long and short positions simultaneously, but send only the net amount to the broker.
5 = virtual hedging with position minimizing. Several positions entered at market are combined to a single net position. Open positions are partially closed to match the net amount (see below).
Virtual hedging algorithm
Virtual hedging mode (Hedge >= 4) reduces trading costs by preventing that the broker opens long and short positions simultaneously and thus pockets commission for both. To prevent this, virtual hedging mode uses two layers of trades, phantom trades and 'pool' trades. The pool trades hold the net amount of the asset's phantom trades. The strategy script only handles the phantom trades, while the broker only receives 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 pool trades are only open in one direction per asset, either short or long.
Virtual hedging mode is transparent to the user; only the Hedge variable needs to be set. If at or above 4, all trades are 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. A short trade of 40 lots is entered. The net amount is now 60 lots (100 long lots minus 40 short lots). Zorro first checks if one of the long positions has an amount of exactly 40 lots; if so, the position is closed. Otherwise, the oldest long positions are closed until the sum of open positions is at or below 60 lots. If it's less than 60 lots, a new long trade is openend at the difference (Hedge == 4). In minimizing mode (Hedge == 5) the last long position is only partially closed so that 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 summed up from the pool trades only, 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 (see lots); those trades contribute to the Long/Short statistics, but do not trigger pool trades.
Virtual hedging affects the system performance. Although the equity curves of a system with Hedge = 2 and Hedge >= 4 are relatively similar, the number of trades, the profit factor, the win rate, and the average trade duration can be very different. The total profit as well as the capital requirement of virtual hedgins can also be different due to lower trade costs. Below is a grid trading system without and with virtual hedging:
Grid trader , Hedge = 2, Win Rate ~ 95%, Profit Factor ~ 10
Grid trader , Hedge = 5, Win Rate ~ 65%, Profit Factor ~ 3
- Hedging is prohibited for US based accounts due to NFA Compliance Rule 2-43(b). Such accounts require the NFA flag. If NFA is set, trades are not partially closed in Hedge = 5 mode, but are always fully closed. 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. If they get out of sync, for instance when pool trades are rejected by the broker or when they were externally or manually closed, the next pool trade with the same asset will synchronize the variables again. As long as Lots is 0 or no phantom trades are opened or closed, pool trades won't be opened either even when LotsPool and LotsPhantom are different.
- Closing a phantom trade does not close the associated pool trade, but the oldest pool trade. For this reason a phantom trade can close with a loss and the subsequent pool trade with a win, or vice versa. If a phantom trade is cancelled (see cancelTrade), a corresponding pool trade is also cancelled.
- 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.
- Virtual hedging is automatically disabled in [Train] mode.
Hedge = 5; // virtual hedging in trade mode
Hedge = 2; // full hedging in test mode
NFA, enterLong/Short, LotsPool, LotsPhantom
► latest version online