Pivot Points

#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("PVT");
		}
#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 PP levels.
			DrawingRegisterPen("Pivot Point", new int[] {
				22 ,81, 238, 255
			}, C_DashStyle.SOLID, 2);
			// Register the pen with which to draw the resistance levels.
			DrawingRegisterPen("Resistance", new int[] {
				22 ,81, 238, 255
			}, C_DashStyle.SOLID, 2);
			// Register the pen with which to draw the support levels.
			DrawingRegisterPen("Support", 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 X value of anchor point 0. 
			int x1 = DrawingAnchorPointBarShift(0);
			// Get the Y value of anchor point 0. 
			double y1 = DrawingAnchorPointValue(0);

			// Get the X value of anchor point 1. 
			int x2 = DrawingAnchorPointBarShift(1);
			// Get the Y value of anchor point 1. 
			double y2 = DrawingAnchorPointValue(1);
			
			// Get yesterdays high.
			double yesterdaysHigh = SessionHigh(1);
			// Get yesterdays low.
			double yesterdaysLow = SessionLow(1);
			// Get yesterdays close.
			double yesterdaysClose = SessionClose(1);

			// Calculate the pivot point.
			double pp = (yesterdaysHigh + yesterdaysLow + yesterdaysClose) / 3;
			// Resistane level 1
			double R1 = (pp * 2) - yesterdaysLow;
			// Resistance level 2
			double R2 = pp + (yesterdaysHigh - yesterdaysLow);
			// Resistance level 3
			double R3 = 2 * pp + (yesterdaysHigh - 2 * yesterdaysLow);
			// Resistance level 4
			double R4 = 3 * pp + (yesterdaysHigh - 3 * yesterdaysLow);

			// Support level 1
			double S1 = (pp * 2) - yesterdaysHigh;
			// Support level 2
			double S2 = pp - (yesterdaysHigh - yesterdaysLow);
			// Support level 3
			double S3 = 2 * pp - (2 * yesterdaysHigh - yesterdaysLow);
			// Support level 4
			double S4 = 3 * pp - (3 * yesterdaysHigh - yesterdaysLow);
			
			// Adjust the first anchor point to be on the pivot point.
			DrawingSetAnchorPoint(0, x1, pp);
			// Adjust the second anchor point to be on the pivot point.
			DrawingSetAnchorPoint(1, x2, pp);

			// Draw the Pivot Point line.
			DrawingSetLine("Pivot Point", x1, pp, "PP", x2, pp, "PP");
			// Draw the R1 line.
			DrawingSetLine("Resistance", x1, R1, "R1", x2, R1, "R1");
			// Draw the R2 line.
			DrawingSetLine("Resistance", x1, R2, "R2", x2, R2, "R2");
			// Draw the R3 line.
			DrawingSetLine("Resistance", x1, R3, "R3", x2, R3, "R3");
			// Draw the R4 line.
			DrawingSetLine("Resistance", x1, R4, "R4", x2, R4, "R4");
			// Draw the S1 line.
			DrawingSetLine("Support", x1, S1, "S1", x2, S1, "S1");
			// Draw the S2 line.
			DrawingSetLine("Support", x1, S2, "S2", x2, S2, "S2");
			// Draw the S3 line.
			DrawingSetLine("Support", x1, S3, "S3", x2, S3, "S3");
			// Draw the S4 line.
			DrawingSetLine("Support", x1, S4, "S4", x2, S4, "S4");
		}
#endregion
	}
}