Post Tagged with: "mql"

Running an Alert-Only Expert Advisor

Running an “Alert-Only” Expert Advisor on your MetaTrader Platform

There are several ways to build and run an Alert-Only Expert Advisor on your MetaTrader platform.

An Alert-Only Expert Advisor is an EA that does not open or close trades when a trade signal is detected, but instead generates an alert.

This is what an alert looks like on the MT platform:

MetaTrader Alert Window

MetaTrader Alert Window

This first method is a bit of a hack, but it does the job. When you attach your Expert Advisor to a price chart, select these options on the Common tab:

  • Allow live trading
  • Ask manual confirmation
MetaTrader EA Common Tab

MetaTrader EA Common Tab

Now, when your EA attempts to open a trade, the MT order confirmation window will appear.

MetaTrader Trade Confirmation Window

MetaTrader Trade Confirmation Window

You can simply cancel the order by clicking the X in the top right corner of the window.

This does not generate an actual alert, but it does prompt you when your trade signal is generated.  It’s not a great solution, but since it requires no work, aside from checking a check-box, I think it’s worth mentioning.

MQL Method for Alert-Only Expert Advisor

If you want to show an actual Alert message from your EA, it’s pretty easy to do in MQL.  The MetaTrader MQL function Alert is easy to use:

Alert(“hello from my EA!”);

Now, if you want to show Alerts instead of opening trades, you would need to manually replace all of the OrderSend MQL function calls with the Alert function call.  This is definitely not an ideal solution.  A better solution would be to add an if/else clause to the EA so your EA can open trades or show alerts.

if( ShowAlerts == true )
      Alert(“hello from my EA!”);
else 
     OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-25*Point,Ask+25*Point,”My order #2″,16384,0,Green);

If the Boolean variable ShowAlerts is declared as an extern type, then it will appear on the Inputs tab each time you run your EA. This way you can make the choice to show alerts or open trades each time you run your EA – without having to modify the MQL code.

MetaTrader EA Inputs Window

MetaTrader EA Inputs Window

 

Using VTS to build an Alert-Only Expert Advisor

Every Element on a VTS drawing has the ability to generate an Alert. This is found on the Message tab, where you can also set the Element to:

  • Write to the Expert tab on the MetaTrader platform
  • Show an Alert
  • Play a sound
  • Send an email
  • Write a message on the price chart

One easy way to create an Alert-Only EA is to set the True End Element on the Open Trade drawing to generate an alert.  This the End Element to configure:

VTS EA Builder Open Buy Drawing

VTS EA Builder Open Buy Drawing

Click on the (+) button to open the configuration window:

  • select the Message tab.
  • check Send Message
  • check Show Alert
VTS End Element Configuration Window

VTS End Element Configuration Window

This will generate an Alert each time the Trade Logic is True.

Important final step.

To run this EA as an Alert-Only EA, simply un-check the Allow live trading check box when the EA is attached to a chart (Just like in the first example above, except un-check the box).

MetaTrader Expert Advisor Common Window

MetaTrader Expert Advisor Common Window

 

Now when you run your EA and the Trade logic is true, you will see this Alert:

MetaTrader Expert Advisor Alert Window

MetaTrader Expert Advisor Alert Window

 

What is the heart of an Expert Advisor?

What is the heart of an Expert Advisor?

Opening and closing trades is not the heart of an Expert Advisor.

Neither is the number of lots traded. And it’s not the value of the stoploss or the takeprofit.

Those parts are important for sure, but they’re mechanical and usually pretty straightforward.

No, the important part of an EA, the very heart of an EA is its logic.

The logic used to open a trade. The logic used to close a trade. Even the logic used to calculate the lot size and the stoploss and the takeprofit.

In VTS, we use Logic Elements to construct the heart of an EA.

I’ve really gone out of my way to make defining the logic easy. For instance, to test for a “cross over”, I created two special operators that you’ll only find in VTS: CROSS_UP and CROSS_DOWN.

To recognize that two lines, say line A and line B, have crossed requires 4 points of information. The values of line A and B now, and the values of line A and B from earlier (maybe from the last  candle).

If the value of A used to be less than B, but now it’s greater, then the lines have crossed.

I write about this in MQL For Traders.  Here is a picture from page 116 that might help. See how the value of the blue line used to be less than the green line (in Period2), but now it is greater (in Period 1.)

 

Cross Over

Cross Over

You wouldn’t realize this unless you really thought about it, huh?

You don’t need to know things like this to use VTS. I suppose it might help, but it’s not mandatory knowledge.

You definitely don’t need to know how to write the MQL code for a cross over. (Honestly, I don’t even like writing that code – it’s confusing and very easy to make a mistake).

You can read more about VTS Logic Elements here:

VTS-Help-Logic

You can easily view the MQL code that’s generated from the CROSS_UP and CROSS_DOWN operators. Just click the Editor button inside of VTS. You can learn more about the Editor here: VTS-Help-editor.