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

Cannot find ChartTrader account

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

    Cannot find ChartTrader account

    When I instantiate a new chart and add an indicator with the following code, I noticed that accountSelector is null:

    Code:
    else if (State == State.DataLoaded)
    {
        Dispatcher.InvokeAsync(() =>
        {
    
            AccountSelector accountSelector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as AccountSelector;
            ...
        }
    }
    The reason, it seems, is because ChartTrader does not create the WPF elements until its visibility is first set to Visible or Hidden from the UI. Once it is visible or at least hidden, the indicator code above behaves as expected and accountSelector is never null.

    Is there a way to force ChartTrader to create the WPF elements? This would prevent this issue when the indicator is added to the chart before ChartTrader is visible/hidden.


    #2
    Hello Zombie,

    Thank you for your reply.

    Chart Trader doesn't exist in the Chart until it's made Visible or Hidden, so you wouldn't be able to access the AccountSelector until it's turned on. There wouldn't be a supported way to force it to create the WPF elements prior to that. Since the chart itself doesn't really have a concept of an Account, you'd want to add code so you don't check the AccountSelector until ChartTrader is active. You could add logic to print a message to the chart when Chart Trader isn't on to tell the user to turn it on for the indicator to function.

    I've attached an example indicator that adds some buttons when Chart Trader is active and removes them when it isn't.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      thanks, I was thinking about turning ChartTrader visibility on programmatically:I tried to do it before searching for the the account selector, but the search failed even though the ChartTrader became visible.

      Is it possible to change the ChartTrader's visibility to Visible/Hidden and only then search for the WPF elements? If so, could you please provide a code sample? Is it possible to subscribe to changes to ChartTrader's visibility?

      Thanks
      Last edited by Zombie; 01-21-2021, 04:19 PM.

      Comment


        #4
        Hello Zombie,

        Thank you for your reply.

        You'd still have to take the same approach as in the previous example - I've created a modified version that forces ChartTrader to be visible. It then checks to see that ChartControl isn't null, then creates buttons. You'd want to use the same approach - check to see that ChartControl isn't null and then look for the AccountSelector.

        Please let us know if we may be of further assistance to you.

        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Thanks for this updated code, Kate.

          I'm trying to do the following in an Indicator:
          • With or without ChartTrader, discover the currently selected Account and Instrument, and be aware of when either changes
          • Create a set of horizontal lines at various price points with additional annotation and a button, similar to how ChartTrader displays a pending order (Price marker + line + Cross-to-Delete, etc)
          I can put the lines and price markers in place, but I would like an example that might come close to providing a delete button on the line and a small annotation on the line too. Even something that points me in the right direction would be useful.

          Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Hello jeronymite,

            Thank you for your reply.

            It should be noted that the chart does not have a concept of an account when ChartTrader is inactive. Chart Trader would need to be active for you to get the account. The instrument would be available regardless.

            I would suggest taking a look at this labeled lines drawing tool add-on that is publicly available on our User App Share to see how these handle rendering text along with a line:

            This is an expansion of the the NinjaTrader Lines Drawing Tool(s) that adds text boxes to each line that will show prices associated with the endpoint or where the line crosses the price scale. Vertical lines will show time stamps. Your own messages can be added to each text box. New settings can be set […]


            You might even consider using the labeled lines as a starting point, using those to draw the lines and then adding a button. I don't have any specific examples of using custom rendering to create something that mimics that X button for deletion on orders, but it would be possible to create a drawing tool that uses the mouse move and mouse click methods to detect when you're are clicking a certain part, and then treat the click as a button press for X action if the click is within certain coordinates.

            You could also consider adding a WPF button and then try using OnRender to move that button so it is near the line, but I'd probably start with modifying the Labeled Lines drawing tool.

            Please let us know if we may be of further assistance to you.

            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Thanks, Kate. Much appreciated.

              Good point on the Account -- I should have realised that. I'll look at the labelled lines drawing tool add-on.

              An observation on the code in the custom example you provided earlier is that having the ChartTrader visibility command in OnRender caused my AddPlots of HLines to plot without the line (price marker was there). If I moved the ChartCommands to State.DataLoaded instead, all worked as expected.

              If OnRender is executed every time there is a chart update, is the ChartCommands needed there, meaning it is executed every refresh of the chart?

              Thanks.
              Multi-Dimensional Managed Trading
              jeronymite
              NinjaTrader Ecosystem Vendor - Mizpah Software

              Comment


                #8
                Hello jeronymite,

                Thank you for your reply.

                When you tried it in OnRender, did you call the base OnRender to make sure standard plots can still plot?

                Code:
                  // call the base.OnRender() to ensure standard Plots work as designed
                  base.OnRender(chartControl, chartScale);
                The original example didn't have plots so this wasn't necessary in that.

                Since OnRender is only called when the strategy is applied to a chart, and thus can access ChartTrader/ChartControl, that's why the ChartCommands is located in there.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the additional advice, Kate. No, I didn't but I have it working now using a different approach ... almost.

                  I update Plots with values and then call ForceRefresh. I do this via a separate event that I can see updates the values (trace output), however, ForceRefresh does not cause the plots to render. Instead, they only render when OnBarUpdate is called. This is surprising, since OnBarUpdate merely calls the exact same method the update event calls and which I can see has already updated the data, but not refreshed the plots.

                  I'm thinking I may need a different approach. Grateful for your advice on how to do this:
                  • Possibly as many as 40+ HLines on a Chart
                  • Want to implement them as an array of lines, each one using a Value from an array of Values updated from a separate (externally updated by custom event) array of line data
                  • As the separate array of data updates, update the lines
                  • As lines are no longer needed to be displayed, set the relevant element of the separate array of data to zero, which cascades to the Values and thus removes the plot
                  • Require the plotted lines to update ASAP after data changes
                  Currently, I presume the Values array has as many elements as I need and in a loop I update the relevant element to be the same as the separate data array. For example:
                  Code:
                  for (int i = 0; i < arraysize; i++)
                      Values[i][0] = separatearray[i];
                  I update the separatearray via a custom event and that cascades through to the plots. It works ... but does not update on ForceRefresh. I do not invoke OnRender.

                  The requirement can be summarised as: For an array of price data, plot an HLine on the relevant chart for each data point that is not zero as the array of data is updated in real-time.

                  Your thoughts on how to do this efficiently most appreciated.

                  Thanks.
                  Last edited by jeronymite; 03-15-2021, 06:01 PM.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment


                    #10
                    Hello Jeronymite,

                    Thank you for your reply.

                    ForceRefresh simply causes OnRender() to be called, it wouldn't update the plots with new values automatically.

                    ChartControl runs a timed event every 250ms to determine the chart needs to be updated. If it does, the OnRender() method is called. Under normal circumstances, the marker used to call OnRender() will be reset after the following conditions:
                    • OnBarUpdate() event
                    • OnConnectionStatusUpdate() event
                    • User clicks on the chart
                    • Drawing object(s) have been removed from the chart
                    • Strategy enabled/disabled on chart
                    • ChartTrader enabled/disabled
                    In most cases, the conditions listed above should be satisfactory for rendering standard and custom chart objects; however for more advance programming concepts, there may be other situations you run into which would NOT force the chart to refresh (e.g., a user interacting with a custom control ). In these special cases, you can use the ForceRefresh() method to re-queue the render event.

                    I'd perhaps suggest if you want the lines to be rendered as soon as possible to use SharpDX to draw the lines within OnRender(). Are you currently drawing the lines within OnBarUpdate?

                    Thanks in advance; I look forward to assisting you further.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks, Kate.

                      I have a method to update the data and consequently the Values. It is called by OnBarUpdate, and I also call it directly by invoking a custom event. Regardless of using ForceRefresh in that method when the data (and therefore Values) change, there is no refresh until the next price change that causes OnBarUpdate to be invoked as a result. I have confirmed that the data are updated correctly and that Values are updated and that ForceRefresh is called ... but the lines do not change until the next price change and OnBarUpdate is invoked.

                      Your advice as to how to "guarantee" that these updates ensure ForceRefresh will redraw the lines would be most appreciated.

                      Thanks.
                      Multi-Dimensional Managed Trading
                      jeronymite
                      NinjaTrader Ecosystem Vendor - Mizpah Software

                      Comment


                        #12
                        Hello jeronymite,

                        Thank you for your reply.

                        As the chart is optimized on a timer, calling ForceRefresh() will NOT immediately trigger a render event. Calling ForceRefesh() simply re-queues the render event to trigger during the next timed event. In other words, it may take up to 250ms for the render event to function, so you'd want to keep that in mind.

                        Unless you're drawing the lines within OnRender, I wouldn't expect that just calling ForceRefresh() would update the values.

                        You could try removing a dummy drawing object to trigger a render:

                        Draw.Dot(this,"none",false, 0, 0, Brushes.Transparent);
                        RemoveDrawObject("none");

                        If you can provide an example snippet of your code that may be helpful in giving more direction.

                        Thanks in advance; I look forward to assisting you further.
                        Kate W.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks, Kate. That's good advice.

                          I'm aware of the 250ms delay, but in my case, the delay is most definitely until OnBarUpdate and can be many seconds in a slow market.

                          I tried adding the dummy drawing object, but that does not help. I'll send you the code in email (Attention Kate W), as it's a few hundred lines and in a couple of modules.

                          Again, very much appreciate your advice.

                          Thanks.
                          Last edited by jeronymite; 03-17-2021, 03:43 PM. Reason: Tested dummy drawing object
                          Multi-Dimensional Managed Trading
                          jeronymite
                          NinjaTrader Ecosystem Vendor - Mizpah Software

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by bortz, 11-06-2023, 08:04 AM
                          47 responses
                          1,610 views
                          0 likes
                          Last Post aligator  
                          Started by jaybedreamin, Today, 05:56 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post jaybedreamin  
                          Started by DJ888, 04-16-2024, 06:09 PM
                          6 responses
                          19 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by Jon17, Today, 04:33 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post Jon17
                          by Jon17
                           
                          Started by Javierw.ok, Today, 04:12 PM
                          0 responses
                          21 views
                          0 likes
                          Last Post Javierw.ok  
                          Working...
                          X