CloseIfProfitorLoss with Trailing Closing Positions Using Total Profit/Loss, with Trailing Stop.

CloseIfProfitorLoss with Trailing Closing Positions Using Total Profit/Loss, with Trailing Stop.

2 March 2017, 13:06
Vladislav Andruschenko
2
4 607

CloseIfProfitorLoss with Trailing


Closing Positions in MetaTrader 4 Using Total Profit/Loss, with Trailing Stop.


A convenient panel for working with an advisor.

  1. You can enable the Separate Order,
  2. Close and calculate separately for BUY SELL items (Separate BUY SELL),
  3. Close and calculate all symbols or the current symbol (All Symbols),
  4. Enable Trailing Profit (Trailing Profit)

Closing takes place according to the deposit currency, points,% and drawdown.

  • You can set the total loss (in dollars, points, percentages) and the advisor will close the loss.
  • You can set a common profit (in dollars, points, percentages) and the adviser will close the profit.

Advisor is intended for use on any account paired with any adviser or in manual trading. Once the total balance of deals (positions) for a pair or for all pairs becomes greater or equal to the value specified in the settings, all deals (positions) are closed and orders are deleted.

This version is able not only to close profits at a set level,  but also to implement a trailing in order to get more profit.

THIS EA - is tab from VIRTUALTRADEPAD

The control panel closes by the general profit or loss in dollars, pips and % of the balance.

The panel can be used as a virtual stop loss or take profit (Separate Order).

There is also an emergency button - Close ALL. 
Added the ability to profit trailing.



  1. The panel works on the symbol that is set (Current Symbol), or considers the total profit or loss for all symbols ( All symbols).
  2. The panel can be used as a virtual stop loss or take profit. To do this, set profit and loss and tick off  Separate Orders. The adviser will close positions separately from each other, according to the set profit and loss values ​​in points, dollars, percentages
  3. In the panel, you can choose the type of closing: for profit in dollars, in pips or in % of balance, as well as for loss in dollars, in pips or in % of balance.
  4. Also there is a button "CLOSE ALL", which closes the position of the symbol or all positions (depending on the choice) instantly.
  5. Directly on the panel displays information about the current state of your positions for the selected methods of closing.
  6. Profit can be set only above 0. If you set 0 - the field will flash red. This means that the set value with an error and this field will not work with the Expert Advisor. If YOU set a value with a minus, the EA will automatically move the field to a positive value (for example -100 = 100)
  7. The loss can only be set to below 0. If you set 0, the field will flash red. This means that the set value with an error and this field will not work with the Expert Advisor. If YOU set a value with a plus, the EA will automatically move the field to the minus value (for example 100 = -100)
  1. Place the panel on your currency pair.
  2. Depending on how you want to close positions (for all characters or for the current one), select the corresponding option on the panel itself.
  3. In order for the check box to select the type of closure to be active, you need to enter parameter values ​​other than 0, with a positive value for profit, and negative for a loss, for example: -100.
  4. Check the boxes opposite the items you want to use when closing (Profit \ Loss in points \ dollars \ percents).
  5. Enable the Close if Profit or Loss button (ON), it should turn blue. This means that the EA is involved in the work.
  6. As soon as the profit or loss of your positions crosses the mark of the established levels, the deals (positions) will be closed.
  7. The emergency button CLOSE ALL is designed to close all positions instantly.

Control:

  1. All Symbols - includes profit calculation and closing for all symbols.
  2. XXXXXX only - includes the calculation of profit and closing only for the current currency pair on which the adviser is installed.
  3. Separate BuySell - includes a separate closing for BUY and SELL positions. The directions BUY and SELL will be calculated separately from each other. And the closure will occur separately on the BUY and SELL direction.
  4. Separate Order - includes a separate closing for each position separately. Profit will be calculated for each position separately. This is analogous to the work of Virtual Stop Loss and Take Profit.
  5. Profit $ - Enables / disables closing on the total profit in dollars or in the deposit currency.
  6. Profit p - Enables / disables the closing of the total profit in points.
  7. Profit% - Enables / disables closing on the total profit as a percentage of the current balance.
  8. Loss $ - Turns on / off the closing for the total loss in dollars or in the deposit currency.
  9. Loss p - Enables / disables the closing of the total loss in points.
  10. Loss% - Enables / disables the closing of the total loss in percent of the current balance.
  11. Close if Profit or Loss ON - The closing function for total profit or loss is ON
  12. Close if Profit or Loss OFF - The closing function for total profit or loss is OFF

 

  - Function of profit trailing at set values.

