17 Lesson17 - Your First Script.pdf
(
370 KB
)
Pobierz
MQL4 COURSE
By Coders’ guru
www.forex-tsd.com
-17-
Your first script
--------------------
I
t was a long path for you to reach this lesson, Congratulations!
You learnt the basics of MQL4 language then you wrote your first indicator and your first
expert advisor.
I hope you enjoyed the journey and interested to continue the road with me.
Today we will create our first script which will simply set the
stoploss
and
takeprofit
values for all the already opened orders on the chart.
It’s useful if you are lazy to set manually your
stoploss
and
takeprofit
values from the
Modify or Delete Orders
window for every order.
What are we waiting for? Let’s script!
What’s a MQL4 script?
The scrip is a program you run once to execute an action and it will be removed
immediately after the execution.
Unlike the expert advisors which will be called every time in a new tick arrival, the script
will be executed only for once.
And unlike the indicators which have the ability to draw lines on the chart, the script has
no access to indicator functions.
In short the script is an expert advisor which you can run only once.
Let's create our first script!
Wizards again!
With the help of the
New Program
wizard we will create the backbone of our scrip.
Bring the
New Program
wizard by choosing
New
from
File
menu or by clicking
CTRL+N hot keys (Figure 1).
We are going to create a script, so choose
Scrip program
in the wizard and click
next.
That will bring the second step wizard (Figure 2).
Fill the Name, Author and Link fields with what you see in the figure 2 (or whatever you
want). Then click finish.
Figure 1 – New Program wizard
Figure 2 – Step 2
By clicking
Finish
button the wizard will write some code for you and left the places to
write your own code, this is the code we have got from the wizard.
//+------------------------------------------------------------------+
//|
My_First_Script.mq4 |
//|
Copyright Coders Guru |
//|
http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright
"Copyright Coders Guru"
#property link
"http://www.forex-tsd.com"
//+------------------------------------------------------------------+
//| script program start function
|
//+------------------------------------------------------------------+
int
start()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Note:
As you can easily notice from the above code that the wizard hasn’t added the
init()
and
deinit()
functions and only added the
start()
function.
That’s because it’s rare to find a script needs to execute code in the program initialization
and de-initialization because the
start()
function itself will be executed for once.
But that’s not mean you can’t add
init()
and
deinit()
functions in a script. You can add
them if you want.
Now, we have to add our code to make our script more useful.
This is the code we have added to the above wizard’s generated code (our added code
marked by the
bold
font):
//+------------------------------------------------------------------+
//|
My_First_Script.mq4 |
//|
Copyright Coders Guru |
//|
http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright
"Copyright Coders Guru"
#property link
"http://www.forex-tsd.com"
#property show_inputs
#include
<stdlib.mqh>
extern double
extern double
TakeProfit=250;
StopLoss=35;
//+------------------------------------------------------------------+
//| script program start function
|
//+------------------------------------------------------------------+
int
start()
{
//----
int
total,cnt,err;
total
=
OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,
SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY)
// long position is opened
{
OrderModify(OrderTicket(),OrderOpenPrice(),
Bid-Point*StopLoss,Bid+Point*TakeProfit,0,Green);
err=GetLastError();
Print("error(",err,"):
",ErrorDescription(err));
Sleep(1000);
}
if(OrderType()==OP_SELL)
{
// short position is opened
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*StopLoss,
Ask-Point*TakeProfit,0,Red);
err=GetLastError();
Print("error(",err,"):
",ErrorDescription(err));
Sleep(1000);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Let’s crack the code line by line as we used to do.
//+------------------------------------------------------------------+
//|
My_First_Script.mq4 |
//|
Copyright Coders Guru |
//|
http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright
"Copyright Coders Guru"
#property link
"http://www.forex-tsd.com"
Here the wizard has written the comment block which contains the script name we have
been chosen, the copyright and link we have been typed.
Then two directive properties have been added according to the date we have entered,
these are the copyright and link properties.
#property show_inputs
We have added the
show_inputs
directive property to our script.
Without this property the script will not accept inputs from the user therefore will not
prompt an input window to the user as the one showed in figure 3.
In our script the user can set the
TakeProfit
and
StopLoss
values from the inputs window
or he can leave the default values we have set in our code.
Note:
If you intend to write a script which doesn’t need inputs from the user, it’s
preferred to remove
show_inputs
directive property.
Figure 3 – Script Input window
#include
<stdlib.mqh>
Later in our code we are going to use the function
ErrorDescription
which returns a
string description of the passed error number. This function is included in the file
“stdlib.mqh”,
so we have included this file to our program using the
include
directive.
That means the content of the
“stdlib.mqh”
is a part of our code now and we can freely
use the
ErrorDescription
function.
extern double
extern double
TakeProfit=250;
StopLoss=35;
Here we have declared two extern variables which the user can set them from the script
inputs window (Figure 2) or he can leave the default values.
We will use these variables in our
start()
function to set the
takeprofit
and
stoploss
values
of all the already opened order.
int
start()
{
//----
int
total,cnt,err;
total
=
OrdersTotal();
....
}
Plik z chomika:
ab31
Inne pliki z tego folderu:
12 Lesson12 - Your First Indicator (Part3).pdf
(391 KB)
11 Lesson11 - Your First Indicator (Part2).pdf
(387 KB)
18 Lesson18 - Working With Templates.pdf
(354 KB)
17 Lesson17 - Your First Script.pdf
(370 KB)
16 Lesson16 - Your First Expert Advisor (Part 4).pdf
(40 KB)
Inne foldery tego chomika:
Indicaters
Zgłoś jeśli
naruszono regulamin