Post Tagged with: "metatrader"

Special MetaTrader Functions: init, deinit and start

Special MetaTrader Functions: init, deinit and start

If you have ever used the MetaEditor to build an Expert Advisor, you may have noticed that the editor automatically builds 3 functions for you: init, deinit and start. This is what they mean:

  • init: This function is called when the Expert Advisor is first attached to a chart, or whenever the EA is reset and the input variables were changed.
  • deinit: This function is called when the Expert Advisor is removed from a chart.
  • start: This function is called on every incoming tick.

In VTS, the main system drawing represents the start function.  The main system drawing is the drawing tab with the same name as the VTS system, for example ‘system1″.

On each new incoming tick of price data your MT platform receives from your brokers server, execution begins at the Start Element of  the main system drawing.

How to Use the init and deinit Functions in Visual Traders Studio

VTS supports a drag, drop and connect approach to building an EA. You connect the Elements on the Drawing Pad in the way that you expect them to execute.  The drag, drop and connect approach does not support the concept of the init and deinit functions cleanly because those functions are not executed on each tick. The init and deinit functions are only executed when the EA is started and stopped.

An Elegant Solution for supporting init and deinit

Don’t worry, we have an elegant solution. If you need to use either the init or deinit functions in a VTS system, simply create a drawing named init or deinit , add the drawing Element to the main system drawing and don’t connect it.

Here are the steps:

  • From the New Elements tab in the Toolbox, drag a Drawing Element Icon onto the Drawing Pad
  • Name the Drawing init (leave the return value as integer and you probably don’t want to add a logic element, so un-check that box)
  • Drag, drop and connect any Elements needed on the init drawing
  • Save the init drawing (click the Save button in the top left corner of VTS)
  • Locate the init drawing in the Toolbox under:  Functions->System Functions->Name of your system->init
  • On the main system drawing, drag and drop the init function Element anywhere. Do not connect it to any other Elements!
  • Done.

If you connect the init Element, it will be executed on each tick, like any Element connected on a drawing. That’s not what you want.

(deinit is handled the same way.)

Normally, any Element not connected on a VTS drawing will not be included in the generated MQL code.  (This feature is a handy way to temporarily remove some functionality from a system).

But the init and deinit functions are handled differently. If they exist on the main system drawing, they will be included in the generated MQL.  Also, VTS often makes use of the init function (especially the plug-ins), but you don’t have to worry about any conflicts. VTS will combine your drawing Elements with its own internal MQL code into a single complete init (or deinit) function.

What are init and deinit used for?

If you don’t have a compelling reason to use these functions then you probably don’t need to be concerned with them.

The init function is good for resetting values and clearing of objects. For example, the VTS TrendLine plug-in may draw trendlines on the chart. There are functions in the TrendLine library to delete all of the trendlines and their labels. You can add those functions to your init or deinit drawing to clear the chart whenever you start or end the EA.

Also, I have found when I am testing an EA, it’s handy to add the fnCloseAll function onto the deinit drawing so that all trades are closed when I finish testing the EA.  This is on a demo account only of course!

Here is a drawing showing how it looks to use  init and deinit on the main system drawing:

MetaTrader init and deinit functions on main system drawing of VTS



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!”);
     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