In the previous post, we introduced some QuantShare functions that deal with dates and use the date vector. We also presented and created a very simple QuantShare custom function to mimic the "Month" function. (The "Month" function returns the current bar's month of the year). Since you are now familiar with the "Custom Functions" plug-in, we will present you an advanced function that again deals with dates. This function could be used in the Rule Analyzer to generate multiple period samples so that you can backtest trading rules for different periods. You can then display a nice chart that shows you how each trading rule performed for each sample. Samples could be years like 2005, 2006, 2007... The function, let us call it "insideperiod", we are about to create will calculate internally two dates: a start date and an end date. It accepts four parameters; the next paragraph will give you a brief description of each parameter. - The first parameter accepts a month. - The second parameter accepts a year. The two parameters will be used to create an origin date with "1" as the day and with the specified month and year. - The third parameter accepts a number (offset in days). This parameter will be used to create the start date, which is the sum of the origin date and the number of days specified in this parameter. - The forth parameter accepts a number (length in days). This parameter is used to create the end date. The end date is the sum of the start date and the number of days specified in this forth parameter. Here is a description of this function's code: We start by setting the internal variables. var _month : int = int(month[0]); var _year: int = int(year[0]); var _offset: int = int(offset[0]); var _length: int = int(length[0]); Creating the start date: var startdate : DateTime = new DateTime(_year, _month, 1); startdate = startdate.AddDays(_offset); // Offset date by the number of days specified in the "offset" parameter. Creating the end date: It is simply the sum of the start date and the number of days specified in the "length" parameter. var enddate : DateTime = startdate.AddDays(_length); Finally, the function loops through all the security bars and checks whether the bar's date is inside the period bounded by the start and end dates. In the former case it returns "1", and in the latter case it returns "0". for(var i: int = 0;i<result.Length;i++) { if(cFunctions.Date[i] >= startdate && cFunctions.Date[i] < enddate) { result[i] = 1; } else { result[i] = 0; } } cFunctions.SetForwardAndBackwardBars(0, 0); // This function doesn't use any backward or forward bars You can download the "insideperiod" function at the following location: (Inside Period).
|