Introduction

Our goal as algotraders, is to create an autonomous trading algorithm with as little as possible manual intervention. An algorithm which can run by it’s own on a certain product while we are doing something else, letting the money work classic paradigm.

Alas, sometimes life is not the way we want to be and the algorithm we design, programmed and tested behaves completely different under certain market conditions. But you’re not always next to your PC, you’re not always able to perform or change something as you have your own life. Assuming you’re sitting in your favourite bar with friends and suddenly some news event happens which impacts significantly the volatility, what can you do then?

I had the same scenarios over and over again, where I couldn’t control my expert adviser without logging into my PC or to the remote PC to make the actual change, which was really frustrating and time consuming. I was thinking again and again, why isn’t there a way to signal the EA to do something and move on, how can I “speak” with the EA even I am 100km away…

Solution

I realised that the solution is much simpler than I thought. Basically, my idea was having a listener on the number of the orders opened, and each time a new order was added, I would check whether it’s an ordinary order or an instruction order. Where an instruction order is a normal pending order with a dedicated range of prices, in my case 2000-… (I am trading mainly FX products who are not in this range of pricing).

Setup

Before looking at the code itself, you must make sure that you enabled notifications in Metatrader. Doing so is quite straightforward, select from the menu tools -> options and navigate to the notifications tab:

Notifications

In there, tick the “Enable Push Notifications” and enter your MetaQuotes ID, a value which can be found in the mobile application you’re using. In iPhone the ID is found at the bottom of the “Chat and Messages Page”:

Meta Quotes ID

Once you entered the ID details in Metatrader, click test and expect to see a message in your mobile device, if not, double check your input.

Orders Number Listener

As the title suggests, this piece of code listens to the Orders number and acts upon a change:

if(OrdersNumber!=OrdersTotal())
{
  OrdersNumber=OrdersTotal();
  ProcessRemoteInstructions();
}

I place it in the OnTick function and check whether the Orders Number changed from the last tick, normally I check every minute as I don’t expect to have so many instructions coming in.

OrdersNumber is a global integer variable which I initialise as part of the OnInit function to the current OrdersNumber value:

OrdersNumber=OrdersTotal();

So the number did change, I store the new value and process the remote instructions if any.

Processing Remote Instructions

Now that we are listening on the orders number, it’s time to do something if the number changed. In the following function, I run over the orders and if it is a pending buy sell order with a price in the range of 2000… I execute according to predefined action:

void ProcessRemoteInstructions()
 {
 for(int i=OrdersTotal()-1;i>=0;i--)
 if(OrderSelect(i,SELECT_BY_POS) && StringCompare(TheSymbol,OrderSymbol())==0 && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT))
 {
 switch((int)OrderOpenPrice())
 {
 case 2000:
 {
 OrderDelete(OrderTicket());
SendNotification(StringConcatenate(
 "1 Remove expertn"
 "2 ..."
 ));
 break;
 }
 case 2001:
 {
OrderDelete(OrderTicket());
SendNotification("Incoming request to remove the expert, exiting...");
 ExpertRemove();
 return;
 }
 case 2002:...

In this small example, I loop through the open orders looking for pending orders. If the pending order is in the price range of 2000+ I act upon it and delete it. In this case, I have two scenarios:

  • 2000: In this case just send a notification to the mobile device with all the avaialble remote instructions.
  • 2001: Terminate the expert adviser.

Usage

Coming back to our scenario. Assuming I am sitting in a bar chilling out after a long day of trading and suddenly I see something I don’t like in the market. All I need to do now is open a pending order with the price of 2001 and the expert adviser will pick it up and terminate itself so I can go back and relax (instead of  login into the remote host and do the same, which is not straightforward).

Of course, I gave here a simple example of a remote instruction. In my expert adviser, I tailored the instructions to my trading style, there in an instruction to open up a specific order, or open a hedge, calculate volatility and so on. But the pattern is still the same:

  1. Open a pending order with a specific price
  2. The expert adviser picks up the instruction and execute remotly
  3. The expert adviser sends a notification open receipt / completion.

Hope it helps, for any questions don’t hesitate to comment here or contact me per telegram / email.

This entry was posted by . Bookmark the . Titled

Leave a Reply

Your email address will not be published. Required fields are marked *