Ruler

#region Namespaces
using System;
#endregion 

namespace ScriptCode {
	/// <summary>
	/// Drawing scripts are used for drawing a visual object.
	/// 
	/// This script can be used in several ways:
	/// (1) It can be used on a chart by clicking the chart in order to set the drawing's anchor points.
	/// (2) It can be used from another script by having that script set the drawing's anchor points.
	/// </summary>
	public partial class MyDrawing : DrawingScriptBase // NEVER CHANGE THE CLASS NAME 
	{
#region Variables

#endregion

#region OnInitialize
        /// <summary>
        /// This function is called when a new drawing instance is created.
        /// </summary>
        /// --------------------------------------------------------------------------------------------------
        /// THIS FUNCTION MUST ACCEPT THE SYMBOL INDEX PARAMETER AND SHOULD NOT ACCEPT ANY OTHER PARAMETERS.
        /// --------------------------------------------------------------------------------------------------
		/// <param name="symbolIndex" type="Symbol" default="">The underlying symbol index for the drawing.</param>
		public void OnInitialize(int symbolIndex) {
			// Initialize the drawing for the symbol to accept 2 anchor points. 		 
			DrawingInitialize(symbolIndex, 2);
			// Set the icon for the drawing tool.
			DrawingSetIcon("Ruler");
		}
#endregion

#region OnChartSetup
		/// <summary>
		/// This function is used for setting up the drawing on the chart and registering its pens (see the DrawingRegisterPen function).
		/// </summary>
		public override void OnChartSetup() {
			// Register the pen with which to draw the ruler.
			DrawingRegisterPen("Ruler Pen", new int[] {
				22 ,81, 238, 255
			}, C_DashStyle.SOLID, 2);
		}
#endregion

#region OnDraw
		/// <summary>
		/// This function is used to draw the drawing by setting all of its anchor points, lines and labels on a virtual canvas. 
		/// The canvas x-axis values are the date and time of the underlying symbol bars and its y-axis values are the symbol prices. 
		///
		/// The location of the drawing on the canvas is specified by the drawing anchor points, which can either be specified by 
		/// clicking on a chart or from another script (see the Drawing functions).
		/// </summary>
		public override void OnDraw() {
			// Get the number of decimals for the symbol.
			int symbolDecimals = SymbolDecimals();
			// Get the X value of anchor point 0. 
			int x1 = DrawingAnchorPointBarShift(0);
			// Get the Y value of anchor point 0. 
			double y1 = Math.Round(DrawingAnchorPointValue(0), symbolDecimals);
			// Get the X value of anchor point 1. 
			int x2 = DrawingAnchorPointBarShift(1);
			// Get the Y value of anchor point 1. 
			double y2 = Math.Round(DrawingAnchorPointValue(1), symbolDecimals);
			// Calculate the tick difference.
			double ticks = Math.Round(Math.Round(Math.Abs(y2 - y1), symbolDecimals) / SymbolTickSize(), 0);
			// Calculate the change difference.
			double CD = 100 * Math.Abs(y2 - y1) / Math.Min(y1, y2);
			// Calculate the time difference.
			string TD = TimeSpan.FromTicks(Math.Abs(DataStartDateTime(x1) - DataStartDateTime(x2))).ToString();
			// Set the entry string.
			string entry = "Start: " + y1;
			// Set the exit string.
			string exit = "End: " + y2 + " | Ticks: " + ticks + " | Change: " + Math.Round(CD, 2) + "%" + "\nTime: " + TD + " | Bars: " + Math.Abs(x2 - x1);
			// Draw the trendline.
			DrawingSetLine("Ruler Pen", x1, y1, entry, x2, y2, exit);
		}
#endregion
	}
}