MQL / Using VTS

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

 

 

  iExpertAdvisor - MetaTrader Expert Advisor Builders
Tags: /

2 Comments

  1. Great stuff,
    The only thing missing is a couple of examples to show creating one, a vid or an email tut.
    So eg. The close all function would be inside the deinit() function ? Does it (closeall() have to be linked to something ?

Leave a Reply

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

*