|
Using the Database keyword it is possible to have access to data of a specific security inside a workgroup. This is useful to verify if a security responds to several rules when validating a trading system signal. Also when building a trading system on different time frames.
To use external data is it necessary to declare it first. Declaration is done at the beginning of the module, outside any function, where all other module variables are declared. The syntax is the following:
Database DatabaseName ( "Workgroup Name", "Security symbol", "Compression Type", Minutes Periods other compression)
For Example:
Database Microsoft("Nasdaq", "MSFT", "N")
open a database named "Microsoft" assigning to it security data contained on the workgroup "Nasdaq" for the symbol "MSFT". Data have normal compression "N".
Arguments Compression Type and Minutes/Periods other compression are optional.
If Compression Type is not defined, then the default compression is "N" (Normal).
Possible compressions are:
N => Normal D => Daily W => Weekly M => Monthly Y => Yearly MIN => Minutes O => Other
The argument Minutes/Periods other compression is used, therefore is mandatory only in the case the choosen compression is Minutes (MIN) or Other (O).
Once the database has been declared it is possible to use it inside any functions, accessing the data using the standard function:
DbValue (Data array, "Database Name", By date (optional))
returns the numeric array or the value of Data Array calculated referring to the security specified on the database declaration
and where:
Data Array is a numeric array. It can be a data array, like Close, an indicator function etc.
Database Name is the name of the previously declared database which is used to calculate the Data Array
By date defines if the numeric value is being calculated based on the current period date or not. By date will avoid to obtain wrong values. It could happen that the two data sets are not aligned and a period from the security being elaborated do not match the period of the security from the external data declared in the Database. In practise could occur that the current period does not have a corresponding date/time period on the external security. If "By date" is set to True, the function will try to find a corresponding period on the external security by Date/Time and will not try to match just its position. Default is True. The parameter is ignored when DbValue returns an array.
For Example:
Database Microsoft("Nasdaq", "MSFT", "N")
Function Main() Dim dMyValue As Numeric Dim Msclose As Numeric() Dim MyRsi As Numeric()
dMyValue = DbValue(Close, "Microsoft")
Msclose = DbValue(Close, "Microsoft")
MyRsi = DbValue(Rsi(Close, 14), "Microsoft")
EndFunction
The line:
Database Microsoft("Nasdaq", "MSFT", "N")
open a database named "Microsoft" assigning to it security data contained on the workgroup "Nasdaq" for the symbol "MSFT". Data have normal compression "N".
The line:
dMyValue = DbValue(Close, "Microsoft")
assigns the close value of Microsoft for the current date/time to the numeric variable dMyValue
The line:
dMyValue = DbValue(Close, "Microsoft", False)
assigns the close value of Microsoft for the position of the current period to the numeric variable dMyValue
The line:
Msclose = DbValue(Close, "Microsoft")
assigns Microsoft close array to the numeric array Msclose
The line:
MyRsi = DbValue(Rsi(Close, 14), "Microsoft")
assigns the array of the RSI indicator calculated on the Microsoft close to the numeric array MyRsi
Another example:
To see if the external Microsoft security for the data being elaborated has a RSI value grater than 70 we could write:
If (DbValue(Rsi(Close, 14), "Microsoft") > 70) Then
It is possible to calculate any numeric expression using the external data set with the standard function:
DbExp (Expression, "Database Name", By date (optional))
which returns the value of Expression calculated based on the security specified on the database declaration
and where:
Expression is any numerical expression
Database Name is the name of the previously declared database which is used to calculate the Expression
By date defines if the numeric value is being calculated based on the current period date or not. By date will avoid to obtain wrong values. It could happen that the two data sets are not aligned and a period from the security being elaborated do not match the period of the security from the external data declared in the Database. In practise could occur that the current period does not have a corresponding date/time period on the external security. If "By date" is set to True, the function will try to find a corresponding period on the external security by Date/Time and will not try to match just its position. The default is True.
For Example:
The line:
dMyValue = DbExp((High - Close) * 2 - Rsi(Close, 14), "Microsoft")
assign the value of (High - Close) * 2 - Rsi(Close, 14), calculated using Microsoft data for the date/time period being elaborated, to the dMyValue numeric variable
|