I've been working on some code that essentially attempts to measure the maximum trading range over the past X sessions. I know there are tools like Range(); in this case I'm using tick charts and I'm only running one data series (i.e. no multiple time frames to track days or other period types).
I've been trying to use Pivots() as a template for getting PriorDayOHLC but I'm having problem understanding how the data is loaded asynchronously when using different timeframes.
My code currently looks like this (the session calculation part). I use a custom list<double> This all runs in OnBarUpdate() for an unmanaged strategy.
There HAS to be a much simpler way of calculating prior X day max ranges for a data period that is different from the input period. I was hoping you guys could help me with that:
// First, determine if this is the first bar of the session (because we don't know what bar we'll be given first). if (BarsArray[BarsInProgress].FirstBarOfSession) { // Update session information currentSessionData.Add(new CustomSession()); currentSessionData[sessionCount].Open = Open[0]; currentSessionData[sessionCount].High = High[0]; currentSessionData[sessionCount].Low = Low[0]; currentSessionData[sessionCount].SessionNum = sessionCount; }
if (currentSessionData.Count > 0) { // Update the high of the current session if (High[0] > currentSessionData[sessionCount].High) currentSessionData[sessionCount].High = High[0]; // Update the low of the current session if (Low[0] < currentSessionData[sessionCount].Low) currentSessionData[sessionCount].Low = Low[0]; // Update current session change currentSessionData[sessionCount].Change = Math.Abs((currentSessionData[sessionCount].Open - Close[0]) / currentSessionData[sessionCount].Open); // Record the full range of the session currentSessionData[sessionCount].MaxChange = Math.Max(Math.Abs((currentSessionData[sessionCount].Open - currentSessionData[sessionCount].High) / currentSessionData[sessionCount].Open), Math.Abs((currentSessionData[sessionCount].Open - currentSessionData[sessionCount].Low) / currentSessionData[sessionCount].Open)); // Calculate the max % change over the trailing session period if (currentSessionData.Count > hiLoSessionChangeLookbackPeriod) for (int i = 1; i <= hiLoSessionChangeLookbackPeriod; i ++) if (currentSessionData[sessionCount].MaxChange > currentSessionData[sessionCount - i].MaxChange) trailingSessionMaxRange = currentSessionData[sessionCount].MaxChange; }
if (BarsArray[BarsInProgress].LastBarOfSession && currentSessionData.Count > 0) { // Add the CLOSE to the current session data. currentSessionData[sessionCount].Close = Close[0]; // Set final % change over the session currentSessionData[sessionCount].Change = (currentSessionData[sessionCount].Open - currentSessionData[sessionCount].Close) / currentSessionData[sessionCount].Open; // Increment the session count sessionCount ++; }
As I mentioned earlier, I'd like to be able to use the Pivots() code for this; my code currently doesn't work on Day type charts or greater due to what's obviously bad code ("FirstBarOfSession" doesn't seem to happen in day/week/month type periods).
Thoughts?
Comment