This function includes trailing profits ( floating profit) at closing. 
In this case, the profit is closed not fixed, but with the trailing of the specified value.

As soon as the profit has reached the specified value, the adviser remembers the current value of profit and then tutoring this value after the profit.

Calculation of trailing occurs in those units that YOU have chosen as closing:  Points, Dollars, Perсents.

If YOU have ticked the closing for profit in dollars - then the profit trailing will work with the currency of the deposit.

If you ticked in 2 types: Points and dollars, then the profit trailing will work in two modes: in dollars and points. In this case, the closure will be on those units that will work first. 

For example :

Set:

  1. If you set the close at profit 10 dollars: 
  2. Profit $ - close in plus. 
  3. Profit $ - how much to close in plus. 
  4. TrailingProfit - distance from Profit $, at which achievement the trailing of profit is included. 

Action:

  1. When you set Profit $ to 10, and for TrailingProfit to 5, the trailing profit line is set to $ 5.
  2. The adviser, when passing the $ 10 mark, will include the profit trailing function and will pull the closing mark after the profit.
  3. In the log, the records of the current position of profit will be displayed. 
  4. After the total profit falls below the last fixed level - TrailingProfit, the adviser will close all deals (positions) forcibly.


The option works for all types of closing: All symbols, Current symbol, Separate direction BUY and SELL, Each order is seperate.

When the value is set to Profit = 10, and for Trailing Profit = 5, the profit trailing line is set at $ 5. The adviser, when passing the $ 10 mark, will include the profit trailing function and will pull the closing mark after the profit. In the log, the records of the current position of profit will be displayed.

10:20:20 Exp - CloseifProfitorLoss wTral EURUSD, H1: Traling is ON tralprofit = 12.00000000 10:20:20 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 12.00000000 10:20:20 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 13.00000000 10:20:20 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 13.50000000 10:20:21 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 13.50000000 10:20:23 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 13.50000000 10:20:23 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 14.00000000 10:20:24 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 14.50000000 10:20:24 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 14.50000000 10:20:29 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 15.00000000 10:20:31 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 16.00000000 10:20:32 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 17.50000000 10:20:36 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 18.50000000 10:20:37 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 21.50000000 10:20:37 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 24.00000000 10:20:37 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 24.00000000 10:20:39 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 26.50000000 10:20:39 Exp - CloseifProfitorLoss wTral EURUSD, H1: Level Profit up tralprofit = 28.00000000

After the total profit falls below the last fixed level - TralOptions, the adviser will close all transactions forcibly:

10:20:42 Exp - CloseifProfitorLoss wTral EURUSD, H1: Close position _close = 23.00000000 tralprofit = 23.00000000 10:20:43 Exp - CloseifProfitorLoss wTral EURUSD, H1: close # 212724663 buy 0.10 EURUSD at 1.34049 at the price of 1.34095 10:20:44 Exp - CloseifProfitorLoss wTral EURUSD, H1: close # 212724661 buy 0.10 EURUSD at 1.34049 at the price of 1.34094 10:20:45 Exp - CloseifProfitorLoss wTral EURUSD, H1: close # 212724660 buy 0.10 EURUSD at 1.34049 at the price of 1.34095 10:20:45 Exp - CloseifProfitorLoss wTral EURUSD, H1: close # 212724658 buy 0.10 EURUSD at 1.34049 at the price of 1.34095 10:20:46 Exp - CloseifProfitorLoss wTral EURUSD, H1: close # 212724657 buy 0.10 EURUSD at 1.34049 at the price of 1.34094

