Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw.Text, Plot suggestions, etc

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

    Draw.Text, Plot suggestions, etc

    After working through the following indicator I have the following questions, possible bugs and suggestions:

    The attached indicator plots a history of daily vwaps and adds a text marker for the current day identifying which vwap it is and the level the vwap was at.

    Questions:
    * Can I set the right margin of a chart programically to ensure the text fits?
    * The Draw.Text seems to be limited to a baroffset of 0. In this indicator I would ideally want to have the text plotted with a negative offset, so as not to block the current bar on the chart. Is there a means to do that? If not, it might make sense to add either a xPixelOffset like you have for the yPixelOffset, or allow for negative bar offsets.

    Possible Bugs:
    * When I load this indicator I sometimes get the following 2 errors when drawing text: Indicator VWAP Profile: Error on calling OnBarUpdate method on bar 1202: Object reference not set to an instance of an object. Failed to call OnRender() for chart object Text: The calling thread cannot access this object because a different thread owns it.
    * For efficiency sake, I tried to encase the drawing of the text object to only the last bar on the chart. I thought the proper approach would be to check the State property is equal to Transition. That would then process the DrawText on the last historical bar available. Unfortunately that doesn't seem to work.
    * Not sure if the border brush is working on the Draw.Text method. I set it to white but I don't see the border being drawn.

    Suggestions:
    * Instead of drawing the text markers, I tried to use the FormatPriceMarker override to return the string with the plot labels but that doesn't seem to work. Even though this method returns a string, is it restricted to only returning strings that convert to a numeric?
    * You will notice that the plots PlotStyle are set as Dots. That is to eliminate the connecting lines between sessions when the PlotStyle is set to line. Cosmetically I would much rather be able to use lines. Is there a simple means to eliminate the connecting lines between sessions? This might be a nice option to add to plots. The other suggestions is allow for even smaller dots. The smallest dot setting is still quite large relative to a line width.
    Attached Files
    Last edited by GrumpyTrader; 07-05-2015, 05:36 PM.

    #2
    Hi GrumpyTrader,

    I wanted to let you know I am working out a reply for you but this is taking a bit longer than expected due to needing to test some of this.

    I appreciate your patience while I get a response ready.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,
      Just wanted you to know so you don't spin your wheels, that the error I was receiving was to do with a) having the freeze the brushes and b) a bug in the Draw.Text that will be fixed in beta3.

      Also, the efficiency sake line, I was able to get what I wanted with the following code update to the OnStateChange and OnBarUpdate method.

      Code:
      	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()
      		{
         			Print(DateTime.Now + ": Current State is State."+State);
      			
      			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.20f;
      				
      				//	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;
      				}
      			}
      			else if (State == State.Realtime)
      			{
      				DrawMarkers();
      			}
      		}
      
      		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];
      				
      				if(State == State.Realtime) DrawMarkers();
      			}
      		}
      		
      		private void DrawMarkers()
      		{
      			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[i].ToString("F"));
      				int barsAgo = 0;
      				double y = vwaps[i];
      				int pixelOffset = 13;
      				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 );
      		
      				Text T = Draw.Text(this, tag, isAutoScale, text, barsAgo, y, pixelOffset, textBrush, font, TextAlignment.Right, outlineBrush, areaBrush, areaOpacity);
      			}
      		}
      								
      		#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
      Last edited by GrumpyTrader; 07-09-2015, 12:25 PM.

      Comment


        #4
        Hello GrumpyTrader,

        The right side margin can be set using ChartControl.Properties.BarMarginRight = n;

        Yes, you can use a negative value when placing Draw.Text() to place the textbox forward of the bars on the chart.

        The error about threading is related to Draw.Text and I am finding is resolved with Beta 3 (when that comes out).

        With the efficiency issue, I recommend when historical data begins to save the bar count. Then in OnBarUpdate on drawn on that bar.

        The issue with the text box border requires that you enable the outline similar to if you drew the text box manually. (by default when a manual text box is drawn the outline is not enabled by default).
        For example:
        Draw.Text(this, "box", true, "some test", 0, Low[0]-3*TickSize, 0, Brushes.White, myFont, TextAlignment.Center, Brushes.White, Brushes.Green, 60).IsOutlineVisible = true;


        With the dot style I will forward this as a feature request to development in your behalf. It will be up to the development team to decide to implement this request.

        Please let me know if you have any other feature requests you would like me to submit.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kujista, Today, 06:23 AM
        4 responses
        13 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by traderqz, Yesterday, 09:06 AM
        2 responses
        15 views
        0 likes
        Last Post traderqz  
        Started by traderqz, Today, 12:06 AM
        3 responses
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by RideMe, 04-07-2024, 04:54 PM
        5 responses
        28 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by f.saeidi, Today, 08:13 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X