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; long and short positions can be open at the same time.
4 = virtual hedging; enter long and short positions simultaneously, but send only the net amount to the broker. Close old trades before new trades (FIFO compliance).
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) allows FIFO compliant closing of trades and 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



  Hedge = 5;  // virtual hedging in trade mode
  Hedge = 2;  // full hedging in test mode

See also:

NFA, enterLong/Short, LotsPool, LotsPhantom


► latest version online