Examples of use are presented in pictures and videos.

CLP_TralOptionsStep is the step of changing the trailing. If you close in percentage, then this parameter is indicated in percent. for example = 0.01, then 0.01% 
Profit% is the profit level at which a profit is closed or trailing. For example 2%. 
TrailingProfit is the distance from the profit to the fixing of the trailing. Indicated in the values ​​in which you will close.

For example, in percent, = 1. This means that when the profit reaches 2%, the profit level will be fixed at the position 1% (2-1 = 1).

If the profit is 2.01%, then the level of the trailing will be fixed at 1.01% and so on. If the profit falls below 1.01% then all positions will close.

 

 

 

CLP_TralOptionsStep - a step change in trailing. If you close in dollars (or another), then this parameter is indicated in dollars (or another). for example = 1, then 1 $ (or another). 
Profit $ is the profit level at which a profit is closed or trailing. For example 5 $ (or another). 
TrailingProfit is the distance from the profit to the fixing of the trailing. Indicated in the values ​​in which you will close.

For example, in dollars (or another), = 1. This means that when the BUY or SELL profit is achieved in $ 5 (or another), the profit level will be fixed in the 4 $ (or another) position (5-1 = 4).

If the BUY profit is $ 6 (or another), then the level of the trailing will be fixed at $ 5 (or another) and so on. If the profit falls below $ 5 (or another), then all positions will close.

If SELL is at a loss, this will not affect the closure of BUY because we have included the option: Separate monitoring of directions.

 


Also, the advisor shows approximate closing prices for total profit or loss: 
parameter   Show_Future_BE_TP_SL   - show lines of future closing positions for total profit or loss. 
Caution: Only works on the current symbol and when the SEPARATE Orders mode is off. 
Lines are shown separately for Buy and Sell positions, or a common line. 
If the line is not shown, then the positions form a lock, which can not be closed by the total profit or loss. 
Note: Line and price data may not be accurate on the floating spreadsheet. 
Remember! SELL is closed by ASK, and BUY is closed by BID

 

 


Name Description
bool  Sounds = true Enables / disables sound alerts when opening / closing \ modifying positions and orders
bool  HintShow = true Turns on / off tooltips when hovering over buttons
bool  English = true Enables / disables English language prompts. If false, all tooltips will be in Russian.
int  MagicNumber = 777

Magic number for positions and orders, Set when opening. Used in determining the positions and orders for My or someone else's. 
0 - without magic number

- Added MultiMagic. Parameter MagicNumber updated: the first digit is intended to indicate the magic number for opening positions and accompany. You can also specify several magic numbers to accompany; 
eg: 
--- MagicNumber = 777 UseAllMagicFromAllAccount = true: The Advisor monitors all positions on the current symbol in the entire account; 
--- MagicNumber = 777 UseAllMagicFromAllAccount = false: The Advisor monitors positions with a magic number of 777 for the current symbol on the whole account; 
--- MagicNumber = 777,888 UseAllMagicFromAllAccount = false: The Advisor follows the positions with the magic number 777 and 888 (opens positions with the magic number 777) by the current symbol on the whole account; 
--- MagicNumber = 777,888,999 UseAllMagicFromAllAccount = false: The advisor follows the positions with the magic number 777, 888 and 999 (opens positions with magic number 777) on the current symbol on the whole account;

bool  UseAllMagicFromAllAccount= true Allows you to take into account all positions opened in the terminal for this currency pair. 

Example: 
UseAllMagicFromAllAccount = true 
then all positions with any magic number will be taken into account when opening, closing, modifying, overturning, information ..... 
UseAllMagicFromAllAccount = false 
The advisor takes into account only those transactions that have the magic number  MagicNumber


