SafetyLock - Position Security and Opening Opposite Pending Orders with LOCK (HEDGE)

SafetyLock - Position Security and Opening Opposite Pending Orders with LOCK (HEDGE)

25 June 2019, 16:45
Vladislav Andruschenko
7
2 933

SAFETYLOCK


"The greatest fortification of the fortress is its defenders ...." 
Silovan Ramishvili

Advisor Exp - SafetyLock PRO   was written to protect such systems from misses not by blocking positions, but by installing oppositely directed pending orders.

It helps traders avoid a strong market turn by placing an opposite pending order for an already open position.



How often did you have to suffer losses due to the fact that you did not look at the monitor at the time?

How often do you refuse more important things than look at the quotes charts?


Probably every 2nd reader has been in a situation where, by waiting of the continuation of the trend is removed    StopLoss   in the hope that this position will bring you more profit than you've expected. From this I wanted to start my article.


The market is very unpredictable, and hoping for some strategy or adviser is quite dangerous. There are so much kind of advisers in Internet , whose authors assure you that the working strategy and this adviser will bring you profit. Quite often, such advisers sooner or later make mistakes in their calculations and you risk losing your money. Working with positions, some traders insure their positions with the help of lock (HEDGE) orders, oppositely directed positions.

But often Lock is not an option, but a temporary satisfaction of one's desires.



Main functions

Advisor Exp - SafetyLock PRO   was written to protect such systems from misses not by blocking positions, but by installing oppositely directed pending orders.

It helps traders avoid a strong market turn by placing an opposite pending order for an already open position.

If a trader or adviser opens a position, our defender opens a deferred opposite order to this position. If the trader's position goes to a loss, the pending protective order triggers, thereby creating a LOCK .

In the functions of the Expert Advisor, it is possible to close the original position or work with the trailing stop, multiply the lot.

There is also a function to modify the price of opening a pending order following the price of the position.


The algorithm of this Expert Advisor:


The advisor starts his work by checking all open positions for the presence of already placed pending orders. In the system there are settings for the items being checked:


MagictoMonitor   - Magic numbers of the positions being checked. This option is designed to limit the expert's work on certain systems. For example, if you want the system to follow the positions of a particular Expert Advisor, you must replace this parameter with the magic number of this Expert Advisor. If you want an advisor to follow all positions, including positions that have been opened manually, this parameter should be set to 0

SymbolToMonitor   - The name of the instrument behind which the positions should be monitored. This option is designed to restrict the expert from working on certain symbols. For example, if you want the system to keep track of the positions of a particular symbol, you must replace this parameter with the exact name of the instrument (for example: EURUSD USDCHF USDRUR). If you want an advisor to follow all positions, including positions that have been opened manually, this parameter should be set to 0


If it is not yet the pending order on the checking position,    Exp - SafetyLock PRO   exposes a pending order at a distance delta from the current price (from the current one, because not all orders are open at a sufficient distance from the price) It should be noted that if the positions are opened when the    Exp - SafetyLock PRO   then the price of these pending orders will be at a distance delta from the opening price of the position. 
A pending order can be set with parameters,    StopLoss and TakeProfit. 
Also, a pending order can be set with a lot multiplied by MNLot. For example: if the position is open with lot 0.5, and the parameter    MNLot   = 2, then the pending order will be opened with lot 1.0. 
Further, the system    Exp - SafetyLock PRO   checks already open pending orders, and if the price has left the order for a distance, then the system modifies the pending order, thereby tutoring the order after the price.

If the main position is closed (by stop-loss, take-profit, manual or another system) - the pending order placed on this position is also deleted.

Parameter    OrderSafetyMonitor   - regulates the ability to check the issued orders. In other words, this parameter allows (true) / disables (false) the setting of pending orders at positions that were opened by the system    Exp - SafetyLock PRO   . 

There are three other important parameters that should be mentioned in this article:    TrailingIfProfit, TrailingStop and Prchoose .

Sometimes need to include trailing stop on positions. And if we started talking about an automatic system, I add to this system automatic Trailingstop.

In order to enable the Automatic trailing stop, the parameter    TrailingIfProfit   should be set to "true". It should be noted that when this function is running, the parameters StopLoss and TakeProfit at the first operation of the function are modified to 0.

Parameter    TrailingStop   - means the trailing stop.

Parameter    Prchoose   - Profit in the points at which the automatic trailing stop will begin its work.

Conclusion: With certain settings and subsequent verification of the system, I found that this tool is a kind of goldmine for all systems that are available on the Internet. This tool can rightly claim the title of "Full-fledged system defender". But notice, that the market is unpredictable and no one, even the most experienced traders can not with 100% confidence give the analysis of the market.


  1. The system monitors open orders for all currency pairs or for those specified in the Advisor settings, the parameter    Symbol_Monitor .
  2. The system follows those positions that are exposed in the advisor    Type_Monitor
  3. The system follows those magic numbers that are displayed in the advisor    Magic_Monitor
  4. A pending order is placed with a stop order for a distance    StopOrderDeltaifUSE   of the current price.
  5. Further, the Pending Order is monitored.
  6. If we have passed the distance    StopOrderDeltaifUSE   from the order and the parameter    ModifyOrdPend = true, the price of the Pending order is tightened. Thus leaving in breakeven.
  7. As soon as the pending order has triggered, we begin to follow it (watch out for it).
  8. The system has the function of disabling the monitoring of their orders. Parameter    SafetyPosMonitor = false disables monitoring of the positions that were created by this Expert Advisor.
  9. If you set the parameter to true, the system will monitor its pending orders.
  10. Further, if the Pending Order does not work and the Order from which the Pending Order was opened is already closed by the user or in another way - The pending order placed on this order is automatically deleted.
  11. In the system there is an opportunity to multiply the lots of the Pending order. Parameter    MNLOT . If, for example, an order is opened with lot 0.2 then the Pending order will be opened with lot 0.2 * MNLOT.
  12. If the    CloseOneifStopsActive = true, the position on which the Pending Order was opened is closed and only the position created by this Expert  Advisor will remain in the work
  13. Also there is an opportunity in the system to expose    StopLoss   and   TakeProfit   pending orders
  14. It is possible to include a trailing stop on the position    Trailling .

When the terminal is restarted, the system does not expose the new Pending orders. It monitors the Pending orders placed earlier.

  1. The system is able to monitor a huge number of orders.


External Variables and Settings

Name Description
MilliSecondsTimerForMonitoring number of milliseconds of execution of the algorithm (default 1000 = 1 second)


symbol Monitoring SymbolMonitoring= ALL_SYMBOL = Check all symbols, all transactions on the account. If you installed an advisor on a chart of any currency pair, he will monitor all currency pairs on the account;

SymbolMonitoring= Check only the current character. The advisor will only inspect and accompany transactions with the symbol on which the expert is installed;

SymbolMonitoring= SPECIFID_SYMBOL = Check only transactions as specified in the SPECIFID_Symbol_Monitor symbol. For example EURUSD - then the adviser will accompany transactions only on the symbol EURUSD;
SPECIFID_Symbol_Monitoring

Added the ability to create a list of characters separated by commas, for monitoring.
You can specify symbols for monitoring:
SymbolMonitoring= SPECIFID_SYMBOL SPECIFID_Symbol_Monitoring= EURUSD, USDCHF, USDJPY
then the adviser will follow 3 pairs!

Warning  !  You must specify the characters as written in the "Market Watch"

If it is written  EURUSD.c the need to write  EURUSD.с      instead  eurusd.c    or  EURUSD.C

Type_Monitoring type of transactions for monitoring, BUY SELL or ALL
Magic_Monitoring Magic number of items for monitoring
Now you can specify a list of magic numbers separated by commas, which you need to Check.
For example:
Magic_Monitoring= -1 means that they will check all magic numbers;
Magic_Monitoring= 0 means that manual transactions will be monitored;
Magic_Monitoring= 555,777 means that magic numbers 555 and 777 will be monitored;
Comment_Monitoring This option filters transactions for placing LOCK on positions whose comments coincide with Comment_Monitoring
For example:
Comment_Monitoring= "" - monitors all transactions, no matter what their comment
Comment_Monitoring= "VirtualTRadePad" - monitors all transactions, with the comment of VirtualTRadePad, even if the comment has VirtualTRadePad-1 or VirtualTRadePadEXTRA or TickVirtualTRadePadEXTRA
Min_Lot_Monitoring The minimum lot for monitoring by the adviser, if 0 - the parameter is disabled;
Max_Lot_Monitoring The maximum lot for monitoring by the adviser, if 0 - the parameter is disabled;

