Bid Tick Bars - Custom

Bid Tick Bars - Custom

#region Namespaces
using System;
#endregion

namespace ScriptCode
{
    /// <summary>
    /// Bar type scripts are used for building custom bar types that can be used interchangeably with native bar types throughout the platform.
    /// 
    /// The custom data bars that are built by the script can either be based on tick, minute or daily bars depending on the underlying bar source.
    /// When a bar type script is used by multiple symbols a single script instance is initialized for each one.
    /// </summary>
    public partial class MyBarType : BarTypeScriptBase // NEVER CHANGE THE CLASS NAME
    {
        #region Variables
		// The bar size as specified by the user.
		private double _barSize;
		// The number of ticks in the currently built data bar.
		private int _barCount;
        #endregion

        #region OnInitialize
        /// <summary>
        /// This function is called when a new bar type instance is created.
        /// </summary>
        /// --------------------------------------------------------------------------------------------------
        /// THIS FUNCTION MUST ACCEPT THE BAR SIZE PARAMETER AND SHOULD NOT ACCEPT ANY OTHER PARAMETERS.
        /// --------------------------------------------------------------------------------------------------
        /// <param name="barSize" type="Double" default="1">The bar size of the new bar type being built.</param>
        public void OnInitialize(double barSize)
        {
            // Set the specified bar size.
            _barSize = barSize;
        }
        #endregion

        #region OnTick
        /// <summary>
        /// This function is called on each new tick of the underlying symbol (see the BarBuilder functions).
        /// </summary>
        /// <param name="tickDateTime" type="DateTime">The tick date/time</param>
        /// <param name="price" type="Double">The tick price</param>
        /// <param name="size" type="Integer">The tick size</param>
        /// <param name="tickType" type="C_TickType">The tick type (BID, ASK, LAST)</param>
        public override void OnTick(
            long tickDateTime,
            double price,
            double size,
            C_TickType tickType)
        {
            // Check whether the tick is a Bid.
			if (tickType == C_TickType.BID) {
				// Merge the tick with the current bar.
				BarBuilderMergeTick(tickDateTime, price, size);
				// Increase the bar count.
				_barCount++;
				// Check whether there are enough ticks in the bar.
				if (_barCount == _barSize) {
					// Clear the bar count.
					_barCount = 0;
					// Commit the bar.
					BarBuilderCommitBar();
				}
			}
        }
        #endregion

        #region OnDataBar
        /// <summary>
        /// This function is called on each new OHLCV+OI bar of the underlying symbol (see the BarBuilder functions).
        /// </summary>
        /// <param name="isMissing" type="Boolean">Indicates whether the data bar is missing (true) or not (false)</param>
        /// <param name="startDateTime" type="DateTime">The data bar start date/time</param>
        /// <param name="endDateTime" type="DateTime">The data bar end date/time</param>
        /// <param name="open" type="Double">The data bar open</param>
        /// <param name="high" type="Double">The data bar high</param>
        /// <param name="low"  type="Double">The data bar low</param>
        /// <param name="close" type="Double">The data bar close</param>
        /// <param name="volume" type="Double">The data bar volume</param>
        /// <param name="openInterest" type="Double">The data bar open interest</param>
        public override void OnDataBar(
            bool isMissing,
            long startDateTime,
            long endDateTime,
            double open,
            double high,
            double low,
            double close,
            double volume,
            double openInterest)
        {
            // OnDataBar Content
        }
        #endregion

        #region OnSessionClose
        /// <summary>
        /// This function is called when a trading session for the underlying symbol is closed.
        /// </summary>
        public override void OnSessionClose()
        {
            // Check whether there is a bar to commit, since ticks were registered.
			if (_barCount > 0) {
				// Clear the bar count.
				_barCount = 0;
				// Commit the current bar.
				BarBuilderCommitBar();
			}
        }
        #endregion
    }
}