Back

Trading days vs days

Whats the best way to get trading days in C# so I can schedule a function to trade once a month?

At the moment I have a days counter but it veers off-course:P
Update Backtest








Use the "Time" property to determine the month, day and year.

For instance if you wanted to trade only on the first of the month, check that Time.Day == 1;

You may need to keep another variable so that you can detect when the day has changed if you have anything other than a daily strategy using daily bars.
0

You might want to use the Scheduled Events API! This allows you to do things at regular intervals. The docs are slowly getting up to date :) but you can see some good examples here: Scheduled Events
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Here's an example of firing an event on the first trading fay of the month at 10am for SPY
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


perfect

thanks guys!
0

Hi,

How would you trigger events based on trading days (not calendar days)?

For example, sending an order 17 trading days before the end of the month, or on the 11th trading day of the month?

0

Since trading days (business days) are not in regular intervals, you will need to write some logics to compare the current day against a given date. 
In order to find which days are trading days, you can use TradingCalendar object:

private List<DateTime> GetBusinessDays()
{
var start = Time.AddDays(1 - Time.Day);
var end = Time.AddMonths(1).AddDays(-1 - Time.Day);
return TradingCalendar.GetTradingDays(start, end)
.Where(x => x.BusinessDay)
.Select(x => x.Date)
.ToList();
}

Assign this method to a class field in a Schedule Event:

private List<DateTime> _tradingDays;

// In Initialize
Schedule.Event().MonthStart("SPY").At(TimeSpan.FromHours(10)).Run(() =>
{
_tradingDays = GetBusinessDays();
});

And, finally, you can access the 17th trading day like this:

// In OnData
var day17 = _tradingDays.ElementAt(17);

 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Spaseeba Alexandre! Will try when I get a chance.

A suggestion if I may: add your TradingCalendar example to the Documentation page since this is a pretty common use case yet hard to find.

The examples in the QC University are great but sometimes you just don't know where to look based on the example algo's name.

For example, not everybody will think about going to QC University => "How Do I Create a Rolling Window of Data?" when trying to access previous data/indicator values. Most established backtesting platform, although less flexible than QC, use a different approach to let users access data history (e.g. Close[1] is the close of the previous bar).

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed