Instant estimation of profit, margin level, drawdown on MetaTrader charts

16 July 2018, 13:05
Stanislav Korotky
10
1 406

I'm sure all of you use the crosshair tool on MetaTrader's charts. It's very useful to measure distance in bars or points between 2 spots. Yet I was always wondering, why it does not provide an option to show profit value, margin level, drawdown or gain percentage in addition to points.

To remedy this problem I've developed a MQL program, which accompanies the standard crosshair tool with all abovementioned metrics.

It was not obvious which type of MQL program to choose for the project. Technically the program intercepts mouse dragging events, creates a label, and then monitors position of mouse cursor in order to move the label accordingly and recalculate the metrics displaying inside the label.

MQL scripts do not receive any events except for OnStart, so they went out of consideration.

MQL experts could apply, but only 1 expert can be attached to a chart, so it would be very inconvenient if you want to measure some stuff on a chart where an expert adviser is already running.

MQL indicators were the last chance. Unfortunately they have their drawbacks too. MQ prohibits usage of some margin related functions in indicators. For example, you can't use AccountFreeMarginCheck in MetaTrader 4 indicator. In an expert adviser you could write something like this:

AccountFreeMargin() - AccountFreeMarginCheck(_Symbol, OP_BUY, 1.0)

to estimage the margin for 1 lot _Symbol buy. But not in an idicator. This looks a bit illogical, because you can still use AccountInfoDouble(ACCOUNT_MARGIN) in indicators. Anyway, writing an indicator which would show margin level dynamically is not possible using standard MQL API functions. This applies to MetaTrader 5 as well, where OrderCalcMargin is limited to work in experts and scripts only. As a workaround you need to implement your own margin calculation. One of possible implementations is attached below.

This is an indicator ProfitMeter.mq4, which also compiles and runs in MT5, if it's renamed to ProfitMeter.mq5 beforehand. The header file ProfitMeter.mqh should be placed in MQL{4|5}/Include folder.

When indicator is attached to a chart, you can select the crosshair tool and use it as usual, pressing main mouse button and dragging it across the chart. As a result you'll see additional, dynamically updated information near the cursor. Here is a screenshot:

Profit/loss, margin level, drawdown or gain % on the crosshair tool in MetaTrader

Profit/loss, margin level, drawdown or gain % on the crosshair tool in MetaTrader

The information contains lot size (specified in parameters, see below), profit/loss in the account currency, margin level, gain or drawdown percentage.

To perform a virtual buy - just use the crosshair as is. To perform a virtual sell - press and hold Shift key, then use the crosshair tool.

Unfortunately, MetaTrader does not provide MQL programs with information, which tool is active. So, the indicator will actually start processing mouse gestures irrespective to selected tool, but immediately stop itself if inappropriate events are detected (such as entire chart changes, etc).

Parameters

  • Lot - lot size, 0 by default, which means minimal lot;
  • FontSize - font size, 8 by default;
  • OffsetByX - horizontal indentation from the crosshair, 3 pixels by default;
  • OffsetByY - vertical indentation from the crosshair, -3 pixels by default;
  • Color - text color, by default Gray;

Please, note that the code uses symbol properties, which the platform provides for MQL API. Some of the properties can be filled incorrectly on servers, which will lead to inconsistent results. For example, I noticed that some symbols on MQ Demo are reported as SYMBOL_CALC_MODE_CFD, but the platform does internally handle them as SYMBOL_CALC_MODE_FUTURES.

The source code is attached. Feel free to modify it and process all specific cases as you think is right.

Files:
Share it with friends: