Overnight Volume

#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion 

namespace ScriptCode {
	/// <summary> 
	/// Indicator scripts are used for calculating a series of numerical values based on the price, volume and open interest of an underlying symbol. 
	/// Common use-cases include plotting them on a chart, displaying them as watchlist columns and using them to implement other scripts.
	/// </summary>
	public partial class MyIndicator : IndicatorScriptBase // NEVER CHANGE THE CLASS NAME
	{
#region Variables
		// The volume traded in the overnight window thus far.
		private double _overnightVolume;
		// The hour value of the overnight start time.
		private int _overnightStartTimeHour;
		// The minute value of the overnight start time.
		private int _overnightStartTimeMinute;
		// The hour value of the trading start time.
		private int _tradingStartTimeHour;
		// The minute value of the trading start time.
		private int _tradingStartTimeMinute;
		// The user-provided overnight start time in 24-hr hhmm notation.
		private long _overnightStartTimeInput;
		// The user-provided trading start time in 24-hr hhmm notation.
		private long _tradingStartTimeInput;
		// The number of days to add or subtract from the overnight start time.
		private int _overnightDayOffset;
		// The number of days to add or subtract from the trading start time.
		private int _tradingDayOffset;
#endregion

#region OnInitialize
		/// <summary>
		/// This function accepts the user parameters for the script and is called when a new indicator instance is created. 
		/// One of the parameters accepted by it must be that of a symbol or another script that is
		/// based on a symbol (drawing, indicator, pattern or signal). This symbol will be used as the underlying symbol for the indicator.
		/// 
		/// The parameter values can be specified from the user interface (UI) or from another script, depending on usage.
		/// </summary>
		/// --------------------------------------------------------------------------------------------------
		/// PLEASE USE THE SCRIPT WIZARD (CTRL+W) TO ADD, EDIT AND REMOVE THE SCRIPT PARAMETERS
		/// --------------------------------------------------------------------------------------------------
		/// YOU MUST SET A PARAM TAG FOR EACH PARAMETER ACCEPTED BY THIS FUNCTION.
		/// ALL PARAM TAGS SHOULD BE SET IN THE 'OnInitialize' REGION, RIGHT ABOVE THE 'OnInitialize' FUNCTION.
		/// THE ORDER OF THE TAGS MUST MATCH THE ORDER OF THE ACTUAL PARAMETERS.

		/// REQUIRED ATTRIBUTES:
		/// (1) name: The exact parameter name.
		/// (2) type: The type of data to collect from the user: 
		/// Set to "Integer" when the data type is 'int'
		/// Set to "IntegerArray" when the data type is 'int[]'
		/// Set to "DateTime" when the data type is 'long'  
		/// Set to "DateTimeArray" when the data type is 'long[]'  
		/// Set to "Boolean" when the data type is 'bool'
		/// Set to "BooleanArray" when the data type is 'bool[]'
		/// Set to "Double" when the data type is 'double'
		/// Set to "DoubleArray" when the data type is 'double[]'
		/// Set to "String" when the data type is 'string'
		/// Set to "StringArray" when the data type is 'string[]'
		/// Set to "Indicator" when the data type is 'Indicator'
		/// Set to "Pattern" when the data type is 'Pattern'
		/// Set to "Signal" when the data type is 'Signal'
		/// Set to "Drawing" when the data type is 'Drawing'
		/// Set to "Symbol" when the data type is 'int' representing a symbol index.

		/// OPTIONAL ATTRIBUTES:
		/// (3) default: The default parameter value is only valid when the type is Integer, Boolean, Double, String or an API Type. 
		/// (4) min: The minimum parameter value is only valid when the type is Integer or Double.
		/// (5) max: The maximum parameter value is only valid when the type is Integer or Double.

		/// EXAMPLE: <param name="" type="" default="" min="" max="">Enter the parameter description here.</param> 
		/// --------------------------------------------------------------------------------------------------
		/// <param name="symbolIndex" type="Symbol">The symbol on which to calculate the indicator, 
		/// it can be replaced with a script that is based on a symbol (drawing, indicator, pattern or signal).</param>
		/// <param name="overnightStartTime" type="Integer" default="0" min="0" max="2400"></param>
		/// <param name="tradingStartTime" type="Integer" default="830" min="0" max="2400"></param>
		public void OnInitialize(int symbolIndex,
			int overnightStartTime,
			int tradingStartTime) {
			// Set the script parameters to script variables.
			_overnightStartTimeInput = overnightStartTime;
			_tradingStartTimeInput = tradingStartTime;
			// Set the time zone to the time zone of the underlying symbol.
			DateTimeSetTimeZone(SymbolTimeZone());
			// Calculate the hour value of the overnight start time.
			_overnightStartTimeHour = overnightStartTime / 100;
			// Calculate the minute value of the overnight start time.
			_overnightStartTimeMinute = overnightStartTime % 100;
			// Calculate the hour value of the trading start time.
			_tradingStartTimeHour = tradingStartTime / 100;
			// Calculate the minute value of the trading start time.
			_tradingStartTimeMinute = tradingStartTime % 100;
		}
#endregion

#region OnBarUpdate
		/// <summary>
		/// This function is used for calculating the indicator value for the latest bar (see the Indicator functions).
		/// </summary>
		/// <returns type="Double">The indicator value for the latest bar.</returns>
		public override double OnBarUpdate() {
			// Check whether it is necessary to manage the day offset variables.
			if(_overnightStartTimeInput > _tradingStartTimeInput){
				// Check whether the day of the current bar has changed.
				if(DateTimeDay(DataEndDateTime(1)) != DateTimeDay(DateTimeCurrent())){
					// Subtract one day to the overnight start time.
					_overnightDayOffset = -1;
					// Don't apply an offet to the trading start time.
					_tradingDayOffset = 0;
				} else {
					// Record what would be the datetime of the new overnight window.
					long newOvernightStartTime = DateTimeCreate(DateTimeYear(DateTimeCurrent()), DateTimeMonth(DateTimeCurrent()), DateTimeDay(DateTimeCurrent()), _overnightStartTimeHour, _overnightStartTimeMinute, 0);
					// Check whether a new overnight window has started.
					if(DataEndDateTime(1) <= newOvernightStartTime && newOvernightStartTime < DateTimeCurrent()){
						// Don't apply an offet to the overnight start time.
						_overnightDayOffset = 0;
						// Add one day to the trading day start time.
						_tradingDayOffset = 1;
					}
				}
			}
			// Create a variable to hold the start of the current day's overnight session.
			long overnightStartTime = DateTimeAddDays(DateTimeCreate(DateTimeYear(DateTimeCurrent()), DateTimeMonth(DateTimeCurrent()), DateTimeDay(DateTimeCurrent()), _overnightStartTimeHour, _overnightStartTimeMinute, 0), _overnightDayOffset);
			// Create a variable to hold the start of the current day's trading session.
			long tradingStartTime = DateTimeAddDays(DateTimeCreate(DateTimeYear(DateTimeCurrent()), DateTimeMonth(DateTimeCurrent()), DateTimeDay(DateTimeCurrent()), _tradingStartTimeHour, _tradingStartTimeMinute, 0), _tradingDayOffset);
			// Check whether it is currently the overnight period and the bar is complete.
			if(overnightStartTime < DateTimeCurrent() && DateTimeCurrent() <= tradingStartTime && DataIsComplete()){
				// Check whether it is time to reset the overnight volume.
				if(DataEndDateTime(1) <= overnightStartTime){
					// Reset the overnight volume.
					_overnightVolume = 0;
				}
				// Add volume of current bar to the cumulative sum of the volume of the overnight period thus far.
				_overnightVolume += DataVolume();
			}
			// Return the overnight volume.
			return _overnightVolume;
		}
#endregion

#region OnChartSetup
		/// <summary>
		/// This function is used for setting up the indicator on the chart and registering its pens (see the IndicatorChartRegisterPen function).
		/// </summary>
		public override void OnChartSetup() {
			// Register a pen.
			IndicatorChartRegisterPenRGB(0, "Pen", new int[] { 22 ,81, 238, 255 }, C_DashStyle.SOLID, 2);
			// Set the indicator in a new panel.
			IndicatorChartSetNewPanel(true);
		}
#endregion

#region OnSelectPen
		/// <summary>
		/// This function is used for selecting a registered indicator pen with which to color the latest indicator value.
		/// Call the IndicatorChartRegisterPen function from the OnChartSetup function in order to register an indicator pen.
		/// </summary>
		/// <returns type="Byte">The indicator pen index to use for coloring the latest indicator value.</returns>
		public override byte OnSelectPen() {
			return 0;
		}
#endregion
	}
}