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

Accessing Bars' data from drawing tool code

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

    Accessing Bars' data from drawing tool code

    Hi there,
    I'm trying to have a rectangle, that once drawn, can access the bars inside it data.
    I mannged to get the bars numbers, but couldn't access more.
    will be happy to get any help

    here is the code :
    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.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    
    #endregion
    
    //This namespace holds Drawing tools in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.DrawingTools
    {
        public class DynamicVolumeProfile : ShapeBase
        {
    
            public override object Icon { get { return Gui.Tools.Icons.DrawRectangle; } }
    
            protected override void OnStateChange()
            {
                base.OnStateChange();
                if (State == State.SetDefaults)
                {
                    Name        = "DynamicVolumeProfile";
                    ShapeType    = ChartShapeType.Rectangle;
    
                }
            }
    
    
            public override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                base.OnRender(chartControl, chartScale);
                Stroke outlineStroke        = OutlineStroke;
                RenderTarget.AntialiasMode    = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                outlineStroke.RenderTarget    = RenderTarget;
                ChartPanel chartPanel        = chartControl.ChartPanels[PanelIndex];
                Point startPoint            = StartAnchor.GetPoint(chartControl, chartPanel, chartScale);
                Point endPoint                = EndAnchor.GetPoint(chartControl, chartPanel, chartScale);
    
                double width                = endPoint.X - startPoint.X;
                double height                = endPoint.Y - startPoint.Y;
                double strokePixAdjust =    outlineStroke.Width % 2 == 0 ? 0.5d : 0d;
    
                SharpDX.Vector2 centerPoint    = (startPoint + ((endPoint - startPoint) / 2)).ToVector2();
    
                SimpleFont TextFont = new Gui.Tools.SimpleFont() { Size = 10}; //new SimpleFont("Consolas", width/8);
                TextFont.Size = 40;
                SharpDX.DirectWrite.TextFormat textFormat = TextFont.ToDirectWriteTextFormat();
                textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center;
                textFormat.WordWrapping = SharpDX.DirectWrite.WordWrapping.NoWrap;
                SharpDX.RectangleF layoutRect = new SharpDX.RectangleF((float)(startPoint.X + strokePixAdjust),
                                                                        (float)(startPoint.Y + strokePixAdjust),
                                                                        (float)(width), (float)(height));
    
    
    
    
    
    
                RenderTarget.DrawText(StartAnchor.SlotIndex.ToString() + " " + EndAnchor.SlotIndex.ToString(), textFormat, layoutRect, Brushes.Blue.ToDxBrush(RenderTarget));
                textFormat.Dispose();
    
            }
    
        }
    }​
    Elad.

    #2
    Hello Elad,

    Thank you for your post and welcome to the NinjaTrader forum community!

    You mentioned you were able to get the bar numbers but you were not able to access the desired bar data. What kind of data are you trying to access? For the bar numbers, I suspect you will need to get a ChartBars index value in order to make sure you are referencing the correct bar to get the data you are looking for. You may get a bar index from an x-coordinate with GetBarIdxByX():


    I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      Thansk for your help.
      Very helpful.
      I'm also able to get use :

      Code:
      ChartBars myBars = GetAttachedToChartBars();
      Print(myBars.Bars.GetHigh((int)StartAnchor.SlotInd ex));​
      Now I'm trying to get other bars' data.
      I've created my own bar type as follows, and when using this bar type I hoped to be able to access the public data (not only HLOC).
      How can I do so?

      here is the new bartype I''ve added , currnetly with "bardelta" that I would like to retrive in the drawing tool object.

      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.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      
      #endregion
      
      //This namespace holds Bars types in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.BarsTypes
      {
          public class MinutesVolumeticks : MinuteBarsType
          {
              public Dictionary<double, double> asks = new Dictionary<double, double>();
              public Dictionary<double, double> bids = new Dictionary<double, double>();
              public double bardelta=0;
              private int activeBar = 0;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Bars type here.";
                      Name                                        = "MinutesVolumeticks";
                      BarsPeriod                                    = new BarsPeriod { BarsPeriodType = (BarsPeriodType) 16, Value = 1 };
                      BuiltFrom                                    = BarsPeriodType.Minute;
                      DaysToLoad                                    = 5;
                      IsIntraday                                    = true;
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
      
              private DateTime TimeToBarTime(Bars bars, DateTime time, bool isBar)
              {
                  if (SessionIterator.IsNewSession(time, isBar))
                      SessionIterator.GetNextSession(time, isBar);
      
                  if (bars.IsResetOnNewTradingDay || (!bars.IsResetOnNewTradingDay && bars.Count == 0))
                  {
                      DateTime barTimeStamp = isBar
                          ? SessionIterator.ActualSessionBegin.AddMinutes(Math.Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
                          : SessionIterator.ActualSessionBegin.AddMinutes(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(SessionIterator.ActualSessionBegin).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
                      if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd) // Cut last bar in session down to session end on odd session end time
                          barTimeStamp = SessionIterator.ActualSessionEnd;
                      return barTimeStamp;
                  }
                  else
                  {
                      DateTime lastBarTime    = bars.GetTime(bars.Count - 1);
                      DateTime barTimeStamp    = isBar
                          ? lastBarTime.AddMinutes(Math.Ceiling(Math.Ceiling(Math.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value)
                          : lastBarTime.AddMinutes(bars.BarsPeriod.Value + Math.Floor(Math.Floor(Math.Max(0, time.Subtract(lastBarTime).TotalMinutes)) / bars.BarsPeriod.Value) * bars.BarsPeriod.Value);
                      if (bars.TradingHours.Sessions.Count > 0 && barTimeStamp > SessionIterator.ActualSessionEnd)
                      {
                          DateTime saveActualSessionEnd = SessionIterator.ActualSessionEnd;
                          SessionIterator.GetNextSession(SessionIterator.ActualSessionEnd.AddSeconds(1), isBar);
                          barTimeStamp = SessionIterator.ActualSessionBegin.AddMinutes((int) barTimeStamp.Subtract(saveActualSessionEnd).TotalMinutes);
                      }
                      return barTimeStamp;
                  }
              }
      
              protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
              {
                  if (SessionIterator == null)
                      SessionIterator = new SessionIterator(bars);
      
                  if (bars.Count == 0)
                      AddBar(bars, open, high, low, close, TimeToBarTime(bars, time, isBar), volume);
                  else if (!isBar && time < bars.LastBarTime)
                      UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
                  else if (isBar && time <= bars.LastBarTime)
                      UpdateBar(bars, high, low, close, bars.LastBarTime, volume);
                  else
                  {
                      time        = TimeToBarTime(bars, time, isBar);
                      AddBar(bars, open, high, low, close, time, volume);
      
                      ///
                      ///
                      if (!asks.ContainsKey(close)) asks.Add(close,0);    
                      if (!bids.ContainsKey(close)) bids.Add(close,0);
      
                      if(close >= ask){
                          asks[close] += volume;
                          bardelta+= volume;
                      }else if (close <= bid){
                          bids[close] += volume;
                          bardelta-= volume;
                      }
                      ///
      
                  }
              }
      
          }
      }
      ​

      Comment


        #4
        Hello Elad,

        Thank you for your reply.

        You are correct, GetAttachedToChartBars() allows you to get the bars object that the drawing tool is attached to:


        When it comes to accessing properties and methods from your barsType, this requires type casting. The Order Flow Volumetric bars are a great example of this; you could follow the idea demonstrated in the snippet found on the help guide page here:


        As an example with a custom barsType, the type casting to access a property might look something like this:
        Code:
        NinjaTrader.NinjaScript.BarsTypes.MyCustomBars barsType = Bars.BarsSeries.BarsType as NinjaTrader.NinjaScript.BarsTypes.MyCustomBars;
        This should allow you to access a property, such as barsType.MyProperty, in your script.

        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi again and thanks!
          Given the code above , now I can access
          Code:
          NinjaTrader.NinjaScript.BarsTypes.MinutesVolumetic ks _mybars = myBars.Bars.BarsSeries.BarsType as NinjaTrader.NinjaScript.BarsTypes.MinutesVolumetic ks;
          ​
          _mybars.bardelta;
          But how can I get the bardelta (MinutesVolumetic property) for a specific bar (using it's bar id)?

          Elad.

          Comment


            #6
            Hello Elad,

            Thank you for your reply.

            In the volumetric bars example, it seems that the methods and properties are added to a collection called Volumes that allows access to those values for a bar index. This has been discussed in the following thread, though it does venture outside the bounds of NinjaTrader support:
            Dear NinjaTrader Support Team, i am trying to create a custom bars type which includes additional data in the form of a list of custom objects. This seems to work pretty well but i am not able to serialize the custom list with my bars type. So currently i need to put an XmlIgnore in Front of my list to make this work.


            I feel like this might be along the lines of what you are looking for. Please feel free to reach out with any additional questions or concerns.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Hi Emily,
              Thanks for your help. I have one issue that blocks me now - when running the newly created barType, I can see in the logs that bar.count gets much lareger than the amount of bars I have on chart..
              how can I get the "barindex" of the each bar without using bars.count?
              or do you know why does the bar.counts get more bars back than I need?

              Elad.

              Comment


                #8
                Hello Elad,

                Thank you for your reply.

                Please provide a snippet or reduced sample that demonstrates how you are accessing the bar.count so I may better understand and assist you.

                I look forward to your reply.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Hi,
                  I'm loading 50 bars back.
                  Now :
                  Code:
                  At protected override void OnDataPoint(Bars bars, double open, double high, double low, double close,
                  DateTime time, long volume, bool isBar, double bid, double ask)​
                  I'm printing bars.Count.
                  And when I using "Reload all historical data" I'm getting the bar's number is much more than my 50 bars back. and bars.count is hitting 1K and more.

                  Elad.

                  Comment


                    #10
                    Hello Elad,

                    Thank you for your reply.

                    bars.Count should correspond with the number of times your script has called AddBar() for each call of OnDataPoint(). You mentioned you are loading 50 bars back; is this based on the Data Series "Load data based on" parameter? Please provide a screenshot of your Data Series settings for the chart. I am also curious what you see if you right-click your chart and select "Show data box" then right-click your data box and make sure that "Show bar indexes" is checked. When you hover your mouse over the most recent bar on the chart, what bar index is shown?

                    I look forward to your reply.
                    Emily C.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Segwin, 05-07-2018, 02:15 PM
                    14 responses
                    1,789 views
                    0 likes
                    Last Post aligator  
                    Started by Jimmyk, 01-26-2018, 05:19 AM
                    6 responses
                    837 views
                    0 likes
                    Last Post emuns
                    by emuns
                     
                    Started by jxs_xrj, 01-12-2020, 09:49 AM
                    6 responses
                    3,293 views
                    1 like
                    Last Post jgualdronc  
                    Started by Touch-Ups, Today, 10:36 AM
                    0 responses
                    13 views
                    0 likes
                    Last Post Touch-Ups  
                    Started by geddyisodin, 04-25-2024, 05:20 AM
                    11 responses
                    63 views
                    0 likes
                    Last Post halgo_boulder  
                    Working...
                    X