By now you should have a design in hand as well as a basic idea of what the code looks like. In this section, we'll take a more in-depth look at how a program is created. After reading this section, you should be able to understand basic program structure and be able to convert your design to code!
An Overview
There are two basic parts to a program:
* Variables - These are items that hold data. This can be data that you collect from the user, or any other data.
* Statements - These form the core of the program. Statements manipulate the data to get results that can be converted to actions.
In addition to these core components, there are also several optional components:
* Functions - These are simply collections of related statements that can be used to perform a specific task. For example, a function telling you when to buy might include a statement to check whether you have enough money, a statement to determine whether it meets your criteria and a statement to place the order. A function combines these, allowing you to simply call on the function instead of rewriting these statements each time you want to buy.
* Arrays - These are simply data structures that hold similar data and enable you to access and manipulate the data more efficiently.
A Look at Variables
Variables are simply objects that you define to hold data. You may recall from the previous section that we used three variables: MATrendPeriod, MaCurrent and MaPrior. MATrendPeriod held a number that defined how many days we would use in our moving average calculation; MaCurrent held a number representing the current moving average; and MaPrior held a number representing the prior moving average.
Creating a Variable
You can use almost any name you want when naming a variable. The only exception is a list of ''restricted words'' that you are not allowed to use because the names are already used by other parts of the program. You can find your trading program's list of restricted words in the program's documentation. In general, names should describe the data being held. For example, notice that we used MaCurrent to define the current moving average.
After you have created a name, you must declare and define the variable. Declaring a variable tells the computer what type of data it is, and tells it to make space for that data. Defining the variable is where actual data is assigned, or added, to the variable. Let's take a look at these processes:
1. Declaring a Variable
In MetaTrader, variables are declared automatically when you assign information to them. In other programs, you may have to declare a variable, which is typically done using the following format:
;
The two types of data are numbers and text, but these are broken down into more groups like integers (whole numbers), double (large numbers), float (decimal numbers), string (text), and others depending on the program you are using. For example, the following code will declare numberOfDays as an integer:
Int numberOfDays;
2. Defining a Variable
After your variable has been declared, the computer has created space for it. Now, all you have to do is add actual data to that space. This can be done in two ways: you can either define a set amount, or you can perform a calculation to obtain a value, which you then assign to the variable.
In MetaTrader, you can add set data using the following format:
Defines:();
In other programs, set data is often assigned simply using the equals sign:
= ;
If you want to perform a calculation to obtain data to assign to the variable, then you simply assign the variable to the calculation:
= ;
For example, to set a 20-day moving average in MetaTrader, we use the following code:
= iMA(20,MODE_SMA,0);
Note that the iMA(20,MODE_SMA,0) portion of the code is the calculation. The format for this calculation was developed by MetaTrader and will differ if you are using another trading program. To find these calculations, you must consult your trading program's documentation, which usually contains a list of all available calculations.
3. Using Variables
Once declared and defined, variables can be used anywhere else within the program to represent the data they contain. To do this, simple type the name of the variable in place of the data. For example, if MATrendPeriod contains the number of days we want a moving average calculated for, we can use it to replace the 20 in our example above:
= iMA(MATrendPeriod, MODE_SMA, 0);
There are two advantages to using variables as opposed to just the data: (1) you can change the data in one place, and (2) the result of an entire calculation can be contained within one variable.
A Look at Statements
Statements are the core of any program - they contain all of the commands that manipulate data to make decisions. Here we will take a look at several of the most common types of statements and how they can be used.
1. Comments
If you have designed a complex trading system, it may take a lot of code to implement your rules; therefore, it would be prudent to insert comments in your code to help yourself understand it in the future, and to help out anyone with whom you may share your code. Almost all trading applications share a similar method for creating comments:
Single Line Comments:
//
Multi-Line Comments:
/*
*/
2. The 'If' Statement
This is the statement you will use most when coding a trading system. This statement lets you create scenarios as we did in the design portion of this tutorial. You may have also noticed that this was the only statement we used in the example program we created. This type of statement is implemented using the following format:
Standard If/Then:
If Then ;
Standard If/Else:
If Then Else ;
So, for example:
If accountBalance <>
An Overview
There are two basic parts to a program:
* Variables - These are items that hold data. This can be data that you collect from the user, or any other data.
* Statements - These form the core of the program. Statements manipulate the data to get results that can be converted to actions.
In addition to these core components, there are also several optional components:
* Functions - These are simply collections of related statements that can be used to perform a specific task. For example, a function telling you when to buy might include a statement to check whether you have enough money, a statement to determine whether it meets your criteria and a statement to place the order. A function combines these, allowing you to simply call on the function instead of rewriting these statements each time you want to buy.
* Arrays - These are simply data structures that hold similar data and enable you to access and manipulate the data more efficiently.
A Look at Variables
Variables are simply objects that you define to hold data. You may recall from the previous section that we used three variables: MATrendPeriod, MaCurrent and MaPrior. MATrendPeriod held a number that defined how many days we would use in our moving average calculation; MaCurrent held a number representing the current moving average; and MaPrior held a number representing the prior moving average.
Creating a Variable
You can use almost any name you want when naming a variable. The only exception is a list of ''restricted words'' that you are not allowed to use because the names are already used by other parts of the program. You can find your trading program's list of restricted words in the program's documentation. In general, names should describe the data being held. For example, notice that we used MaCurrent to define the current moving average.
After you have created a name, you must declare and define the variable. Declaring a variable tells the computer what type of data it is, and tells it to make space for that data. Defining the variable is where actual data is assigned, or added, to the variable. Let's take a look at these processes:
1. Declaring a Variable
In MetaTrader, variables are declared automatically when you assign information to them. In other programs, you may have to declare a variable, which is typically done using the following format:
The two types of data are numbers and text, but these are broken down into more groups like integers (whole numbers), double (large numbers), float (decimal numbers), string (text), and others depending on the program you are using. For example, the following code will declare numberOfDays as an integer:
Int numberOfDays;
2. Defining a Variable
After your variable has been declared, the computer has created space for it. Now, all you have to do is add actual data to that space. This can be done in two ways: you can either define a set amount, or you can perform a calculation to obtain a value, which you then assign to the variable.
In MetaTrader, you can add set data using the following format:
Defines:
In other programs, set data is often assigned simply using the equals sign:
If you want to perform a calculation to obtain data to assign to the variable, then you simply assign the variable to the calculation:
For example, to set a 20-day moving average in MetaTrader, we use the following code:
Note that the iMA(20,MODE_SMA,0) portion of the code is the calculation. The format for this calculation was developed by MetaTrader and will differ if you are using another trading program. To find these calculations, you must consult your trading program's documentation, which usually contains a list of all available calculations.
3. Using Variables
Once declared and defined, variables can be used anywhere else within the program to represent the data they contain. To do this, simple type the name of the variable in place of the data. For example, if MATrendPeriod contains the number of days we want a moving average calculated for, we can use it to replace the 20 in our example above:
There are two advantages to using variables as opposed to just the data: (1) you can change the data in one place, and (2) the result of an entire calculation can be contained within one variable.
A Look at Statements
Statements are the core of any program - they contain all of the commands that manipulate data to make decisions. Here we will take a look at several of the most common types of statements and how they can be used.
1. Comments
If you have designed a complex trading system, it may take a lot of code to implement your rules; therefore, it would be prudent to insert comments in your code to help yourself understand it in the future, and to help out anyone with whom you may share your code. Almost all trading applications share a similar method for creating comments:
Single Line Comments:
//
Multi-Line Comments:
/*
2. The 'If' Statement
This is the statement you will use most when coding a trading system. This statement lets you create scenarios as we did in the design portion of this tutorial. You may have also noticed that this was the only statement we used in the example program we created. This type of statement is implemented using the following format:
Standard If/Then:
If
Standard If/Else:
If
So, for example:
If accountBalance <>
0 comments:
Post a Comment