Show_Future_BE_TP_SL Show break-even and profit-loss levels on the current chart. The price of lines is calculated on the basis of the entered values ​​in the fields Profit and Loss in points \ dollars \ percents. 
Caution: Only works on the current symbol and when the SEPARATE Orders mode is off. 
The lines are shown, at the intersection of which, the function of "closing on profit or loss" 


Lines are shown separately for Buy and Sell positions, or a common line. 
If the line is not shown, then the positions form a lock, which can not be closed by the total profit or loss. 
Note: Line and price data may not be accurate on the floating spreadsheet. 
Remember! SELL is closed by ASK, and BUY is closed by BID
double  CLP_TralOptionsStep= 0 The step of changing the level of profit when trailing profits are conventional units (points, deposit currency, per cent). 
When the Trailing Profit function is in operation, the Expert Advisor remembers the profit level of each 1 conventional unit. 

Example: 
CLP_TralOptionsStep = 1 
The fixation of the level of profit occurs every 1 unit 
CLP_TralOptionsStep = 15 
The profit level is fixed every 15 units
bool  CLP_DeleteStopOrders = false Delete pending orders when "closing on the total profit or loss"
bool  Use_Lots_in_CLP_Points= false

When calculating points of profit or loss, the adviser will also take into account the lot of these positions. This option is useful when trading on a hedge account with different lots. 

Example: 
Open 2 positions SELL for EURUSD 
SELL 0.01 with a profit of 20 points and $ 0.2 
SELL 0.02 with a loss of -12 points and -0.24 dollars 

-When Use_Lots_in_CLP_Points = false, the Expert Advisor will calculate the "clean" items. 
in this case, the profit will be: -0.04 dollars or 8 points. 
As we see, in positions the position gained profit, but in dollars it is a loss. 

-When using_Lots_in_CLP_Points = true, the Expert Advisor will calculate the items taking into account the Lots. 
in this case, the profit will be: -0.04 dollars or -4 points. 
When calculating the profit in points and including the option "Include lots in the calculation of profits," 
the adviser finds the minimum lot from the presented positions and, proceeding from this lot, calculates Points. 
eg: 
Minimum position 0.01, 1 "clean" item = 1 point, 
If a position with lot 0.02 is opened, then 1 net point = 2 points including the lot. 
If the position with lot 0.05 is opened, then 1 net point = 5 points including the lot.

For example:

Open 2 positions SELL for EURUSD 
SELL 0.01 with a profit of 20 points and $ 0.2 
SELL 0.02 with a loss of -24 points and -0.24 dollars

Total: Loss in points = -4 points, loss in dollars = -0.04 dollars!

bool  AutoCorrect_LevelCLP = true In the loss field, you can now enter only minus values, if the user entered +10, then the system will automatically translate to -10. 
In the profit field, you can now enter only positive values, if the user entered -10, then the system will automatically translate to +10. 
CLP_TralOptions, you can enter only a positive value, otherwise the Expert Advisor will return an error.
bool  CPL_AlerT = false Issue closing message on profit or loss on terminal graph
bool  CPL_Mail = false Issue a message of closing on profit or loss to the established mail
bool  CPL_Push = false Issue a closing message on profit or loss to the phone in the form of a PUSH notification
bool  CPL_CloseTerminalAfterClose= false Closing the terminal after all positions have been closed
CPL_AUTOOFF_AfterClose automatic shutdown of the button "Closing on the total profit or loss" after the closing of all positions. 
Disconnect occurs only when ALL! positions are closed. 
The Magic number specified in the Expert Advisor is taken into account, as well as the symbol for closing. 
If you choose to close only 1 currency. In this case, the parameter CPL_AUTOOFF_AfterClose is enabled, then the EA waits until all positions for this currency are closed and only after this the EA can turn off the button.


