Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

DrawText ATR

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    DrawText ATR

    Originally posted by NinjaTrader_ChelseaB View Post
    Hello D Trader,

    Below are links to an example that demonstrates modifying the Chart Trader area.


    Attached is another example of this.
    Hi Chelsea - I am seeking help on a similar question. I am trying to display point value, ATR, and 2 other calculated values on my charts. I've created an indicator to do this (code copied below). But I am getting errors with the way ATR is calculating and issues with referring to OnBarUpdate(). I would greatly appreciate help in understanding if there is an alternative way to display values and text on the chart.

    The example you attached adds test using string.message, is this a better way to do this? Can you help me simplify this to eliminate the reference to button actions?

    The ATR from the code below is not calculating correctly and I am getting an error stating:
    Error on calling 'OnBarUpdate' method on bar 15 (Gui.Chart): The calling thread cannot access the object because a different thread owns it.

    I would greatly appreciate help in correcting my code to display these values on the chart.

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class IndicatorLegend : Indicator
    {
    private double atrValue = 0;
    private double riskperlot = 0;
    private double stop = 0;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Displays PV, Risk/Lot, Stop";
    Name = "IndicatorLegend";
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    Calculate = Calculate.OnBarClose;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    }

    }
    protected override void OnBarUpdate()
    {

    double currentBid = GetCurrentBid();

    //currentBid is using historical data,

    if(CurrentBar <= 14) return;

    atrValue = ATR(14)[0];
    riskperlot = atrValue*Instrument.MasterInstrument.PointValue;
    stop = currentBid - atrValue;

    Draw.TextFixed(this, "chartlegend", "Point Value = " + Instrument.MasterInstrument.PointValue + Environment.NewLine +
    "Risk/lot = " + riskperlot.ToString("F") + "ATR = " + atrValue.ToString("F") + Environment.NewLine +
    "STOP = " + stop.ToString("F") + "CurrentBid " + currentBid.ToString("F"),
    TextPosition.TopLeft, Brushes.White, new Gui.Tools.SimpleFont("Arial", 12), Brushes.Transparent, Brushes.Transparent, 100);
    ChartControl.InvalidateVisual();

    //Add your custom indicator logic here.
    }


    public override string DisplayName
    {
    get {return "";}
    }
    }
    }

    #2
    Hello ashlantz,

    The example I have posted in thread 94255 demonstrates how to make modifications to the chart UI in Chart Trader by changing the colors of the Buy Market and Sell Market buttons and adds custom event handlers to these.
    Your inquiry appears to be of a different subject and does not involve modifying the Buy Market and Sell market buttons in ChartTrader (or other UI items).

    Because of this I have moved your post into a new thread as your inquiry appears to be unrelated.

    Further, to export a NinjaTrader 8 NinjaScript so that this may be shared:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message

    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


    Below is a link to the help guide on Exporting NinjaScripts.



    It appears in your script you are wanting to Draw.TextFixed() and put a few calculated values in a text box.


    Are you certain that this script is having an error?

    I've added the code to a script and tested but I am not getting any errors.
    I've exported the script and attached the export so that you can see what I am testing.
    Attached is a screenshot of the results when adding the script to a chart.

    Have you attempted to add prints to see which line is causing the error?
    Which line is causing the error?


    A quick note, ChartControl.InvalidateVisual(); is not supported to use. Instead use ChartControl.ForceRefresh().
    Your script, however, updates when the bar closes. There is absolutely no need for this to be in OnBarUpdate() at all.

    Also, you may find it helpful to use string.Format.
    For example:
    Code:
    string message = string.Format("Point Value = {0:F}\r\nRisk/lot = {1:F}\r\nATR = {2:F}\r\nSTOP = {3:F}\r\nCurrentBid = {4:F}", Instrument.MasterInstrument.PointValue, riskperlot, atrValue, stop, currentBid);
    
    Draw.TextFixed(this, "chartlegend", message, TextPosition.TopLeft, Brushes.White, new Gui.Tools.SimpleFont("Arial", 12), Brushes.Transparent, Brushes.Transparent, 100);
    Insert values into a string with string.Format. Specify percentages, decimals and padding.
    Attached Files
    Last edited by NinjaTrader_ChelseaB; 04-04-2017, 12:25 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea - Thank you very much for the reply and the help. I have successfully made the changes you suggested, and on future posts I will insert the code in a more readable format. Also, thank you for the string message formatting help. I am a beginner and appreciate your time and effort in replying.

      Regards,
      Ashley

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by funk10101, Today, 12:02 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by GLFX005, Today, 03:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by nandhumca, Yesterday, 03:41 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by The_Sec, Yesterday, 03:37 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by vecnopus, Today, 06:15 AM
      0 responses
      1 view
      0 likes
      Last Post vecnopus  
      Working...
      X