4. Create a Sample Program (2)

Bookmark and Share

Let's ponder over the argument to be passed on to iHighest().
There are 5 arguments that are to be passed to iHighest():

  1. iHighest(string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0) //parameters for iHighest ()
  2. iHighest(NULL, PERIOD_H1, MODE_HIGH, BreakPeriod, 1) //arguments used in this sample program
  1. symbol (type string)
    This parameter points to the currency pair to be calculated. By passing the argument NULL, Expert Advisor uses the currency pair chart already attached to it. In most cases, symbol should be set to NULL (exceptions are cases when, for example, data for EURUSD is being used for transactions in =AUDJPY)
  2. timeframe (type int)
    This parameter points to the time frame of a chart (e.g. hour, month, or 5 minute increments) for which the maximum value will be calculated, in minutes. The time frame for the chart is activated with an input of 0. 
    The example above passes the argument PERIOD_H1, which is set to value 60 by default, effectively setting an hourly increment as the time frame.
  3. type (type int)
    This is the type of price used for calculation.
    1. MODE_OPEN initial value(value 0)
    2. MODE_LOW minimum value(value 1)
    3. MODE_HIGH maximum value(value 2)
    4. MODE_CLOSE final value(value 3)
    The type of price is typically chosen from one of the standard constants. 
    The above example compares the current value against maximum value, so the argument passed is MODE_HIGH. 
  4. count (type int)
    This parameter points to the scope of the subjected period in terms of the number of time frames.
    For example, if count is set to 10 in an hourly chart, then the maximum value is calculated for a period of 600 minutes (60 x 10 = 600).
    If the value is not specified (i.e. pass only 3 arguments to the function), then this parameter is set to the entire period stored in the PC memory.
  5. start (type int)
    This parameter points to the end of the entire period, specified by the number of preceding bars.
    Normally this value is set to 0 or 1, and subjects itself to the latest data. In the example above, "start" is set to 1. 
    The value is 0 by default when it is not specified.

Comparing the calculated maximum value and present value

In cases where present value is higher than the calculated maximum value of some time frame, long position must be taken. The following code can execute such an action:

  1. // LONG
  2. double max=High[iHighest(NULL, PERIOD_H1, MODE_HIGH, BreakPeriod, 1)];
  3. if(max <= High[0] && OrdersTotal() < 1){
  4.   // Take a long position
  5. }

In the programming code, "if...then" statements are written as「if(条件式){動作}」.
In the above example, a command to take a new long position is executed when present value is higher than the calculated maximum value, yet have no position at that moment. 

Taking a long position

We will conclude this section with an object that actually takes the long position.

  1. OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Ask-StopLoss*Point, NULL, NULL, 0, 0, Blue);

OrderSend() has been talked about once already, so I will spare the details here.

Summary

Let's sum up the program we have written thus far. 

  1. //----
  2. // LONG
  3.   double max=High[iHighest(NULL, PERIOD_H1, MODE_HIGH, BreakPeriod, 1)];
  4.   if(max <= High[0] && OrdersTotal() < 1){
  5.     OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Ask-StopLoss*Point, NULL, NULL, 0, 0, Blue);
  6.   }
  7. //----

Additional Note: Standard Constant

Standard constant refers to the predetermined constants utilized for often-used items. 

For example, if "timeframe" is to be passed on to a function (e.g. iHighest()), its value is passed in the unit of minutes. 

In other words, in order to set weeks as the period of time frame, the value to be passed will be 7 x 24 x 60 = 10080. Since it is laborious to calculate such a value every time, those that may be used frequently are already set and are available as an argument to be passed on to a function. 

In the case of "timeframe," the following values have been preset.

Name Value Common Name
PERIOD_M1 1 1 Minute Period
PERIOD_M5 5 5 Minute Period
PERIOD_M15 15 15 Minute Period
PERIOD_M30 30 30 Minute Period
PERIOD_H1 60 1 Hour Period
PERIOD_H4 240 4 Hour Period
PERIOD_D1 1440 Daily
PERIOD_W1 10080 Weekly
PERIOD_MN1 43200 Monthly

By using standard constants, instead of writing

  1. double max = High[iHighest(NULL, 60, 2, BreakPeriod, 1)];

one can write as follows:

  1. double max = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, BreakPeriod, 1)];

and achieve the same result.

up