- ago
I'd like to test whether there it is possible to code in a first hour (or any other timeframe) of the week skip. It seems like the volatility is great and sometimes doesn't truly indicate the market direction.

I know the 1% per week model has code to determine the first day of the week but it's not clear to me how to translate that into the first hour.
1
268
8 Replies

Reply

Bookmark

Sort
- ago
#1
On how to deal with the opening range of first N minutes, check out the built in C# Strategy "Opening range breakout" (Sample Strategies folder).
0
- ago
#2
Github Copilot Chat is your friend. Here is an efficient way to do it without using constant backward scans, and this should work with any intraday scale. To use this you need to call UpdateFirstTradingOfWeek(BarHistory bars, int idx) first thing from Execute(). Then you can use IsFirstHourOfFirstDayOfWeek(BarHistory bars, int idx) from your trade logic. I didn't actually try this code, but it looks right. Your mileage may vary...
CODE:
private bool? _isFirstTradingDayOfWeek; private DateTime _lastCheckedDate; private void UpdateFirstTradingDayOfWeek(BarHistory bars, int idx) { var currentDate = bars.DateTimes[idx].Date; // Check if the date has changed if (currentDate != _lastCheckedDate) { _lastCheckedDate = currentDate; _isFirstTradingDayOfWeek = null; // Iterate backward to find the most recent trading day for (var i = idx - 1; i >= 0; i--) { var previousDate = bars.DateTimes[i].Date; var previousDayOfWeek = previousDate.DayOfWeek; // If the previous trading day is in the previous week, then the current day is the first trading day of the week if (previousDate < currentDate && previousDayOfWeek > currentDate.DayOfWeek) { _isFirstTradingDayOfWeek = true; return; } // If the previous trading day is in the same week, then the current day is not the first trading day of the week if (previousDate < currentDate && previousDayOfWeek < currentDate.DayOfWeek) { _isFirstTradingDayOfWeek = false; return; } } // If no previous trading day is found, assume the current day is the first trading day of the week _isFirstTradingDayOfWeek = true; } } private bool IsFirstTradingDayOfWeek(BarHistory bars, int idx) { UpdateFirstTradingDayOfWeek(bars, idx); return _isFirstTradingDayOfWeek ?? false; } // write a method that determines if the current bar is in the first hour of trading of the first day of the week public bool IsFirstHourOfFirstDayOfWeek(BarHistory bars, int idx) { if (!IsFirstTradingDayOfWeek(bars, idx)) { return false; } return bars.DateTimes[idx].GetTime() >= 930 && bars.DateTimes[idx].GetTime() <= 1030; }
1
- ago
#3
Ah, bars.IntradayBarNumber(idx) - didn't know that one was there! Hence, you should checkout the Opening Range Breakout strategy.
0
- ago
#4
Then, to detect the first trading day of week in any bar scale:

CODE:
public override void Execute(BarHistory bars, int idx) {          if (idx > 0)          {             bool _firstDayOfWeek = bars.TradingDaysRemaining(idx, Frequency.Weekly) > 0 &&                bars.TradingDaysRemaining(idx - 1, Frequency.Weekly) == 0;                          if(_firstDayOfWeek)                WriteToDebugLog(idx);                      }          ...
0
- ago
#5
@Eugene, I don't want to nit-pick, but I thought you should know, in case you want to document TradingDaysRemaining, its not in online help or WL8 help. (Of course, it shows in Intellisense.)
0
- ago
#6
Thanks, Paul and Eugene. I'll try these this evening.
0
Cone8
 ( 6.71% )
- ago
#7
Re: TradingDaysRemaining
I can get that. Thanks.
0
- ago
#8
Here is some code that is much simpler than what GitHub Copilot produced in Post #2. I tested this for one year in the U.S. Market and it seemed to work fine. It uses the Market info of the bars which takes into account the market trading days and holidays...

CODE:
public static bool IsFirstHourOfFirstDayOfWeek(BarHistory bars, int idx) { // we know if it's not the first hour of any day then it certainly isn't // the first hour of the first day of the week if (bars.DateTimes[idx].GetTime() < 931 || bars.DateTimes[idx].GetTime() >= 1030) { return false; } var day = bars.DateTimes[idx].Date; var currentWeek = day.WeekOfYear(); // we are guaranteed to exit this loop when we move to a prior week while (true) { // go back one actual day day = day.AddDays(-1); // we crossed into the prior week if (day.WeekOfYear() != currentWeek) { return true; } // we are in the same week, but 'day' is a trading day if (bars.Market.IsTradingDay(day)) { // some prior day in the same week is a trading day return false; } } }
0

Reply

Bookmark

Sort