int  MAGIC_FONTSIZE = 12 The text size on the chart when using MagicTarget as well as  Show_Future_BE_TP_SL and Show_Future_Line
color  MAGIC_ORDER = clrYellow The color of the center line and the opening price of the position and the order on the chart
color  MAGIC_STOPLOSS = clrRed The color of stoploss lines on the graph
color  MAGIC_TAKEPROFIT = clrGreen The color of the takeprofit lines on the graph
color  NULLColor1 = clrGray Color 0 or blank in the information in the panel
color  PLUS_Color = clrLime The color of the plus value in the panel information
color  MINUS_Color = clrOrangeRed The color of the minus value in the panel information
color  TEXTColor4 = clrAqua The text color in the panel information


Limiting  LimitFor = 3 If YOU decided to limit losses on the current client magic number, then you can do this in a new block. 

Attention: This block does not close the deal! he just brings up an alert! 
The next LimitFor this parameter is reset to 0. 
Also you can enable sending messages to mail and phone when reaching the limits NotificationMailSend 

Choices: 
DAY - take into account the profit and loss for the current day 
WEEK- take into account the profit and loss for the current week 
MONTH- take into account the profit and loss for the current month 
ALLTIME- account for profit and loss for all time. 

Example: 
You need an expert to send you mail and display an alert on the screen when the total loss for open and closed transactions, within LimitFor, will be less than -100 dollars: 
Your settings will be as follows: 
LimitFor = 0; 
LimitForLosses = -100; 
LimitForProfits = 0; 
After reaching a loss on the history of the day -100 dollars, the advisor will give a message on the screen in the form of Alert and send mail.
LimitForLosses The loss is indicated in the deposit currency
LimitForProfits The profit value in the deposit currency is indicated
NotificationMailSend Also you can enable sending messages to mail and phone when reaching the limits NotificationMailSend
filling  TypeFilling = Auto The type of fill positions and orders. 
Used for MT5 terminal. 
In AUTO mode, the Expert Advisor tries to determine the fill type automatically. But, in some situations, you need to set the fill type yourself. 
If you get an error when opening a position 

10030 
TRADE_RETCODE_INVALID_FILL 
An unsupported type of execution of the remainder order is indicated. 
Set the type of fill that your broker indicates. 

Example: 
FillOrKill 
This execution policy means that the order can be executed only in the specified amount. If the market currently does not have a sufficient amount of a financial instrument, then the order will not be executed. The required volume can be made up of several offers available at the moment in the market. 


ImmediateOrCancel 
It means the agreement to make a deal on the maximum available volume on the market within the limits specified in the warrant. In case of impossibility of full execution, the order will be executed for an accessible volume, and the unexecuted order volume will be canceled. 


Return 
This mode is used for market, limit and stop-limit orders, and only in the modes "Market Execution" and "Stock Execution". In case of partial execution, a market or limit order with a residual volume is not withdrawn, but continues to operate. 
For stop-limit orders, the corresponding limit order with the execution type Return will be created upon activation. 




On the chart there are 2 open positions with lots of 0.1 and 0.2. I see that from the current price to the break-even 50 points, that is, loss is 50 points, and you show -150 points, why?

The calculation in points is very correct from our side. Therefore, even if you have 2 positions with different lots. The sum of the points of loss will be greater than the distance from the current price then the break-even points.

The adviser takes into account in the calculation of the spread, swap, commission, therefore all calculations and indicators of our adviser take into account many factors.

Let's take an example:

if 1 order is minus -10 points, then it is -10 points 
if 2 orders are minus -10 points, then it is already -20 points 
and if 2 orders with different lots, then lot 0.1 gives -10 points, and lot 0.2 gives -20 points and in total - 30 points 
because if you put a close at -50 points, you will have 1 order -50 points, then it will close with a loss of -50 dollars 
but if by your calculation there will be 100 orders there, but the distance is -50 points, then when closing it will be already -5000 dollars 
It seems to be worth -50 points, and upon closing the amount can jump from $ 50 loss to $ 5000 and so on


Vladon, Expforex,CLP,CloseifProfitorLoss,Close Profit,Total Profit, Close if Profit or Loss , Trailing Profit, Smart Trailing Stop
Share it with friends: