Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The calling thread can not access this object ...

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

    The calling thread can not access this object ...

    I still keep getting this error with this indicator. I'm freezing all the brushes. I've narrowed the error to the Draw.Text call. Any ideas?

    Code:
    #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.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 VwapProfile : Indicator
    	{
    		private const int number = 5;
    		private double iCumVolume = 0;
    		private double iCumTypicalVolume = 0;
    		private List<double> vwaps;
    	
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description					= @"";
    				Name						= "VWAP Profile";
    				Calculate					= Calculate.OnBarClose;
    				IsAutoScale					= false;
    				IsOverlay					= true;
    				DisplayInDataBox			= true;
    				DrawOnPricePanel			= true;
    				DrawHorizontalGridLines		= true;
    				DrawVerticalGridLines		= true;
    				PaintPriceMarkers			= false;
    				ScaleJustification			= NinjaTrader.Gui.Chart.ScaleJustification.Right;
    				//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;
    
    				//	SET DEFAULT VALUES
    				Opacity = 0.35f;
    				
    				//	ADD PLOTS					
    				AddPlot(new Pen( Brushes.Cyan, 1), PlotStyle.Dot, "V0");
    				AddPlot(new Pen( Brushes.LightGreen, 1), PlotStyle.Dot, "V1");
    				AddPlot(new Pen( Brushes.Magenta, 1), PlotStyle.Dot, "V2");
    				AddPlot(new Pen( Brushes.Red, 1), PlotStyle.Dot, "V3");
    				AddPlot(new Pen( Brushes.Orange, 1), PlotStyle.Dot, "V4");
    			}
    			else if (State == State.Configure)
    			{
    				vwaps = new List<double>();
    				vwaps.Insert(0, 0d);
    				for(int i = 0; i < number; i++) 
    				{
    					Brush tmp = Plots[i].Brush.Clone();
    					tmp.Opacity = Opacity;
    					tmp.Freeze();
    					Plots[i].Brush = tmp;
    				}
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if(Bars.IsFirstBarOfSession) 
    			{
    				//	RESET CURRENT
    				iCumVolume = VOL()[0]; 
    				iCumTypicalVolume = VOL()[0] * Typical[0];
    				//	ADD CURRENT
    				vwaps.Insert(0, iCumTypicalVolume/iCumVolume);
    				//	REMOVE EXTRA VWAPS
    				if(vwaps.Count > number) vwaps.RemoveAt(vwaps.Count - 1);
    			} 
    			else 
    			{
    				iCumVolume = iCumVolume + VOL()[0];
    				iCumTypicalVolume = iCumTypicalVolume + (VOL()[0] * Typical[0]);
    				//	UPDATE CURRENT
    				vwaps[0] = iCumTypicalVolume/iCumVolume;
    			}
    
                if(vwaps.Count > 4) 
    			{ 
    				V0[0] = vwaps[0];
    				V1[0] = vwaps[1];
    				V2[0] = vwaps[2];
    				V3[0] = vwaps[3];
    				V4[0] = vwaps[4];
    				
    				for(int i = 0; i < number; i++)
    				{
    					string tag = string.Format("V{0}", i.ToString("N0"));
    					bool isAutoScale = false;
    					string text = string.Format( "V{0} {1}", i.ToString("N0"), vwaps[0].ToString("F"));
    					int barsAgo = 0;
    					double y = vwaps[i];
    					int pixelOffset = 12;
    					Brush textBrush = Brushes.White; 
    					textBrush.Freeze();
    					NinjaTrader.Gui.Tools.SimpleFont font = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 10);
    					Brush outlineBrush = Brushes.White; 
    					outlineBrush.Freeze();
    					Brush areaBrush = Plots[i].Brush.Clone(); 
    					areaBrush.Freeze();
    					int areaOpacity = Convert.ToInt32( Opacity * 100 );
    			
    					try
    					{
    						Text T = Draw.Text(this, tag, isAutoScale, text, barsAgo, y, pixelOffset, textBrush, font, TextAlignment.Justify, outlineBrush, areaBrush, areaOpacity);
    					}
    					catch(System.Exception e)
    					{
    						System.Windows.MessageBox.Show(e.Message);
    					}
    				}
    			}
    		}
    						
    		#region Properties
    		[Range(0, 1)]
    		[NinjaScriptProperty]		
    		[Display(Name = "Opacity", GroupName = "Parameters", Order = 0)]
    		public float Opacity
    		{
    			get; set;
    		}		
    		
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> V0
    		{
    			get { return Values[0]; }
    		}
    
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> V1
    		{
    			get { return Values[1]; }
    		}
    		
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> V2
    		{
    			get { return Values[2]; }
    		}
    
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> V3
    		{
    			get { return Values[3]; }
    		}
    
    		[Browsable(false)]
    		[XmlIgnore]
    		public Series<double> V4
    		{
    			get { return Values[4]; }
    		}		
    		#endregion
    
    	}
    }
    
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    	{
    		private VwapProfile[] cacheVwapProfile;
    		public VwapProfile VwapProfile(float opacity)
    		{
    			return VwapProfile(Input, opacity);
    		}
    
    		public VwapProfile VwapProfile(ISeries<double> input, float opacity)
    		{
    			if (cacheVwapProfile != null)
    				for (int idx = 0; idx < cacheVwapProfile.Length; idx++)
    					if (cacheVwapProfile[idx] != null && cacheVwapProfile[idx].Opacity == opacity && cacheVwapProfile[idx].EqualsInput(input))
    						return cacheVwapProfile[idx];
    			return CacheIndicator<VwapProfile>(new VwapProfile(){ Opacity = opacity }, input, ref cacheVwapProfile);
    		}
    	}
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    	{
    		public Indicators.VwapProfile VwapProfile(float opacity)
    		{
    			return indicator.VwapProfile(Input, opacity);
    		}
    
    		public Indicators.VwapProfile VwapProfile(ISeries<double> input , float opacity)
    		{
    			return indicator.VwapProfile(input, opacity);
    		}
    	}
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    	{
    		public Indicators.VwapProfile VwapProfile(float opacity)
    		{
    			return indicator.VwapProfile(Input, opacity);
    		}
    
    		public Indicators.VwapProfile VwapProfile(ISeries<double> input , float opacity)
    		{
    			return indicator.VwapProfile(input, opacity);
    		}
    	}
    }
    
    #endregion

    #2
    Originally posted by GrumpyTrader View Post
    I still keep getting this error with this indicator. I'm freezing all the brushes. I've narrowed the error to the Draw.Text call. Any ideas?

    When changing from a "ES 03-15" 4 range chart to a 3 minute chart it is fine.

    When changing from a 4 range chart or 3 minute chart to a 1 minute chart - I get "Object reference not set to an instance of an object."

    The message won't go away. It keeps popping up. I can navigate to other windows in Beta2 and work on them. When I try to access the chart with the indicator and go to the indicators to remove it - that chart will lock up. And the message is still there and keeps coming back.

    Not connected to anything like Playback.
    Last edited by sledge; 07-06-2015, 09:56 PM. Reason: new terms

    Comment


      #3
      In Playback - Historical - I get:

      "Indicator VWAP Profile": Error on calling 'OnRender' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      I switched to Playback - Market Replay and that's spinning circle of death. The chart is incapacitated and dead, but everything else in NT is responsive.

      Comment


        #4
        You are likely facing a bug in Draw.Text itself which is fixed in Beta 3. We're working hard to get beta 3 out ASAP this week however could be pushed to next week. Trying to wrap up some last minute release items before we could release.

        Sorry for the hair pulling!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Haiasi, Today, 06:53 PM
        1 response
        4 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by ScottWalsh, Today, 06:52 PM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by ScottW, Today, 06:09 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by ftsc2022, 10-25-2022, 12:03 PM
        5 responses
        256 views
        0 likes
        Last Post KeyonMatthews  
        Started by Board game geek, 10-29-2023, 12:00 PM
        14 responses
        244 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Working...
        X