Trader's Kit: Drag Trade Library

TheXpert | 11 May, 2012


Introduction

At the time of development, there already existed some products that to a certain extent implemented the functionality provided by the library or had similar features. I cannot say whether the library is better or worse than those. It loses out to some of them in functionality, e.g. to well-known Autograph, and to others - in convenience. However Drag Trade Library has its peculiarities and the product will hopefully be in demand.


1. Purpose of Development

It was initially planned to create an Expert Advisor that would trade and handle orders directly on a chart using the mouse and control objects while retaining user friendliness and a minimalistic interface. That said, a major feature of the Expert Advisor was the fact that it was developed entirely in the original language of the terminal being therefore safe for the users since it could not contain malicious code. Besides, the Expert Advisor was originally intended to be free software.

Work on the project gave rise to the library that combined the above features and could easily be integrated into virtually any EA. In addition, the library code contains a lot of useful functions that can be utilized outside the library framework.


2. Structure

The library functionality can be divided into four blocks:

  1. Trading block
  2. Information block
  3. Order handling block
  4. Settings block


Fig. 1. Library functionality

The EA's functionality is displayed based on the same block division principle - user interface is divided into 3 independent blocks that can be used jointly and independently by disabling and enabling the required blocks as may be necessary during operation. Moreover, this approach allows to save resources getting wasted when processing unnecessary logical blocks or to even use minimum resources when functionality blocks are disabled. All functionality blocks are disabled by default.


2.1. Principles of enabling/disabling blocks

All block drawing and processing functions are located in the Expert Advisor. However the information and most control and information objects are not drawn in the main tool window. The drawing space is provided by pseudoindicators that also serve to enable and disable the functionality blocks. This means that if a certain indicator is present in the chart, the respective functionality block of the Expert Advisor is enabled and operating.

The indicators as such are not used. Their only purpose is to provide drawing space.


Fig 2. Auxiliary indicators

There are some limitations on the use of indicators. Any external indicators can be dragged on to pseudoindicator windows but not the other way round as the presence of a window is determined by the pseudoindicator name and the respective functionality block will simply not get enabled.


3. User Interface

3.1. Expert Advisor settings

The settings were taken out of the library to form a separate functionality block. This enabled us to actually single out and place the entire functionality in the library and make settings externally both before and during the operation of the Expert Advisor. In addition, the settings can be divided into separate logical blocks without skimping on specification.

The process itself consists in applying the appropriate script to the required chart.

At the moment, there are 3 setting scripts:

The script provides a description for every property so there should be no problem figuring out how they are set.

The implementation of this feature is based on the use of global variables of the terminal for storing the settings. This imposes some limitations on its use - lines cannot be passed in this manner as the maximum size of transmitted information is limited and equals to the double type, i.e. 8 bytes.

An example of setting the Expert Advisor properties can be seen below. Settings for comment and object output are changed for a running Expert Advisor:

To be able to freely test strategies in the tester without being afraid of resetting or changing parameters for online trading, there is a different parameter passing for online and test mode. Setting the parameters for the one will not affect the other.

Therefore, if you want to change the default parameters in the tester, you should use the similar scripts for the test mode:

3.2. Functionality

So, the interface is divided into 3 independent areas which can be disabled:

As a whole it looks like this:


Let us have a more detailed look at every one of them.

3.2.1. Information panel

The information panel is an area for displaying information. This is the most uncomplicated part of the functionality. It displays general account or instrument information as well as current library settings.

Fig. 3. Information panel

Information for display can easily be expanded which will however require modifications of the library code.

If it does not scare you off, the functionality is represented by the following functions: void ClearInformation() and void DrawInformation()

3.2.2. Panel of orders

This panel displays information on current (open) instrument orders and contains controls for handling them.

Fig 4. Panel of orders

What you can do by enabling this panel:

The following video provides a graphic demonstration of the functionality of this panel:

3.2.3. Trading panel

This panel is used to display trading functionality. It is the most important and necessary panel as it contains the main functionality of the library. Getting this panel enabled gives you an opportunity to place orders and handle them using certain controls directly on the chart.


Fig. 5. Trading panel

What you can do by enabling this panel:

Market orders are placed as follows:

A market order is closed by simply dragging any of the stop levels to the other side of the price.

The video below shows examples of handling market orders

Handling buy orders:




Handling sell orders:




Pending orders are placed as follows:

A pending order is closed by simply dragging the order line to the other side of the price.

The video below shows examples of handling pending orders.

Handling stop orders:




Handling limit orders:



4. Integration into the Expert Advisor

And finally, the most interesting part.

It is in fact very simple. The integration process is considerably simplified due to the fact that only the necessary minimum of settings is passed to the library during integration; the remaining settings are covered by external scripts.

The simplest example of an Expert Advisor can be demonstrated by a shell turning the library into an Expert Advisor. It will appear as follows:

//+------------------------------------------------------------------+
//|                                      DragTrade_Example_v_1.0.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, TheXpert"
#property link      "theforexpert@gmail.com"

extern int      Magic          = 1111;
extern int      EAMagic        = 1234;
extern int      TimesToRepeat  = 3;
extern int      Slippage       = 3;

#include <DragTrade_v_1.0/DragTradeLib_v_1.0.mqh>

bool Testing;

int init()
{
   Testing = IsTesting();
   DragTrade_Init(Magic, EAMagic, TimesToRepeat, Slippage, "Drag Trade Example");

   Comment_("Test Example of using Drag Trade Library");

   return(0);
}

int deinit()
{
   DragTrade_Deinit();
   return(0);
}

int start()
{
   DragTrade_Start();
   
   return(0);
}

The above code can be applied as a ready to use Expert Advisor to trade using the mouse.

Events in this code are handled with every new tick.

The main library integration points:

  1. DragTrade_Init
  2. DragTrade_Deinit
  3. DragTrade_Start

The DragTrade_Init function is as follows:

   void DragTrade_Init(int magic, int eaMagic, int timesToRepeat, int slippage, string ordersComment = "");

Let us have a detailed look at the parameters to be passed:

If you want a quick execution of commands, the EA's start function can be looped:

int start()
{
   while (!IsStopped())
   {
      DragTrade_Start();
      Sleep(200);
   }
   return(0);
}

It will however be impossible to test such an Expert Advisor in the tester - it will freeze up.

A solution can be found by making a provision for a different behavior of the tester:

int start()
{
   if (Testing)
   {
      DragTrade_Start();
   }
   else
   {
      while (!IsStopped())
      {
         RefreshRates();
         
         DragTrade_Start();
         Sleep(200);
      }
   }
   
   return(0);
}

The standard package attached to the article contains this particular simplest Expert Advisor.

Besides that, the package includes a standard Expert Advisor MACD Sample featuring the integrated library and the library trading functions.

Apart from the functions required for integration, the DragTradeLib_v_1.0.mqh file provides a whole set of functions that may prove useful in the implementation of trading strategies which are used by the library and can be used to implement your code.


5. Library Installation

The installation is simple - you need to unzip the attached archive into your terminal folder, restart the terminal if it was working, so that the executable files appear in the list of available files in the terminal.


6. License

/* =============================================================================================== */
/* Copyright (c) 2010 Andrey Nikolaevich Trukhanovich (aka TheXpert)                               */
/*                                                                                                 */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this software   */
/* and associated documentation files (the "Software"),                                            */
/* to deal in the Software without restriction, including without limitation the rights            */
/* to use, copy, modify, merge, publish, distribute,                                               */
/* sublicense, and/or sell copies of the Software, and to permit persons                           */
/* to whom the Software is furnished to do so, subject to the following conditions:                */
/*                                                                                                 */
/*                                                                                                 */
/* The above copyright notice and this permission notice shall be included                         */
/* in all copies or substantial portions of the Software.                                          */
/*                                                                                                 */
/* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS                        */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,                     */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR          */
/* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER                                     */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR         */
/* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                   */
/* =============================================================================================== */

Conclusion

The library was tried out while working with a real account and proved itself to be a useful, reliable and stable tool.

Once again, I would like to express my profound gratitude to Viktor Khabibulaevich Rustamov. He was the originator of the idea of creating the library, as well as a tester and a valuable source of advice in the implementation of the idea.

There is quite a lot of code so small errors and faults are quite likely to be present. I would really appreciate if you let me know about them.

And I will try to provide feasible (not guaranteed) code maintenance. Further project developments under the license terms by other people will get my full support. Any comments and suggestions with regard to the library will be taken into account to the extent possible in future versions.

Good luck and trade easy!