Click here to Login




The 'inside period' function

Updated on 2009-09-23





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).











no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1479 days ago

Profile Graphs
Posted 1584 days ago

QuantShare
Previous Posts

The 'inside period' function
Posted 5568 days ago

Quantshare version 1.4
Posted 5582 days ago

Looking for trading ideas
Posted 5588 days ago

A vector-based language
Posted 5591 days ago

Data snooping bias
Posted 5604 days ago

Limit and Market orders
Posted 5615 days ago


More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.