Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Screenshotting from a Strategy

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

    Screenshotting from a Strategy

    Would there be any problem if I don't use Dispatch.Invoke() while screenshotting?

    I am using the code from this example created by NinjaTrader Support - Dave



    or is there a built-in function now?

    It works great except for the fact that some of my newly drawn elements don't appear and I think that is because it is using Invoke

    #2
    Hello NinjaCustomer,

    Because the UI and NinjaScripts are on different threads, it is necessary to use the ChartControl.Dispatcher.InvokeAsync() to invoke into the UI thread to take a picture of the UI from the script without causing one or both threads to lock up.

    Invoke() and BeginInvoke() can both cause thread lockups.

    Below is a link to an example.
    https://ninjatrader.com/support/foru...95#post1050595

    Unfortunately, there is no built-in functionality for saving a screenshot from a script.

    I will submit a feature request on your behalf for the development team to consider this.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I can leave the State == State.Configure assignment as

      Dispatcher.BeginInvoke(new Action(() => { chart = Window.GetWindow(ChartControl) as Chart; })); ? and just change to the ChartControl.Dispatcher.InvokeAsync() for the screenshotting?

      Comment


        #4
        Hello NinjaCustomer,

        Yes, BeginInvoke should always be InvokeAsync().

        However, does the example I have provided you work as expected?
        If so, I would recommend modeling your code after the working example.
        If not, let me know and I will take a look to see if I am able to reproduce.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi, the code that performs the actual screenshot looks the same like I have the code in my Strategy (taken from the Dave version I linked to) - so that works fine, on BeginInvoke - I was using it for the last 2 days without problems apart from what I mentioned about arrows not having been drawn on the chart yet. I have now changed to invoke async but I haven't had time to test that yet.

          So I need to add this WPF Controls code ? or if I haven't had problems yet then I might not need it? I am only taking screenshots from a Strategy, not Indicator - don't know if that matters.

          Comment


            #6
            Hello NinjaCustomer,

            The wpf buttons added are to make it easy to take the screenshot when clicking the button, but are not required. This does make it easy to ensure that all the desired objects are being included with the screenshot.

            If you are taking the screenshot after calling a draw method, you may want to wait until after the next render pass so the object is drawn first. (Like setting a flag in OnRender() to take the screenshot on the next tick in OBU/OMD)
            Last edited by NinjaTrader_ChelseaB; 04-16-2019, 09:37 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              would OnRender() make a difference to what I normally do? I force refresh the chart first before saving the screenshot. My screenshotting is to save the chart state after entering or exiting a trade so that I can debug any problems later

              Comment


                #8
                Hello NinjaCustomer,

                Calling ForceRefresh() sets a flag that the chart should be rendered on the next render pass. It doesn't cause the chart to immediately re-render.
                https://ninjatrader.com/support/help...rcerefresh.htm

                Even if it did, if you are calling ForceRefresh() are you waiting until after the chart has been re-rendered before taking the screenshot?
                Rendering happens asynchronously in another thread and takes a small amount of time.
                Last edited by NinjaTrader_ChelseaB; 04-16-2019, 08:47 AM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  ok, how about this...

                  1. make a trade
                  2. draw the arrows
                  3. set a flag
                  4. have onrender check the flag
                  5. when set then call invokeasync from there to make the screenshot

                  ?

                  Comment


                    #10
                    Hello NinjaCustomer,

                    My recommendation would be not to add any code to OnRender() that is not absolutely necessary.
                    OnRender() is called a lot, and you don't want any code in here that could cause delays.
                    I am still recommending what I recommend in post #6, that you set the flag in OnRender() and then take the screenshot on the next pass of OnBarUpdate or OnMarketData.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      ok got it...

                      1. set flag A after drawing arrows in OnBarUpdate() which is where I draw my arrows and call screenshot right now
                      2. OnRender: if flag A is set, set flag B
                      3. in OnMarketData check flag B and InvokeAsync if set

                      I assume OnMarketData happens at least as often as OnBarUpdate

                      Comment


                        #12
                        Hello NinjaCustomer,

                        Yes, OnMarketData triggers for every received market update (so updates more often than OnBarUpdate), but triggers immediately after OnBarUpdate.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          thanks, one more question here

                          so what exactly does the screenshot capture?

                          The current tab of the window?

                          or the current tab of the whole NT8 workspace?

                          Comment


                            #14
                            Hello NinjaCustomer,

                            The example I have provided you takes a picture of the chart tab contents.

                            Is this performing differently when you have tested it?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              it is taking a picture of the active tab, but since I was not using other windows (had them open but didn't switch focus to them - I was just switching between tabs of the same window) I am unsure if it takes a screenshot of the chart of active tab of the same window that the chart with the strategy is running in, or if it just takes a screenshot of the active tab of the focused window of the workspace, that is what I'm trying to clarify. Because if it is just the active tab of the strategy window then I can simply move the tab I like to switch to out to a separate window so I can view and still be confident that every time I take a screenshot it will be of the strategy chart because I would just leave that chart as the active tab of its window.

                              Hopefully I'm not being too confusing here.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              56 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              133 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              73 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X