#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="">Use for the underlying symbol index on which to draw the drawing script.</param> public void OnInitialize(int symbolIndex) { // Initialize the drawing for the symbol to accept 5 anchor points. DrawingInitialize(symbolIndex, 5); } #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}, IQ_DashStyle.SOLID, 2); // Register the pen with which to draw dashed lines DrawingRegisterPen("Dashed Pen", new byte[] {5, 110, 180, 255}, IQ_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}, IQ_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}, IQ_DashStyle.SOLID, 1); // Register the pen with which to draw text boxes and text DrawingRegisterPen("Text Pen", IQ_Color.WHITE, IQ_DashStyle.SOLID, 1); // Register the pen with which to fill text boxes DrawingRegisterPen("Text Fill Pen", IQ_Color.BLACK, IQ_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); // Get X value of anchor point 4 int x4 = DrawingAnchorPointBarShift(4); // Get the Y value of anchor point 4 double y4 = DrawingAnchorPointValue(4); // Draws the solid line from X to A and it's respective text info on the line if(x0 != x1 && y0 != y1){ DrawingSetLine("Solid Pen", x0, y0, "X\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(((MathAbs((x0 + x1) / 2)) + x0)/ 2); tempY = MathAbs(((MathAbs((y0 + y1) / 2)) + y1) / 2); // Writes the text on the drawing DrawingSetText("" + MathRound(MathAbs(y0 - y1), 2) + "\nXA: " + MathAbs(x0 - x1), tempX, tempY, "Text Pen", "Text Fill Pen") ; } // Draws the solid line from A to B and it's respective text info on the line // Also draws the .382XA retracement line if(x0 != x2 && y0 != y2){ DrawingSetLine("Solid Pen", x1, y1, "A\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)MathAbs(((MathAbs((x1 + x2) / 2)) + x1)/ 2); tempY = MathAbs(((MathAbs((y1 + y2) / 2)) + y1) / 2); // Writes the text on the drawing for AB line DrawingSetText("" + MathRound(MathAbs(y1 - y2), 2) + "\nAB: " + MathAbs(x1 - x2), tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the .382XA retracement line double retY = MathRound(MathAbs(y1 - (.382)*(MathAbs(y0 - y1))), 2); DrawingSetLine("Dashed Pen", x0, y0, "", x2, retY, "" + retY); // Draws a solid horizontal line on one end to look more clean DrawingSetLine("Solid Pen", x2 - 2, retY, "", x2 + 2, retY, ""); // Writes text on the retracement line tempX = (int)(MathAbs((x0 + x2) / 2)); tempY = MathAbs(((MathAbs((y0 + retY) / 2)) + retY) / 2); DrawingSetText("0.382XA", tempX, tempY, "Text Pen", "Text Fill Pen") ; } // Draws the solid line from B to C and it's respective text info on the line // Also draws the .866AB retracement line if(x0 != x3 && y0 != y3){ DrawingSetLine("Solid Pen", x2, y2, "B\n" + MathRound(y2, 2), x3, y3, ""); // 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(((MathAbs((x2 + x3) / 2)) + x2)/ 2); tempY = MathAbs(((MathAbs((y2 + y3) / 2)) + y3) / 2); // Writes the text on the drawing DrawingSetText("" + MathRound(MathAbs(y2 - y3), 2) + "\nBC: " + MathAbs(x2 - x3), tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the .886AB retracement line double retY = MathRound(MathAbs(y1 - (.114)*(MathAbs(y1 - y2))), 2); DrawingSetLine("Dashed Pen", x1, y1, "", x3, retY, "" + retY); // Draws a solid horizontal line on one end to look more clean DrawingSetLine("Solid Pen", x3 - 2, retY, "", x3 + 2, retY, ""); // Writes text on the retracement line tempX = (int)MathAbs(((MathAbs((x1 + x3) / 2)) + x1)/ 2); tempY = y1; DrawingSetText("0.886AB", tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the necessary shaded triangle using the C point retracement line DrawingSetPolygon("Blank Pen", "Fill Pen", new int[] {x1, x3, x3}, new double[] {y1, retY, MathAbs((y2 + y3) / 2)}, new string[] {"", "", "", ""}); } // Draws the solid line from C to D and it's respective text info on the line // Also draws the two retracement lines after D is placed if(x0 != x4 && y0 != y4){ DrawingSetLine("Solid Pen", x3, y3, "C\n" + MathRound(y3, 2), x4, y4, "D\n" + MathRound(y4, 2)); // 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(((MathAbs((x3 + x4) / 2)) + x3)/ 2); tempY = MathAbs(((MathAbs((y3 + y4) / 2)) + y3) / 2); // Writes the text on the drawing DrawingSetText("" + MathRound(MathAbs(y3 - y4), 2) + "\nCD: " + MathAbs(x3 - x4), tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the 1.13XA retracement line double retY = MathRound(MathAbs(y0 - (.113)*(MathAbs(y0 - y1))), 2); DrawingSetLine("Dashed Pen", x0, y0, "", x4, retY, "" + retY); // Draws a solid horizontal line on one end to look more clean DrawingSetLine("Solid Pen", x4 - 2, retY, "", x4 + 2, retY, ""); // Writes text on the retracement line tempX = (int)MathAbs(((MathAbs((x0 + x4) / 2)) + x0)/ 2); tempY = y0; DrawingSetText("1.13XA", tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the 2.0BC reatracement line retY = MathRound(MathAbs(y2 - (MathAbs(y2 - y3))), 2); DrawingSetLine("Dashed Pen", x2, y2, "", x4, retY, "" + retY); // Draws a solid horizontal line on one end to look more clean DrawingSetLine("Solid Pen", x4 - 2, retY, "", x4 + 2, retY, ""); // Writes text on the retracement line tempX = (int)MathAbs(((MathAbs((x2 + x4) / 2)) + x2)/ 2); tempY = MathAbs(((MathAbs((y2 + retY) / 2)) + y2) / 2); DrawingSetText("2.0BC", tempX, tempY, "Text Pen", "Text Fill Pen") ; // Draws the necessary shaded triangle using retracement lines and point D // 3rd Y value is a 3.618 retracement of y3 DrawingSetPolygon("Blank Pen", "Fill Pen", new int[] {x2, x4, x4}, new double[] {y2, retY, (retY - (MathAbs(retY - y2) + (.618)*MathAbs(y2 - y3)))}, new string[] {"", "", "", ""}); } } #endregion } }

The Algorithmic Trading Software for Hedge Funds and Quants