Script basics

Please see: http://forums.babypips.com/expert-advisors-automated-trading/51473-automated-trading-howto-mt4.html when you don’t understand where this relates to.

First I want you to introduce to the basic template I am using for the thread. This is the most basic as it egts and it will grow over time. I just want to make sure that we have the same starting point. Each script shared will run on its own, so there is no need to copy and paste stuff if you want to use only that script. When you want to make your own, you need to copy sections and paste them in you own script.

The basic template I use is the standard EA template generated by Metaeditor, the scripting and compiling tool that comes with your MT4. It is called: metaeditor.exe or you can find it by clicking F4, when you are in your MT4 program.

Create a new template in metaeditor, and you can choose from a selection. The one used here is the ‘Expert Advisor’, and it will look like the file I attached.

There are more templates, the ones you probably will only use when you are not too fancy are:

  • Expert Advisor
  • Custom Indicator
  • Script.

I’ll explain the difference in a very simplified manned:

  • Expert Advisor: runs continuous on the chart you attached it to. It can include indicators and is able to trade (when coded).

  • Custom Indicator: runs continuous, but can’t trade. You can make your own indicator, just like the moving averages ons in MT4. I build EAs, so I won’t be looking at these so often.

  • Scripts: Like EAs, but they only run one cycle. It does what is asked and it stops. I will use these in my thread to set some variables, like to turn the EA on or off.

Each have their own location directory:

  • Expert Advisor: C:\Metatrader4\experts
  • Custom Indicator: C:\Metatrader4\experts\indicators
  • Scripts: C:\Metatrader4\experts\scripts

Files MUST be located in these directories to work. They will only work when compiled, so you need to open them in metaeditor.exe and click on F5.

Please, open the attached file in Metaeditor and have a look at it, I will explain some more later. With F5 in Metaeditor, you compile the script, meaning that it can be executed by MT4. A copy of the file will be saved with the extension .EX4. Scripts that you can edit have the extension .mq4

Line_Notification.zip (440 Bytes)

Okay, the first tool.

This little EA helps you to set Alerts using visible lines. When you attach it to a chart it will show two lines that you can set at the desired price levels. When done, the script will alert you when the line is hit. It uses notifications, so set that up. You can find the link in the main thread.

I want to have a thread on how to attach an EA to a chart, but I give priority to giving out tools for you. So I haven’t made that thread yet. Please, google it for now. I am sure there are manuals or youtube movies describing it. Sorry for that, I will add that thread later. You need to compile the file first for it to work. I only provided the .mq4 file.

Okay, let us have a look at the script:

//±-----------------------------------------------------------------+
//| Line_Notification.mq4.mq4
//| Toekan - <[email protected]>
//| http://forums.babypips.com/expert-advisors-automated-trading/51473-automated-trading-howto-mt4.html
//±-----------------------------------------------------------------+
#property copyright “Toekan - <[email protected]>”
#property link “http://forums.babypips.com/expert-advisors-automated-trading/51473-automated-trading-howto-mt4.html

//±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
//----
The text marked purple isn’t in the script. But I added it in the post. When you compare the basis script in the last post with this one, you’ll see that I added the green text. The text here is located in the expert initialization function. This section instructs MT4 to do before the actual script is starts running. Here I instructed to create two lines. You can see what the lines do by reading the comments that, those are the lines that start with //

//Creats a line 
ObjectCreate("AlertUp", OBJ_HLINE , 0,TimeCurrent(), (Bid+0.003));
//Gives the line a name you can see
ObjectSetText("AlertUp", "AlertUp", 10, "Arial", Red);
//Gives the line a color.
ObjectSet("AlertUp", OBJPROP_COLOR, Lime);

ObjectCreate("AlertDown", OBJ_HLINE , 0,TimeCurrent(), (Bid-0.003));
ObjectSetText("AlertDown", "AlertDown", 10, "Arial", Red);
ObjectSet("AlertUp", OBJPROP_COLOR, Red);

//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
//----
In this section, the expert start function, is where the action happens when your script is running. The script will carry out all instructions for every tick that happens on the chart. Now, this is simplified as ticks can be missed while the script is carrying out the instructions, but that is too much detail for now.

The script instructs MT4 to:

  • See if the lines exist
  • See if the Bid-price is above the AlertUp line or below the AlertDown line.
  • When this happens it will send a notification with the text “Alert!”.
  • When that is done, it will delete the two lines. So

//Does the line AlertUp exist, otherwise the Alert will not be triggered. This is needed as otherwise the Bid is ALWAYS above the line, as its value will be zero.
if(ObjectFind(“AlertUp”) == 1)
{
//Determine if the Bid is above or below one of the two lines.
if(Bid > ObjectGet(“AlertUp”, OBJPROP_PRICE1) || Bid < ObjectGet(“AlertDown”, OBJPROP_PRICE1))
{
//Send a notification
SendNotification(“Alert!”);
//Delete the objects (or the alert will be triggered again.
ObjectDelete(“AlertUp”);
ObjectDelete(“AlertDown”);
}
}
//----
return(0);
}
//±-----------------------------------------------------------------+

That is it for now. I just wanted to show a simple script and tell something about the sections. Also to make it practical for you, I wanted to add a usable automation tool for you to use. It is simple, but it can be helpful.

Line_Notification.zip (854 Bytes)

[Reserved for more theory]

Good stuff toekan…

Great thread, playing with it now. Thanks