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
QUANTCONNECT COMMUNITY
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
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.
Paul Saunders
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.Jared Broad
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.
Michael Handschuh
Max_leverage_yolo
Vovor
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?
Alexandre Catarino
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);
Vovor
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).
Max_leverage_yolo
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!