RAQ

How to Grab a Number from Label on a MetaTrader Chart

Q:  Do you know if it is possible to take a number from an object description text and use it as a variable that can be used, for example, as a TP or SL.

Please see link below.  In the image you see an object description of ” EOM: 15″.  I want to pull in the number 15 and use it as a TP or SL.  The number is different each candle and could be different number of digits. Any ideas?

A: Yes, it’s possible. It’s not easy, and it pushes the drag and drop paradigm to the limit, but it can be done!

First off, I created a simple EA that does this, so instead of following steps you can load the system and see how it was done.

Here are the general steps.

Grabbing the description text from a label is pretty easy.  We use the ObjectDescription function from the “Advanced\Object” menu in the VTS Toolbox

Now comes the hard part. By looking at the text, we see there is a colon (“:”) that separates the text from the value:

Avg EOM : 11

We’ll get the position of the colon using the function StringFind function from the “Advanced\Strings” menu in the VTS Toolbox.

Next, we’ll get the length of the entire string using StringLen function from the “Advanced\Strings” menu in the VTS Toolbox.

Now we used the position and length values to get a substring from the larger string. We use the StringSubstr from the “Advanced\Strings” menu in the VTS Toolbox.

We now have the number as a string. But there might be whitespace on the left and right. Eventually we are going to convert the number from a string value to an integer. Any whitespace may mess up the conversion.

We use StringTrimLeft and StringTrimRight to trim out the white space. (Again, from “Advanced\Strings”)

Finally, we are ready to convert the string value to an integer using the function StrToInteger.

We now have an integer that can be used as a stoploss or a takeprofit. In the VTS function fnOpenOrder, for the stoploss or takeprofit, check the “Offset value” check box and set the value as _integerValue.

Here is a excerpt of the MQL code generated by VTS:

_GetLabelText = ObjectDescription(  “SMART_EOM_avgeom” );
_startIndex = StringFind(  _GetLabelText, “:”, 0 );
_totalLen = StringLen(  _GetLabelText );
_stringValue = StringSubstr(  _GetLabelText, _startIndex + 1, _totalLen – (_startIndex -1) );
_strTrimLeft = StringTrimLeft(  _stringValue );
_strTrimmed = StringTrimRight(  _strTrimLeft );
_integerValue = StrToInteger(  _strTrimmed );

You can find and load the VTS system below to see the drawings. Notice how I configured the “message” tab of every function and printed the values on the chart.  This really helps for the kind of code that parses strings.  This is what the output looks like:

 

 

Download the VTS zip here (the MQL code is included as well):  VtsPackageLabelTest.zip

For help loading a VTS zip file see: http://www.iexpertadvisor.com/how-do-i-open-a-vts-zip-file/

 

One more thing!

While creating this system, I found a problem with the StringSubstr function. I will fix this in the next release (4.0.0.80). If you have an earlier version, please download the StringSubstr.zip file, extract it and copy it to this folder on your PC:

C:\Program Files (x86)\iExpertAdvisor\Visual Trader Studio Connect\elements\platform\MT4\functions

StringSubstr,zip

 

 

 

To get notified about the latest questions and answers, follow us!