for example, you need to use the current SafetyLock advisor to protect positions that have gained a large lot, for example when using martin. Lot, on which you need to set the opposite stop order = 0.16, and the advisor should not touch positions that are less than 0.16, then you need to set the following parameters: Min_Lot_Monitoring= 0.16;
for example, you need to lock all positions that have a lot from 0.1 to 0.25, then you need to set the following parameters:
Min_Lot_Monitoring= 0.1;
Max_Lot_Monitoring= 0.25;
SafetyPosMonitoring Enable monitoring of the positions opened by the SafetyLock advisor.
If SafetyLock has locked your position by placing a pending order and this pending order was activated and turned into a position, then SafetyLock will install a new pending order for this position.
This algorithm is similar to the Swing algorithm.


Stop Loss StopLoss Orders of Pending Orders SafetyLock
TakeProfit TakeProfit of pending orders SafetyLock
MNLOT Lot multiplier for pending orders
If, for example, the order is opened with lot 0.2, then the pending order will be opened with lot 0.2 * MNLOT ;
StopOrderDeltaifUSEe Distance for pending orders SafetyLock
DynamicStopOrderDelta 

dynamic distance parameter:
Allows you to set a different distance of pending order, depending on the lot of the protected position.
With DynamicStopOrderDelta >0: The Higher the lot, the closer the pending order distance!
With DynamicStopOrderDelta <0: The higher the lot, the further the pending order distance!
DynamicStopOrderDelta is set to a position with a volume of 1 lot.

For example:
With DynamicStopOrderDelta = 100:
If the lot (volume) of your position is 1 lot, then the StopOrderDeltaifUSEe distance will be = 100 points;
If the lot (volume) of your position is 0.1 lot, then the StopOrderDeltaifUSEe distance will be = 1000 points;
If the lot (volume) of your position will be 0.01 lot, then the StopOrderDeltaifUSEe distance will be = 10,000 points;

With DynamicStopOrderDelta = -100:
If the lot (volume) of your position is 1 lot, then the StopOrderDeltaifUSEe distance will be = 100 points;
If the lot (volume) of your position is 0.1 lot, then the StopOrderDeltaifUSEe distance will be = 10 points;
If the lot (volume) of your position is 0.01 lot, then the StopOrderDeltaifUSEe distance will be = 1 point;



NumberofStopOrder The number of pending orders SafetyLock for one position.
You can set NumberofStopOrder = 5, then 5 pending LOCK orders will be opened for each position.
Allows you to build a grid of pending orders
DistancefromStopOrders The grid spacing in points, when using NumberofStopOrder


ModifyOrdPend Include a modification of the opening price of pending orders when the price moves.
Lets stretch pending orders after the price.
ModifyOrdPend_STEP Step of modification in points
ModifyOrdPend_onlytoOpenPrice

true - the ability to change a pending order only up to the opening price of the position (old version)

false - continue to modify the opening price of pending orders by price movement



ReopenClosedSafetyOrders Allows you to reopen the SafetyLock pending orders if they were closed with a stop, while the protected position is still open.
For example: Pending order SafetyLock has worked and turned into a position, this position is set stoploss, takeprofit or trailing stop. This position, when the price moves, is closed by its Stoploss or TakeProfit. When this option is enabled, the SafetyLock advisor will issue a new pending order to the main position.
StopOrderDeltaifReopens The distance to set the pending order SAFETYLOCK after reopening the order.
If StopOrderDeltaifReopens = 0, then the distance = StopOrderDeltaifUSEe (old version)
The price for setting a pending order will be calculated from the current price.
Close_One_if_Safety_Closed_In_Profit

