#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 7 anchor points. DrawingInitialize(symbolIndex, 7); } #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); // Get X value of anchor point 4 int x4 = DrawingAnchorPointBarShift(4); // Get the Y value of anchor point 4 double y4 = DrawingAnchorPointValue(4); // Get X value of anchor point 5 int x5 = DrawingAnchorPointBarShift(5); // Get the Y value of anchor point 5 double y5 = DrawingAnchorPointValue(5); // Get X value of anchor point 6 int x6 = DrawingAnchorPointBarShift(6); // Get the Y value of anchor point 6 double y6 = DrawingAnchorPointValue(6); // Draws the first solid line and it's respective text info on the line if(x0 != x1){ DrawingSetLine("Solid Pen", x0, y0, "", x1, y1, "1\n" + MathRound(y1, 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((x0 + x1) / 2)) + x0)/ 2); tempY = (MathAbs((y0 + y1) / 2)); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y0 - y1), 2) + "\nBars: " + MathAbs(x0 - x1), tempX, tempY, "Text Pen", "Text Fill Pen"); } // Draws the second solid line and it's respective text info on the line if(x0 != x2){ DrawingSetLine("Solid Pen", x1, y1, "", 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((x1 + x1 + x2) / 3); tempY = MathAbs((y1 + y2 + y2) / 3); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y1 - y2), 2) + "\nBars: " + MathAbs(x1 - x2), tempX, tempY, "Text Pen", "Text Fill Pen"); } // Draws the third solid line and it's respective text info on the line // And draws the first 1.618 retracement line if(x0 != x3){ DrawingSetLine("Solid Pen", x2, y2, "", x3, y3, "2\n" + MathRound(y2, 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((x2 + x2 + x2 + x3) / 4); tempY = MathAbs((y2 + y2 + y3) / 3); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y2 - y3), 2) + "\nBars: " + MathAbs(x2 - x3), tempX, tempY, "Text Pen", "Text Fill Pen"); // Draws the 1.618 retracement line double retY = MathRound(MathAbs(y1 - (.618)*(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 = MathAbs((y1 + y1 + retY) / 3); DrawingSetText("1.618", tempX, tempY, "Text Pen", "Text Fill Pen"); // Draws the necessary shaded triangle using the retracement line and point '2' (x3) DrawingSetPolygon("Blank Pen", "Fill Pen", new int[] {x1, x3, x3}, new double[] {y1, retY, (y1 - ((.27)*MathAbs(y1 - y2)))}, new string[] {"", "", "", ""}); } // Draws the fourth solid line and it's respective text info on the line if(x0 != x4){ DrawingSetLine("Solid Pen", x3, y3, "", x4, y4, ""); // 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((y3 + y4 + y4) / 3); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y3 - y4), 2) + "\nBars: " + MathAbs(x3 - x4), tempX, tempY, "Text Pen", "Text Fill Pen"); } // Draws the fifth solid line and it's respective text info on the line // Also draws the second 1.618 retracement line if(x0 != x5){ DrawingSetLine("Solid Pen", x4, y4, "", x5, y5, "3\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((x4 + x5) / 2)) + x4)/ 2); tempY = MathAbs((y4 + y5) / 2); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y4 - y5), 2) + "\nBars: " + MathAbs(x4 - x5), tempX, tempY, "Text Pen", "Text Fill Pen"); // Draws the 1.618 retracement line double retY = MathRound(MathAbs(y3 - (.618)*(MathAbs(y3 - y4))), 2); DrawingSetLine("Dashed Pen", x3, y3, "", x5, retY, "" + retY); // Draws a solid horizontal line on one end to look more clean DrawingSetLine("Solid Pen", x5 - 2, retY, "", x5 + 2, retY, ""); // Writes text on the retracement line tempX = (int)(MathAbs((x3 + x3 + x5) / 3)); tempY = MathAbs((y3 + y3 + retY) / 3); DrawingSetText("1.618", tempX, tempY, "Text Pen", "Text Fill Pen"); // Draws the necessary shaded triangle using the retracement line and point '3' (x5) DrawingSetPolygon("Blank Pen", "Fill Pen", new int[] {x3, x5, x5}, new double[] {y3, retY, (y3 - ((.27)*MathAbs(y3 - y4)))}, new string[] {"", "", "", ""}); } // Draws the last solid line and it's respective text info on the line if(x0 != x6){ DrawingSetLine("Solid Pen", x5, y5, "", x6, y6, ""); // 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((x5 + x6) / 2)) + x5)/ 2); tempY = (MathAbs((y5 + y6 + y6) / 3)); // Writes the text on the line just drawn DrawingSetText("" + MathRound(MathAbs(y5 - y6), 2) + "\nBars: " + MathAbs(x5 - x6), tempX, tempY, "Text Pen", "Text Fill Pen"); } } #endregion } }

The Algorithmic Trading Software for Hedge Funds and Quants