Modifying an Indicator on the fly

Post Reply
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Modifying an Indicator on the fly

Post by gaheitman »

Attached is a version of the BasketEquityLive indicator that allows for virtual trades to be added on the fly. I'm posting it here because I was hoping to discuss the way it works and not immediately how to use it in trading.

Instead of using the Inputs dialog to add virtual trades, I decided to create a control interface into the indicator via messages embedded in global variables. The commands the indicator currently "knows" are:

GET_EQU - return the current equity
GET_VER - return the indicator version
GET_RDY - test whether all data is available and equity calculation is valid
SET_MAP - change the Moving Average period
SET_MAT - change the Moving Average type
SET_RDW - force a redraw of the indicator
SET_BUY - add a buy trade
SET_SEL - add a sell trade
SET_RST - delete all virtual trades and reset indicator

When the indicator sees a new command, it does the following actions:
  • creates a new global variable to save the command
  • performs the command
  • deletes the command global variable
  • creates a response global variable
The format of the command global variables is:

Code: Select all

?[AAA]CMD_NAM{parameters}
where the question mark signifies it is a command to be processed by the indicator. The "[AAA]" is a command code that is set in the indicator's settings, and it must be three characters long. This allows you to have multiple copies of the same indicator with different options at the same time. Think of it as a simple password to make sure that nothing accidentally gets a command.

The saved copy is the same as the command, but is prefixed by an asterisk instead of a question mark.

Code: Select all

*[AAA]CMD_NAM{parameters}
The format of the response global vairables is:

Code: Select all

![AAA]CMD_NAM{parameters}{OK}
I save off a copy of the commands so that events that cause reloading of the indicator (changing timeframes, etc.) do not destroy the configuration that another program sent. When this happens, the code converts them to normal commands, deletes them and processes the normal commands.

So, an example conversation to get the current equity would be:

Requester creates a global variable:

Code: Select all

?[AAA]GET_EQU
The indicator sees the global variable and creates a new global variable:

Code: Select all

![AAA]GET_EQU{OK}
which has the current equity as its value. It then deletes the original command global variable.

A more complex example is the setting of a virtual trade. This requires passing the Direction, Symbol, Open Price, Lots and Open Time

The command looks like this:

Code: Select all

?[AAA]SET_BUY{EURJPY|103.33800000|1.0|1353085440}
I've attached a couple of scripts you can use to play with controlling the indicator. VirtBuy and VirtSell will add a virtual trade in the direction stated when you drag and drop it on a chart. It uses the current chart symbol, and the price/time where you drop it for the entry information. For example, VirtBuy is simply:

Code: Select all

int start()
  {
   
   GlobalVariableSet("?[AAA]SET_BUY{"+Symbol()+"|"+WindowPriceOnDropped()+"|1.0|"+WindowTimeOnDropped()+"}",0);
   
   return(0);
  }
In a real example, an EA would want to do a reset, add the trades, and check that the data was ready before getting the equity.

I still need to add commands to get the current MA value and the current Equity-MA value. I may also add commands to delete trades, but we'll see.

This may be nothing more than idle hands coding a solution in search of a problem. It wouldn't be the first time. :lol:

George
You do not have the required permissions to view the files attached to this post.
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Modifying an Indicator on the fly

Post by dietcoke »

It's certainly an idea worth investigating George.

I use a GV to switch logging on/off in flight.
I can think of plenty of situations where you might want to change an EA in flight, particularly in multipair ea's where you only want to change a global setting for one pair or CCY code. eg.

temporarily stopping trading.
changing stops or targets from their normal settings


However, I find GV's frustratingly incomplete. eg,
The value must be a number.
It's quite clumsy looking through them and manipulating them as they do not integrate well into MQL's other data structures.


One thing I found was that there is a bug in GV's where you are unable to delete a variable with more than a certain number of chars in the name.

I think it allows you to create a gv with 64 characters but you cannot delete it. A GV with 63 characters appears to be the longest that can be removed.

I came across this during an attempt to use GV's to hold delimited lists in the same way you are using them, so you need to be careful not to make the commands too long.
Image
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: Modifying an Indicator on the fly

Post by gaheitman »

dietcoke wrote:It's certainly an idea worth investigating George.

I use a GV to switch logging on/off in flight.
I can think of plenty of situations where you might want to change an EA in flight, particularly in multipair ea's where you only want to change a global setting for one pair or CCY code. eg.

temporarily stopping trading.
changing stops or targets from their normal settings


However, I find GV's frustratingly incomplete. eg,
The value must be a number.
It's quite clumsy looking through them and manipulating them as they do not integrate well into MQL's other data structures.


One thing I found was that there is a bug in GV's where you are unable to delete a variable with more than a certain number of chars in the name.

I think it allows you to create a gv with 64 characters but you cannot delete it. A GV with 63 characters appears to be the longest that can be removed.

I came across this during an attempt to use GV's to hold delimited lists in the same way you are using them, so you need to be careful not to make the commands too long.
Another option was using text labels off screen, but they would only be available to that chart. There is a generic implementation of that at mql4.com.

I agree that the limitations are frustrating. Why would they make it so you can only use numbers? I don't think the 63 characters will be too big of a deal, but thanks for bringing it up. I have hit that limit on text labels before, and it was very annoying.

George
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Re: Modifying an Indicator on the fly

Post by Radar »

Hey George,

Have you looked at MT4i's QuickChannel for communication between EA's, scripts, and indicators in either the same instance of Empty4, or across multiple instances on the same machine?

Check it out here... http://www.mt4i.com/appstore/category.a ... popularity

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
Post Reply

Return to “Coders Hangout”