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 "";}
}
}
}
Comment