Summary:

This drawing script is used for measuring the risk vs. reward between three points on a chart, stop, entry and target.

#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 3 anchor points. DrawingInitialize(symbolIndex, 3); } #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 risk ruler. DrawingRegisterPen("Risk Pen", new byte[] { 5, 110, 180, 255 }, AT_DashStyle.SOLID, 2); // Register the pen with which to draw the reward ruler. DrawingRegisterPen("Reward Pen", new byte[] { 5, 110, 180, 255 }, AT_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 number of decimals for the symbol. int symbolDecimals = SymbolDecimals(SymbolIndex()); // Get the X value of anchor point 0. int x1 = DrawingAnchorPointBarShift(0); // Get the Y value of anchor point 0. double y1 = Math.Round(DrawingAnchorPointValue(0), symbolDecimals); // Get the X value of anchor point 1. int x2 = DrawingAnchorPointBarShift(1); // Get the Y value of anchor point 1. double y2 = Math.Round(DrawingAnchorPointValue(1), symbolDecimals); // Get the X value of anchor point 2. int x3 = DrawingAnchorPointBarShift(2); // Get the Y value of anchor point 2. double y3 = Math.Round(DrawingAnchorPointValue(2), symbolDecimals); // Calculate the risk price difference. double riskPriceDifference = Math.Round(Math.Abs(y2 - y1), symbolDecimals); // Calculate the risk in ticks. double riskTicks = Math.Round(riskPriceDifference / SymbolTick(SymbolIndex()), 0); // Calculate the risk change. double riskChange = Math.Round(100 * Math.Abs(y2 - y1) / Math.Min(y1, y2), 1); // Draw the risk line. DrawingSetLine("Risk Pen", x1, y1, "", x2, y2, ""); // Calculate the reward price difference. double rewardPriceDifference = Math.Round(Math.Abs(y3 - y2), symbolDecimals); // Calculate the reward in ticks. double rewardTicks = Math.Round(rewardPriceDifference / SymbolTick(SymbolIndex()), 0); // Calculate the reward change. double rewardChange = Math.Round(100 * Math.Abs(y3 - y2) / Math.Min(y2, y3), 1); // Calculate the reward/risk ratio. double riskReward = Math.Round(rewardTicks / Math.Max(riskTicks, 1), 2); // Set the target string. string target = "Target: " + y3 + " | " + rewardTicks + "T | " + rewardChange + "%"; // Set the entry string. string entry = "Entry: " + y2 + " | R/R: " + riskReward; // Set the risk string. string risk = "Stop: " + y1 + " | " + riskTicks + "T | " + riskChange + "%"; // Draw the risk line. DrawingSetLine("Reward Pen", x2, y2, "", x3, y3, target + "\n" + entry + "\n" + risk); } #endregion } }

The Algorithmic Trading Software for Hedge Funds and Quants

AlgoTerminal LLC is the developer of the world's most sophisticated algorithmic trading software for buy-side firms and professional quants.

© 2018 AlgoTerminal LLC. All rights reserved.

RISK DISCLOSURE: Equities, Futures and Forex trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than his initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not an indication of future performance.