Click here to Login

How to use date components in your trading rules

Updated on 2009-09-15

Buy on Monday, sell on Wednesday, exit your positions on September or October; these are simple rules that can be easily implemented in any of your trading systems. The common point of these rules is that they all use the date vector in their internal calculation. The date vector is an array of dates where each date corresponds to a barís start date.

Buy on Monday and sell on Wednesday rule uses the QuantShare function (DayOfWeek), which returns 1 if the current bar starts in Monday, 2 for Tuesday, 3 for Wednesday...
This simple trading system could be translated into the following two rules:
buy = DayOfWeek() == 1;
sell = DayOfWeek() == 3;

If you want to include a trading rule that uses the month value in its calculation then the "Month" function is what you need. For the example that says exit your positions on September or October, the QuantShare formula is:
sell = (Month() == 9 || Month() == 10);

Here is a list of other QuantShare date-related functions:
Date: Returns the date in a String format.
DateTicks: Returns the number of ticks that represents the current bar date.
DayOfYear: Returns the current bar's day of the year, Example: On 11-01-2007, this function returns a value of 11.
Week: Returns the current bar's week of the year
Day: Returns the current bar's day of the month
Month: Returns the current bar's month
Year: Returns the current bar's year

There are also some other date-related functions that we will not discuss here and which deal with hours, minutes and seconds.

If you need more complex and advanced date-related functions, you can always create a custom function and use the JScript.Net language to implement it. As an example, we will create a very simple function that returns the current bar's month value and thus will give the same results as the QuantShare "Month" function.
First, open the "Custom Functions" form and create a new function, call it for example "Month1". We have to iterate through all the values of the date vector.
The date vector, which is of "VectorDate" type, is retrieved using the following function: "cFunctions.Date". It internally contains an array of Date Time values.
To get the date of the bar number 1200; we use the following function: cFunctions.Date.GetValue(1200);
Let us go back to our iteration; for each bar's date, we need to get the date's month value. The .Net property of a DateTime variable (Month) is used for that purpose.
You do not have to guess what methods, properties or fields are available for a given variable; all you need to do is type CTRL+SPACE (Control + Space) and QuantShare will display all the available methods, fields and properties.

The final script uses only three lines of code:

for(var i:int = 0;i<result.Length;i++)
      result.SetValue(i, cFunctions.Date.GetValue(i).Month);

no comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1288 days ago

Profile Graphs
Posted 1393 days ago

Previous Posts

Quantshare version 1.4
Posted 5391 days ago

Looking for trading ideas
Posted 5397 days ago

A vector-based language
Posted 5400 days ago

Data snooping bias
Posted 5413 days ago

Limit and Market orders
Posted 5424 days ago

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright © 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
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.