mlsignups Posted January 19, 2023 Report Posted January 19, 2023 @Mike Medved Sometime last year you gave me some code to calculate after hrs volume. it helped but never seemed to give me the exact right numbers. the code is here: Looking at it in detail, it seems to be picking up the closing bar - right at 4pm on my system - and treating it as part of after hrs. For instance, Today TSLA had 2M shares trade in the last minute (at 4pm) and this is being picked up in after hrs volume. If I'm correct, is there a way to adjust the code or the session times to not pick up 4pm bar but start at 4:01 or something like that? Quote
Mike Medved Posted January 20, 2023 Report Posted January 20, 2023 Thing is, 4pm candle (anything that is traded at or after 4pm) is considered afterhours. IsAfterHours in TradingDay is defined as Timestamp >= SessionEnd && Timestamp <= DayEnd So instead of IsAfterHours in your code, if you want to give a 1 min buffer you can just say Timestamp >= SessionEnd.SessionEnd.AddMinutes(1) Quote
mlsignups Posted January 20, 2023 Author Report Posted January 20, 2023 cool; thanks I'll try it. Quote
mlsignups Posted January 20, 2023 Author Report Posted January 20, 2023 Sorry, I know this is only because I'm not that experienced at coding, but I had: And when I replace it TradingDay.IsAfterHours with TradingDay.Timestamp >= SessionEnd.SessionEnd.AddMinutes(1) I get this error: 13, 42 CS1061 'System.DateTime' does not contain a definition for 'SessionEnd' and no extension method 'SessionEnd' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?) And if instead I replace TradingDay>isAfterHours with Timestamp >= SessionEnd.SessionEnd.AddMinutes(1) I get this error: 13, 20 CS0103 The name 'SessionEnd' does not exist in the current context Quote
mlsignups Posted January 20, 2023 Author Report Posted January 20, 2023 Just now, mlsignups said: Sorry, I know this is only because I'm not that experienced at coding, but I had: And when I replace it TradingDay.IsAfterHours with TradingDay.Timestamp >= SessionEnd.SessionEnd.AddMinutes(1) I get this error: 13, 42 CS1061 'System.DateTime' does not contain a definition for 'SessionEnd' and no extension method 'SessionEnd' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?) And if instead I replace TradingDay>isAfterHours with Timestamp >= SessionEnd.SessionEnd.AddMinutes(1) I get this error: 13, 20 CS0103 The name 'SessionEnd' does not exist in the current context May have gotten it... using this only one sessionend Quote
Mike Medved Posted January 20, 2023 Report Posted January 20, 2023 Sorry. Of course I mean TradingDay.SessionEnd Timestamp >= TradingDay.SessionEnd.AddMinutes(1) Quote
3acor Posted January 20, 2023 Report Posted January 20, 2023 Could something similar be done with the Pre-market volume? I couldn't find CurrentState.PM Quote
Mike Medved Posted January 20, 2023 Report Posted January 20, 2023 Would be TradingDay.IsPreMarket Quote
3acor Posted January 20, 2023 Report Posted January 20, 2023 oh yes it works now thanks. Had to modify the variable. One last thing, is it possible to compute the average pre-market volume? Quote
Mike Medved Posted January 21, 2023 Report Posted January 21, 2023 Well yes That is what FIFO queues are about - to calculate averages for last N periods. Send me your scan and I will add that. Quote
3acor Posted January 21, 2023 Report Posted January 21, 2023 Great! I don't have a scan. I just want a scan that only does this. To calculate the average pre-market volume over a certain period and return it as a ScanResult. Or if possible the average turnover/dollar volume would be better. Quote
Mike Medved Posted January 21, 2023 Report Posted January 21, 2023 Note that everything *after* MainCalculation is just standard code inserted by MT when you make it a state machine. Also - to make it work right, it has to be run as an Intraday scan, with the extended hours set to Always. ==================================================================================================================== private struct PaintbarState { public Double PreMarketVolume; } int LastDayNumber = -1; FIFOQueue PreMarketVolumes = new FIFOQueue(5); // averaging on 5 days period Boolean WasPreMarket = false; public void MainCalculation() { SetDaysNeeded(5); // need 5 day's data for this to work if (TradingDay.DayNumber != LastDayNumber) // initialize to 0 start of every day { CurrentState.PreMarketVolume = 0; LastDayNumber = TradingDay.DayNumber; } if (TradingDay.IsPreMarket) { CurrentState.PreMarketVolume += BarVolume; WasPreMarket = true; } else { if (WasPreMarket) // add the premarket volume AFTER the premarket ends to the average queue { WasPreMarket = false; PreMarketVolumes.Add(CurrentState.PreMarketVolume); } } SetScanResultColumnName(0, "Avg Premarket"); SetScanResult(0, Convert.ToInt64(PreMarketVolumes.Average)); } /// <summary> /// INITIALIZES paintbar. Is called at start of paintbar calculation, should be used to initialize anything needed for the paintbar /// NOTE: do NOT initialize the CurrentState here - the PaintbarClearState will be called AFTER this call, and will wipe it out. /// </summary> private void PaintbarInitialize() { } /// <summary> /// Holds current PB state - use to calc PB, changes to it carry over to next PB calc /// </summary> private PaintbarState CurrentState; /// <summary> /// Holds saved PB state - internal /// </summary> private PaintbarState SavedState; /// <summary> /// Is called at start of paintbar calculation, should be used to clear the paintbar state /// and initialize whatever variables you place into it. This is called AFTER PaintbarInitialize /// </summary> private void PaintbarClearState() { CurrentState = new PaintbarState(); } /// <summary> /// Saves paintbar state (called internally). /// </summary> private void PaintbarSaveState() { SavedState = CurrentState; } /// <summary> /// Restores paintbar state (called internally). /// </summary> private void PaintbarRestoreState() { CurrentState = SavedState; } 1 Quote
3acor Posted January 22, 2023 Report Posted January 22, 2023 Could it be possible to have the average pre-market volume calculated up until a certain time? like for example "average pre-market volume from the open of premarket up until 9:00 am" Quote
Mike Medved Posted January 22, 2023 Report Posted January 22, 2023 Sure. Change if (TradingDay.IsPreMarket) to if (Timestamp < TradingDay.SessionStart.AddMinutes(-30)) 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.