This is a semi-auto Expert to help you protect your position, using Hedging, when the market goes opposite direction. It will manages all lots automatically and exits the market in a proper way.
- Trailing stop an existing position when it makes profit.
- Opens a Hedging position when market goes in opposite direction.
- Exits the market when the sum of profit > 0 or specific condition is triggered.
- The EA WILL NOT enter the market by itself. It deals with how to exit only.
- Devil in the EA.
- HedgeDistance(Pts): The hedge distance in points (ex. EURUSD buy at 1.12345, HedgeDistance(Pts)= 1000 means Open a Sell at 1.11345).
- HedgeLotRatio: Ratio factor multiplied by the last total opposite position (ex. ratio=1.4 and total buy lots = 1.0, then open a sell 1.0*1.4=1.4 lots to hedging. Default value = 1.414. Range from 1.0 to 99.0).
- SpreadLimited(Pts): In floating spread condition, EA will not close the position when spread > SpreadLimited(Pts).
- SCLot: The SectionClose Operation threshold (please refer to SectionClose Operation description below).
- MaxLot: The safety threshold of maximum lots (If last open lot > MaxLot, Hedging will stop and make total buy = total sell, more detailed description is below).
- MagicNumber: The magic number of EA.
- TrailingStopDistance(Pts): Trailing stop distance in points.
- DisplayMessage: Show a message window in left corner.
- OpenTime(local): The timer. Open a initial position in specific time (local time). This feature can be used in backtesting or real time trading.
- Operation: The operation of the initial position. DirLong = buy; DirShort = sell; DirNone = do nothing.
- InitialLot: The initial lot size opened by timer.
The Hedging Idea
For example you have bought 0.02 lots EURUSD at 1.12000. Then EA will pend a hedge order to protect the initial position.
Hedgedistance = 400, HedgeLotRatio = 1.414. There will be a SellStop order with 0.03(=0.02*1.414) lots at 1.11500.
1. If the market goes up, the EA will set a trailing stop level, then close the order when stop level is triggered.
2. If the market goes down, and SellStop order triggers, you have 0.02 lots of buy and 0.03 lots of sell. If the market continues going down and down enough (depending on the Hedgedistance and HedgeLotRatio you have set), the sum of profit may come to positive and the EA will close both buy and sell orders.
3. If the SellStop order has been triggered, EA will pend a BuyStop Order with 0.02 lots at 1.12000. If market goes up again, you have 0.04 lots of buy and 0.03 lots of sell. If market continues going up, you may exit the market, otherwise the EA will continue the pending loop until you can exit.
Devil in EA (about MaxLot)
Being noticed that the loop may cause the opening lot size growth very fast. It is multiplied by the parameter HedgeLotRatio.
Ex. initial lot = 0.02, and ratio=1.414.
Using MaxLot parameter allows to stop the loop.
Sometimes in hedging condition the market will not move deep enough so that you could not have a positive total profit, but some part of it. Now you have 2 options:
1. You can wait and continue the loop till the market goes sufficiently long distance. The lot size may grow very big and cause bankrupt if your balance is not enough to fulfill the margin requirement.
2.You can close some part of hedging orders using the last hedging order's profit. Decrease the total lot size and wait for the next chance to open a new order.
The SectionClose Operation is to calculate the profit of the last hedging order, and how many orders could be closed within the small profit.
The SCLot parameter means that if the last hedging lot > SCLots, SectionClose Operation will be executed. A small value (0.01) means permanent execution, and a big value (99) means never.
After the operation, you may have an equal lot size in both side.
This EA works on 5 or 3 digits brokers with fix spread or floating spread. I have been tested and running it on demo and real accounts.
Auto Clear pending orders --- Default is true. EA will check the pending lot size whether fulfill the hedge ratio or not. If not ,EA will clear the pending orders and recreate a new one. Set the value to false to turn off.
Strict MagicNumber: (default = false). In older version, EA will calculated all opening orders in the same symbol, whether they have magic number or not. This setting sllows you to open your order manually anytime. Set the value = TRUE ,if you want to calculate the orders with the magic number specified in the parameter "MagicNumber". In an other way, EA will ignore the order you opened by hand, because there is no magic number. So the initial order may be opened by another EA.
Auto clear TP/SL: (default is 'true') in older version, this was a default setting. The EA will auto clear all TP/SL in opened orders. Set this option to 'false' to turn it off. But I do not recommend to do so.