closes the original position if the SafetyLock position has been closed in profits that cover the current loss of the original position.
For example, Close_One_if_Safety_Closed_In_Profit = true MNLOT = 2.
As soon as the LOCK goes off and the Safety position is closed by the TakeProfit, then the original position will be closed.

When using NumberofStopOrder> 1, the profit of SafetyLock positions is calculated from the amount of profit of all positions in the Ticket of the main position.

Now the closing of the main position occurs only when the amount of profit of the closed positions SAFETYLOCK and the main position is greater or
is equal to Close_One_if_Safety_Closed_In_Profit_Dollars.
All orders and positions of SAFETYLOCK are closed.


For example :

  1. Close_One_if_Safety_Closed_In_Profit = true and Close_One_if_Safety_Closed_In_Profit_Dollars = 10 and    StopOrderDeltaifReopens = 10;
  2. Original BUY position and warrant SAFETYLOCK SELLSTOP;
  3. The price goes down and BUY closes at a loss of -10 $ and the pending order SELLSTOP is activated;
  4. The price continues to go down and SELL becomes a profitable position with a profit of $ 20;
  5. According to the terms of this function: Loss from the original position BUY -10 $ + current position SAFETYLOCK SELL with a profit of + 20 $ = 10 $ total profit;
  6. The position SAFETYLOCK SELL is closed and the total profit from two positions (BUY + SAFETYLOCK_SELL) = 10 $.
Close_One_if_Safety_More_profitable

closes the original position if the SafetyLock position is more profitable, overlapping the current loss of the original position.
For example, Close_One_if_Safety_More_profitable = true MNLOT = 2.
As soon as the LOCK goes off and the Safety position is more profitable, both the Safety position and the original position will be closed.

When using NumberofStopOrder> 1, the profit of SafetyLock positions is calculated from the amount of the profit of all positions according to the ticket of the main position.

Now the closing of the main position and positions of SAFETYLOCK occurs only when the amount of profit of these positions is greater or equal
Close_One_if_Safety_More_profitable_Dollars (With NumberofStopOrder> 1)
To get the results of previous versions, set Close_One_if_Safety_More_profitable_Dollars = 0.


For example :

  1. Close_One_if_Safety_More_profitable = true and Close_One_if_Safety_More_profitable_Dollars = 10 and  StopOrderDeltaifReopens  = 10 and  MNLOT = 3;
  2. Original BUY position with lot 0.1 and order SAFETYLOCK SELLSTOP with lot 0.3;
  3. The price goes down and the current loss is BUY -10 $, then the pending order SELLSTOP is activated;
  4. The price continues to go down and SELL becomes a profitable position with a profit of $ 30, a loss of the original position BUY = -20 $;
  5. According to the conditions of this function: Loss from the original position BUY -20 $ + current position SAFETYLOCK SELL with a profit of + 30 $ = 10 $ total profit;
  6. The SAFETYLOCK SELL position and the original BUY position are closed and the total profit from two positions (BUY + SAFETYLOCK_SELL) = 10

If both Close_One_if_Safety_More_profitableand Close_One_if_Safety_Closed_In_Profitfunctions are enabled,
then the total profit is calculated from the closed positions SAFETYLOCK and positions SAFETYLOCK, which are still open. (Actual for NumberofStopOrder> 1)
The profit amount of SAFETYLOCK must be greater than or equal to Close_One_if_Safety_More_profitable_Dollars + Close_One_if_Safety_Closed_In_Profit_Dollars.,
than the main position.


