#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
// The underlying symbol index for the drawing.
private int _symbolIndex;
#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("Gann Fan");
// Keep the symbol index.
_symbolIndex = symbolIndex;
}
#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 1x1 gann line.
DrawingRegisterPen("1x1", new int[] {
22 ,81, 238, 255
}, C_DashStyle.SOLID, 2);
// Register the pen with which to draw the 1x2 gann line.
DrawingRegisterPen("1x2", new int[] {
35, 145, 255, 255
}, C_DashStyle.SOLID, 2);
// Register the pen with which to draw the 1x3 gann line.
DrawingRegisterPen("1x3", new int[] {
100, 150, 235, 255
}, C_DashStyle.SOLID, 2);
// Register the pen with which to draw the 1x4 gann line.
DrawingRegisterPen("1x4", new int[] {
75, 135, 185, 255
}, C_DashStyle.SOLID, 2);
// Register the pen with which to draw the 1x8 gann line.
DrawingRegisterPen("1x8", new int[] {
10, 140, 145, 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);
if (x1 < 0)
x1 = 0;
if (x2 < 0)
x2 = 0;
// Find the maximum X.
int maxX = Math.Max(x1, x2);
// Find the minimum X.
int minX = Math.Min(x1, x2);
// Get the number of bars in the drawing range.
int length = Math.Abs(x1 - x2);
// Find the highest high and lowest low in the drawing bar range.
double highest = 0;
double lowest = 10000000;
// Iterate over all of the highs and all of the lows
for (int i = 0; i < length; i++) {
// Check whether a new high was found.
if (DataHigh(minX + i) > highest) {
highest = DataHigh(minX + i);
}
// Check whether a new low was found.
if (DataLow(minX + i) < lowest) {
lowest = DataLow(minX + i);
}
}
// Calculate the price advance per bar in the drawing bar range.
double advance = 0;
if (length != 0) {
advance = (highest - lowest) / length;
}
// Get the Y value of anchor point 1.
double y2 = advance * (maxX - minX) + y1;
// Fix the anchor point.
DrawingSetAnchorPoint(1, x2, y2);
// Draw the gann lines.
DrawingSetLine("1x1", x1, y1, "", x2, y2, "1x1");
DrawingSetLine("1x2", x1, y1, "", x2, 0.5833 * advance * Math.Abs(x2 - x1) + y1, "1x2");
DrawingSetLine("1x2", x1, y1, "", x2, 1.4166 * advance * Math.Abs(x2 - x1) + y1, "1x2");
DrawingSetLine("1x3", x1, y1, "", x2, 0.4166 * advance * Math.Abs(x2 - x1) + y1, "1x3");
DrawingSetLine("1x3", x1, y1, "", x2, 1.5833 * advance * Math.Abs(x2 - x1) + y1, "1x3");
DrawingSetLine("1x4", x1, y1, "", x2, 0.3333 * advance * Math.Abs(x2 - x1) + y1, "1x4");
DrawingSetLine("1x4", x1, y1, "", x2, 1.6666 * advance * Math.Abs(x2 - x1) + y1, "1x4");
DrawingSetLine("1x8", x1, y1, "", x2, 0.1666 * advance * Math.Abs(x2 - x1) + y1, "1x8");
DrawingSetLine("1x8", x1, y1, "", x2, 1.8333 * advance * Math.Abs(x2 - x1) + y1, "1x8");
}
#endregion
}
}