#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 4 anchor points. DrawingInitialize(symbolIndex, 4); } #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 solid lines DrawingRegisterPen("Solid Pen", new byte[] {5, 110, 180, 255}, AT_DashStyle.SOLID, 2); // Register the pen with which to draw dashed lines DrawingRegisterPen("Dashed Pen", new byte[] {5, 110, 180, 255}, AT_DashStyle.DASH, 2); // Register a blank pen with which to outline the triangles needed in the drawing DrawingRegisterPen("Blank Pen", new byte[] {5, 110, 180, 75}, AT_DashStyle.SOLID, 0); // Register the pen with which to fill the triangles needed in the drawing DrawingRegisterPen("Fill Pen", new byte[] {5, 110, 180, 75}, AT_DashStyle.SOLID, 1); // Register the pen with which to draw text boxes and text DrawingRegisterPen("Text Pen", AT_Color.WHITE, AT_DashStyle.SOLID, 1); // Register the pen with which to fill text boxes DrawingRegisterPen("Text Fill Pen", AT_Color.BLACK, AT_DashStyle.SOLID, 1); } #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() { // Used for the difference in bar shift between given lines that need to be determined at runtime int tempX; // Used for the difference in y value between given lines that need to be determined at runtime double tempY; // Get X value of anchor point 0 int x0 = DrawingAnchorPointBarShift(0); // Get the Y value of anchor point 0 double y0 = DrawingAnchorPointValue(0); // Get X value of anchor point 1 int x1 = DrawingAnchorPointBarShift(1); // Get the Y value of anchor point 1 double y1 = DrawingAnchorPointValue(1); // Get X value of anchor point 2 int x2 = DrawingAnchorPointBarShift(2); // Get the Y value of anchor point 2 double y2 = DrawingAnchorPointValue(2); // Get X value of anchor point 3 int x3 = DrawingAnchorPointBarShift(3); // Get the Y value of anchor point 3 double y3 = DrawingAnchorPointValue(3); // Draws the solid line from A to B and it's respective text info on the line if(x0 != x1){ DrawingSetLine("Solid Pen", x0, y0, "A\n" + MathRound(y0, 2), x1, y1, ""); // Detemines the temporary x and y of where to place text on the line just drawn // These specific calcuations are done to place the text boxes on the chart as cleanly as possible tempX = (int)MathAbs((x0 + x0 + x1) / 3); tempY = (MathAbs((y0 + y0 + y1) / 3)); // Writes the text on the drawing DrawingSetText("" + MathRound(MathAbs(y0 - y1), 2) + "\nAB: " + MathAbs(x0 - x1), tempX, tempY, "Text Pen", "Text Fill Pen"); } // Draws the solid line from B to C and it's respective text info on the line if(x0 != x2){ DrawingSetLine("Solid Pen", x1, y1, "B\n" + MathRound(y1, 2), x2, y2, ""); // Detemines the temporary x and y of where to place text on the line just drawn // These specific calcuations are done to place the text boxes on the chart as cleanly as possible tempX = (int)((x1 + x1 + x2) / 3); tempY = ((y1 + y2 + y2) / 3); // Writes the text on the drawing for BC line DrawingSetText("" + MathRound(MathAbs(y1 - y2), 2) + "\nBC: " + MathAbs(x1 - x2), tempX, tempY, "Text Pen", "Text Fill Pen"); } // Using the D point as an anchor to determine distance, this draws three retracement lines, // 1.0AB, 1.27AB, and 1.618AB if(x0 != x3){ // Puts the anchor point for D down but does not draw the actual line DrawingSetLine("Blank Pen", x2, y2, "C\n" + MathRound(y2, 2), x3, y3, "D\nCD: " + MathAbs(x2 - x3)); // Finds y for the 1.27AB line tempY = (y2 - MathAbs(y0 - y1)); // Draw the 1.0AB retracement line DrawingSetLine("Dashed Pen", x2, y2, "", x3, tempY, "1.0AB , Y=" + MathRound(tempY, 2)); // Finds y for the 1.27AB line tempY = (y2 - (1.27)*MathAbs(y0 - y1)); // Draw the 1.27AB retracement line DrawingSetLine("Dashed Pen", x2, y2, "", x3, tempY, "1.27AB , Y=" + MathRound(tempY, 2)); // Finds y for the 1.618AB line tempY = (y2 - (1.618)*MathAbs(y0 - y1)); // Draw the 1.618AB retracement line DrawingSetLine("Dashed Pen", x2, y2, "", x3, tempY, "1.618AB , Y=" + MathRound(tempY, 2)); // Draws the necessary shaded triangle using the retracements of point DrawingSetPolygon("Blank Pen", "Fill Pen", new int[] {x2, x3, x3}, new double[] {y2, (y2 - MathAbs(y0 - y1)), tempY}, new string[] {"", "", "", ""}); } } #endregion } }

The Algorithmic Trading Software for Hedge Funds and Quants