Andrews Pitchfork

#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 on the specified symbol to accept 3 anchor points.
			DrawingInitialize(symbolIndex, 3);
			// Set the icon for the drawing tool.
			DrawingSetIcon("Andrews Pitchfork");
		}
		#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 drawing.
			DrawingRegisterPen("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 X position of anchor point 0. 
			int X0 = DrawingAnchorPointBarShift(0);
			// Get the Y position of anchor point 0. 
			double Y0 = DrawingAnchorPointValue(0);

			// Get the X position of anchor point 1. 
			int X1 = DrawingAnchorPointBarShift(1);
			// Get the Y position of anchor point 1. 
			double Y1 = DrawingAnchorPointValue(1);

			// Get the X position of anchor point 2. 
			int X2 = DrawingAnchorPointBarShift(2);
			// Get the Y position of anchor point 2. 
			double Y2 = DrawingAnchorPointValue(2);

			int upperX = 0, lowerX = 0, centerX = 0;
			double upperY = 0, lowerY = 0, centerY = 0;

			// Check whether point 0 should be used as the center, as its bar index is the farthest from the right hand side.
			if (X0 >= X1 && X0 >= X2) {
				// Set the center point X value to be that of point X0.
				centerX = X0;
				// Set the center point Y value to be that of point Y0.
				centerY = Y0;
				// Check whether point 1 should be used as the upper line.
				if (Y1 > Y2) {
					upperX = X1;
					upperY = Y1;
					lowerX = X2;
					lowerY = Y2;
				}
				else {
					upperX = X2;
					upperY = Y2;
					lowerX = X1;
					lowerY = Y1;
				}
			}
			// Check whether point 1 should be used as the center, as its bar index is the farthest from the right hand side.
			if (X1 >= X0 && X1 >= X2) {
				// Set the center point X value to be that of point X1.
				centerX = X1;
				// Set the center point Y value to be that of point Y1.
				centerY = Y1;
				// Check whether point 0 should be used as the upper line.
				if (Y0 > Y2) {
					upperX = X0;
					upperY = Y0;
					lowerX = X2;
					lowerY = Y2;
				}
				else {
					upperX = X2;
					upperY = Y2;
					lowerX = X0;
					lowerY = Y0;
				}
			}
			// Check whether point 2 should be used as the center, as it's bar index is the farthest from the right hand side.
			if (X2 >= X0 && X2 >= X1) {
				// Set the center point X value to be that of point X2.
				centerX = X2;
				// Set the center point Y value to be that of point Y2.
				centerY = Y2;
				// Check whether point 0 should be used as the upper line.
				if (Y0 > Y1) {
					upperX = X0;
					upperY = Y0;
					lowerX = X1;
					lowerY = Y1;
				}
				else {
					upperX = X1;
					upperY = Y1;
					lowerX = X0;
					lowerY = Y0;
				}
			}

			// Calculate the center of the pitchfork.
			double centerX2 = (upperX + lowerX) / 2;
			double centerY2 = (upperY + lowerY) / 2;

			// The slope.
			double m = 0;
			// Check whether a slope can be calculated.
			if (centerX - centerX2 != 0)
			// Calculate the slope of all three lines.
				m = (centerY - centerY2) / (centerX - centerX2);

			// Check whether all three anchor points are set differently. 
			// Initially all anchor points get the values of the first one.
			if (X2 != X0 || Y2 != Y0) {
				// Draw the upper line.
				DrawingSetRay("Pen", upperX, upperY, upperX - 1, upperY - m);
				// Draw the center line.
				DrawingSetRay("Pen", centerX, centerY, centerX - 1, centerY - m);
				// Draw the lower line.
				DrawingSetRay("Pen", lowerX, lowerY, lowerX - 1, lowerY - m);
			}
			else {
				// Draw a temporary line between the first two points.
				DrawingSetLine("Pen", X0, Y0, "", X1, Y1, "");
			}
		}
		#endregion
	}
}