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

Extending ChartTrader as an Add-On

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

    Extending ChartTrader as an Add-On

    I was wondering if you could provide an example of just the bare bones skeleton of getting the ChartTrader window reference as an Add-On? Basically I want to extend ChartTrader but not via an indicator, because I want to place orders through it with some new added buttons. Also how can I get a reference to OnBarUpdate() from within this Add-On?

    I wrote it as an indicator initially but I cannot place orders from it that way. I started from the examples you had of customizing it but they were all indicator based and not add-on based.

    Thanks in advance.

    #2
    Hello jalley,

    To append to a chart window without an indicator would require using an Addon and the OnWindowCreated override.
    There is a sample of getting the chart trader control here: https://ninjatrader.com/support/foru...t=7#post752775

    You would do exactly the same process in an addon but rather than using the ChartControl to locate the window you would just use the passed in window from OnWindowCreated.

    chartTrader = window.FindFirst("ChartWindowChartTraderControl") as ChartTrader;

    You can find some examples of modifing the chart trader control here: https://ninjatrader.com/support/foru...ions#post96376

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I did what you said and finished the visual part of the controls for my extended ChartTrader but now I need to be able to place orders.

      What is the best way from an Add-On to get the selected instrument, price (bid and ask) and selected qty?

      How do you intercept OnBarUpdate() or even process tick by tick within an Add-On? I know how to do this from a strategy or indicator but haven't done it before with an Add-On...

      Thanks again!

      Comment


        #4
        Hello jalley,

        Your addon won't be able to just get data from a window, you would need to do a BarsRequest if you need data of some kind. https://ninjatrader.com/support/help...ub=barsrequest

        If you want to use the charts instrument selector you would have to additionally locate that control and then get the selected instrument. You can find an example of working with the instrument selector in the addon sample basic. Once you have found the control using FindFirst you could work with it like shown in the sample: https://ninjatrader.com/support/help...t_overview.htm

        To find different controls in the user interface you would need to use Microsofts tool named Inspect.


        That will allow you to see automation ID's for controls like the chart trader or instrument selector. There are also existing samples for these type of items if you search the forums for the concept you are asking about. https://ninjatrader.com/support/help...ub=barsrequest

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I was able to get my custom ChartTrader working for the first chart I open and only if I start it with a data provider already connected.

          Issues I'm having...

          Any other chart I open it doesn't show the customization.
          If I start not connected to a data provider it shows customization controls fine but after connecting to a data provider it is throwing thread errors trying to remove event handlers etc...
          Throws these thread errors when starting other charts after the first one. I added null checks but that's only a bandage to not crash the system.

          What insight could you give me? Below is sort of the main control section of the code where the thread errors happen. Thank in advance!

          protected override void OnWindowDestroyed(Window w)
          {
          // if(w.IsEnabled) return;
          Cleanup(w);
          }

          protected override void OnWindowCreated(Window window)
          {
          if (window != null)
          {
          window.Dispatcher.InvokeAsync((Action)(() =>
          {
          AddButtonsMethod(window);
          }));
          }
          }

          // Called by TabControl when tab is being removed or window is closed
          public void Cleanup(Window w)
          {
          // Make sure to unsubscribe to the market data subscription
          if (marketData != null)
          marketData.Update -= OnMarketData;
          // Make sure to unsubscribe to the bars request subscription
          if (barsRequest != null)
          {
          barsRequest.Update -= OnBarUpdate;
          barsRequest.Dispose();
          barsRequest = null;
          }
          RemoveWPFControls();
          }

          private bool TabSelected()
          {
          bool tabSelected = false;

          // loop through each tab and see if the tab this indicator is added to is the selected item
          foreach (TabItem tab in chartWindow.MainTabControl.Items)
          if ((tab.Content as Gui.Chart.ChartTab).ChartControl is ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
          tabSelected = true;

          return tabSelected;
          }

          private void TabChangedHandler(object sender, SelectionChangedEventArgs e)
          {
          if (e.AddedItems.Count <= 0)
          return;

          tabItem = e.AddedItems[0] as TabItem;
          if (tabItem == null)
          return;

          chartTab = tabItem.Content as Gui.Chart.ChartTab;
          if (chartTab == null)
          return;

          if (TabSelected())
          InsertWPFControls();
          else
          RemoveWPFControls();
          }
          }

          protected void GetChartTraderParameters()
          {
          if (chartTrader != null)
          {
          instrument = chartTrader.Instrument;
          myAccount = chartTrader.Account;
          selector = chartTrader.FindFirst("ChartTraderControlATMStrate gySelector") as NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector;
          qty = chartTrader.Quantity;
          tickSize = chartTrader.Instrument.MasterInstrument.TickSize;
          }
          else
          {
          chartTrader = currentWindow.FindFirst("ChartWindowChartTraderCon trol") as ChartTrader;
          if (chartTrader != null)
          {
          GetChartTraderParameters();
          }
          }
          }

          // This method is fired on market data events
          protected void OnMarketData(object sender, MarketDataEventArgs e)
          {
          // Do something with market data events
          if ((marketData != null) &&
          (marketData.Last != null))
          {
          price = marketData.Last.Price;
          }
          }
          Last edited by jalley; 08-27-2020, 08:31 PM. Reason: I see it didn't perserve formatting after pasting the code. I added spaces and it still left justifies it. Not sure to fix formatting

          Comment


            #6
            I got a little further and now I'm not getting the same problems but have a new one. All the charts now display my added controls but click events etc, and edit control values are being shared by all charts so if I change one chart, the rest are changing as well and I want them to each be unique.

            Also is there an easy way from an addOn to tie into the chart's onBarUpdate event without having to do a seperate bar request and calculate range etc? I just need to know when the chart starts the plot of the next bar to get previous close. This way it would work with any bar type or time range.

            I fixed most of the past posts by disabling checking of tabcontrol state etc.. was avoiding dynamically inserting wpf controls onto the additional charts. I also added a reference count for the += OnMarketData event handler and check that before trying to remove one that doesn't exist. I'm sure I'm not doing it the right way but at least I'm not seeing errors now when connecting data provider or adding new charts.

            Comment


              #7

              I got a little further and now I'm not getting the same problems but have a new one. All the charts now display my added controls but click events etc, and edit control values are being shared by all charts so if I change one chart, the rest are changing as well and I want them to each be unique.
              It sounds like you may have declared some class level variables or did not do your action per each window instance individually. From the code you provided earlier I can't really tell. If you can provide a updated sample as an attachment instead of pasting it that would be helpful.

              Also is there an easy way from an addOn to tie into the chart's onBarUpdate event without having to do a seperate bar request and calculate range etc? I just need to know when the chart starts the plot of the next bar to get previous close. This way it would work with any bar type or time range.
              This would not be something you can access from the addon, you would generally need to apply an indicator to the chart if you want to work with the charts data/events. Your addon is its own tool and it would have to subscribe to its own data.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                I saw in another post in the forum that you can't place orders from an indicator and that is the goal of my add-on.

                Comment


                  #9
                  Hello jalley,

                  An indicator can place orders, you would have to use the Addon account methods for that.




                  This is the same concept that the addon would otherwise use to submit orders.

                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Perfect! Thank you for your patience!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by gentlebenthebear, Today, 01:30 AM
                    2 responses
                    13 views
                    0 likes
                    Last Post gentlebenthebear  
                    Started by Kaledus, Today, 01:29 PM
                    2 responses
                    8 views
                    0 likes
                    Last Post Kaledus
                    by Kaledus
                     
                    Started by frankthearm, Yesterday, 09:08 AM
                    13 responses
                    46 views
                    0 likes
                    Last Post frankthearm  
                    Started by PaulMohn, Today, 12:36 PM
                    2 responses
                    16 views
                    0 likes
                    Last Post PaulMohn  
                    Started by Conceptzx, 10-11-2022, 06:38 AM
                    2 responses
                    56 views
                    0 likes
                    Last Post PhillT
                    by PhillT
                     
                    Working...
                    X