For example :

  1. Close_One_if_Safety_More_profitable = true and Close_One_if_Safety_Closed_In_Profit = true and Close_One_if_Safety_More_profitable_Dollars = 10 and  StopOrderDeltaifReopens = 10 and  NumberofStopOrder = 5 and TakeProfite = 10 and MNLOT = 3;
  2. Original BUY position with lot 0.1 and 5 warrants SAFETYLOCK SELLSTOP with lot 0.3;
  3. The price goes down and the current loss is BUY -10 $, then the pending order is activated the first SELLSTOP;
  4. The price goes down and the current loss is BUY -20 $, then the pending order of the second SELLSTOP is activated; In this case, the first position SAFETYLOCK SELL closes with a profit of $ 30
  5. The price continues to go down and the second SELL becomes a profitable position with a profit of $ 10, a loss of the original position BUY = -30 $;
  6. According to the conditions of this function: Loss from the original position BUY -30 $ + current position SAFETYLOCK SELL with a profit of + 10 $ + Profit from the closed position SAFETYLOCK SELL + 30 $ = 10 $ total profit;
  7. The position SAFETYLOCK SELL and the original BUY position are closed and the total profit from three positions (BUY + SAFETYLOCK_SELL + SAFETYLOCK_SELL) = 10 $.
CloseOneifStopsActive Close the position if the SafetyLock pending order has triggered.
If the pending order is activated and turned into a position, then, if this option is enabled, the main position will be closed.
CloseSafetyPositionsifclosed Close SafetyLock positions, which are already activated if the main position is closed.
If the main position has been closed, then the SafetyLock position will also be closed
Close_One_if _Safety_Closed If the position SAFETYLOCK was closed for any reason (manual close, takeprofit, trailing stop), then the main position will also be closed.
Remove_TAKEPROFIT_ofOriginalifStopsActive Delete the take-profit of the original position if the SafetyLock pending order has been activated;
Remove_STOPLOSS_ofOriginalifStopsActive Delete StopLoss of the original position if the SafetyLock pending order has been activated;

Allows you to "hard" fix LOCK on two positions while deleting stop loss and takeprofit. Creates a "Safe Lock", which will not be unlocked if the original position was closed by its ststoloss or takeprofit.


TrailingStopUSE Enable trailing stop positions SAFETYLOCK PRO.
Includes trailing stop on positions that have been activated and opened by an advisor
IfProfTrail Include trailing stop only when the position is typed TrailingStope points of profit.
If you turn this option off, the EA will turn on the trailing stop function immediately, as soon as the position reaches +0 profit points. Then the first stop-loss will be established in the negative zone.
TrailingStarte Number of profit points to start the trailing stop function
TrailingStope Distance in points from the current price to the established stop-loss.
If the current profit is 200 points, and TrailingStope = 100, then the stop-loss will be set at +100 points. Thus, we give the price the possibility of a rollback of 100 points maximum. In the other case stop-loss will work and the position will be closed at +100 points.
TrailingStep Step of stop loss when the trailing stop function is enabled.


MovingInWLUSE Enable the break-even function.
The function modifies the stop loss in LevelWLoss points when the position dials the LevelProfit of the profit items.

Example:
LevelWLoss = 50 LevelProfit = 200
As soon as the position accumulates 200 points of profit, StopLoss of the position will be modified to the opening price of the position (+ spread) + 50 points
Example:
LevelWLoss = 0 LevelProfit = 100
As soon as the position accumulates 100 points of profit, StopLoss of the position will be modified to the opening price of the position (+ spread)
LevelWLoss The level of stop-loss in the positive zone in points
LevelProfit The number of points of profit that a position must pass for the lossless function to modify the stop-loss.


SetMinStops Automatically normalizes all parameters in points to the lowest possible level of stops on the server.
If YOU specified stop loss or distance = 10 points, and the minimum stop level on your server = 20 points, then the Expert Advisor automatically normalizes stop loss and distance values to 20 points.
If you turn this option off, then when the server returns error 130, the Expert Advisor can not install a pending order or stop-loss
Magic The magic number of opened pending orders by the adviser
Slippage The level of maximum possible slippage in points when opening and closing positions.

Example:
Slippage = 1
Maximum deviation when opening a position = Opening price + -1 point.
Slippage = 100
Maximum deviation when opening a position = Opening price + -100 point.
For example: The price of opening a position when sending an order to the server = 1.12345
But, if during the time of sending and opening a position the price has changed within 100 points, then the position will open with a slip in the range of 1.12245 - 1.12445
MarketWatch It includes the ability to open positions with stop-loss / take-profit on an account with MARKET execution.
The first opens the position, after the successful opening, the levels of StopLoss and TakeProfit are modified.