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

Chart locks up when trying to call the following

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

    Chart locks up when trying to call the following

    When calling this method - sometimes it will fire correctly and other times it freezes the entire chart. When I try to exit ninjatrader it says I am in a order - but nothing is in control center or on the chart (since it is frozen)

    Order order = Acct.SelectedAccount.CreateOrder(inst, OrderAction.Buy, OrderType.Market, TimeInForce.Gtc, 1, 0, 0, string.Empty, "Entry", null);
    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy("test-atm", order);

    #2
    Lots of things to check here -

    What is "inst" set to? Try replacing "inst" with NinjaTrader.Cbi.Instrument.GetInstrument("ES 06-23").
    Is Acct set correctly? Try replacing that with Account.All.FirstOrDefault(a => a.Name = "ACCOUNTNAMEHERE");
    Have you tried making it a day order not a Gtc order?
    Is "test-atm" a defined ATM strategy template name and that is spelled exactly right? Try making a new one and then use the new one's name.
    Have you checked your Documents -> NinjaTrader 8 -> trace file to see what it shows when you do this?

    Maybe check those things to start with and let's see what remains.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hello WHICKED,

      Thanks for your post.

      QuantKey_Bruce has made some great points regarding items to check in the script.

      You could consider creating a very simple test script to see what exactly might be occurring in the script when you see the behavior occur. Create a simple test script that calls this line of code.

      Ensure that "inst" is set to an instrument name. You could try using NinjaTrader.Cbi.Instrument.GetInstrument("ES 06-23") instead of "inst" to see if that is causing the behavior.

      Make sure that "Acct" is set to the account you want to submit the order to.

      See the sample code on this help guide page about Account: https://ninjatrader.com/support/help...ount_class.htm

      Do you have an ATM Strategy template created called "test-atm" that is set to use the same quantity as the quantity argument passed into CreateOrder() and has the appropriate number of stops/targets defined?

      Are there any error messages occurring in the Log tab of the Control Center when the behavior occurs? If so, what exactly do they report.

      I have attached a simple strategy called StartAtmStrategyTestIndi that you could view and test on your end. The indicator uses an ATM Strategy template named "AtmStrategyTemplate". I also attached a screenshot showing what the template might look like so you could set it up on your end.

      Let us know if I may assist further.
      Attached Files
      Last edited by NinjaTrader_BrandonH; 06-08-2023, 09:45 AM.
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        "sometimes it will fire correctly and other times it freezes the entire chart"

        it works, it creates the order and will set the assigned ATM. BUT sometimes it will 'create' the order on the instrument and then it will hang the chart.

        attaching trace and log file.
        Last edited by NinjaTrader_BrandonH; 06-05-2023, 07:02 AM.

        Comment


          #5
          It properly uses the instrument name and does not matter what ATM I create or use it will behave the same way.

          Live market or in playback the behavior is the same. Will work - but eventually will hang - and will hang after executing a trade (which is a HORRIBLE bug)

          Comment


            #6
            Hello WHICKED,

            Thanks for your notes.

            I have removed the Log and Trace files you shared from the post because Log and Trace files contain sensitive information that should not be shared publicly.

            That said, I am unable to reproduce the behavior when testing the simple test strategy I attached in post # 3.

            Please try reproducing the behavior using the simple test script I shared in post # 3 while using the Playback connection.

            If you are able to reproduce the behavior with the test script I shared, send me the exact steps, settings, and ATM Strategy template you used to produce the behavior so I may try to reproduce it on my end.

            If you are not able to reproduce the behavior with the test script, you would need to reduce your script by commenting out code and adding debugging prints to determine what exact line(s) of code in your logic are causing the behavior to occur.

            To see if the behavior is specifically caused by calling that line of code, you could create a very simple test script similar to the one I shared that calls that line of code. Note that when working with any issue, it is important to take steps to isolate the issue so that the exact line causing the behavior can be found. This is a process of elimination and a process of debugging by adding prints.

            If the behavior doesn't persist in a simple test script, it is likely something else in your logic that is causing the behavior.

            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              would there be best practices to get the Account Info and ATM strategy that has been selected in Chart Trader and passing that to the sample script that you provided?
              Last edited by WHICKED; 06-05-2023, 09:53 AM.

              Comment


                #8
                You could try the idea posted in this thread: https://forum.ninjatrader.com/forum/...n-chart-trader. It retrieves the connection, but it needs the account to do that so by looking at that example you'll see how to retrieve the account. Retrieving the strategy should be possible also and similar.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Hello WHICKED,

                  Thanks for your notes.

                  The forum thread QuantKey_Bruce shared could be viewed. In the forum thread my colleague Chris explains how the Chart Trader selected account object can be accessed in NinjaScript.

                  Please see this forum post from my colleague Jesse on the subject of getting information from the Chart Trader's ATM Strategy Selector based on automation ID:
                  https://ninjatrader.com/support/foru...775#post752775
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    I modified the script to somewhat align with what I am trying to do, running this script it will lock up even though the logic is from what you are stating.
                    Attached Files
                    Last edited by WHICKED; 06-07-2023, 02:07 PM.

                    Comment


                      #11
                      So taking a quick look you're locking (Account.All) twice on every OnBarUpdate... and invoking the chartcontrol dispatcher as well... even though you're only doing this in realtime because you're early-exiting on State.Historical... how about this... you want to read which account is selected by the control. So, start a timer thread that's a dispatcher timer that runs on the chart control's thread so you don't have to invoke ever, and only in that thread that runs, say, once a second, check the account dropdown and update the acct variable and the position. Then, in OnBarUpdate, you can just refer to the (already known in advance) account and positions by their variables. You should be able in this way to eliminate 100% of the invokes and 100% of the locks. That should make diagnosing any remaining issues simpler because you've eliminated the invoking and locking from the mix. Also, in this way, you can structurally be sure that the ONLY thing you're doing in the chart control's thread is reading the controls to update those variables, and the ORDERS are all placed in the calculation thread (in OnBarUpdate or events). I don't know that these suggestions will fix it, but they're what I would do, in order to clean up the structure in a way that would make any remaining issues more clear.
                      Bruce DeVault
                      QuantKey Trading Vendor Services
                      NinjaTrader Ecosystem Vendor - QuantKey

                      Comment


                        #12
                        Hello WHICKED,

                        Thanks for your notes.

                        QuantKey_Bruce has made some great points in their comments.

                        The freezing behavior you are getting is due to using BeginInvoke or Invoke in your script and not InvokeAsync. You can only use InvokeAsync in NinjaScript, not BeginInvoke/Invoke.

                        Further, you should use the Dispatcher for the object you want to use threading with instead of just using Dispatcher. Dispatcher is using the scripts dispatcher, not the charts.

                        For example, this.Dispatcher.BeginInvoke is invalid. Instead it should be:

                        if (ChartControl != null)
                        ChartControl.Dispatcher.InvokeAsync

                        See this help guide page for more information about Dispatcher: https://ninjatrader.com/support/help...patcher​
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          I also want to mention that the above atm strat that I attached crashes horribly if you ru it on multiple instruments at a time as well - I did implement the Timer class and that seems to work okay.

                          It was the Timer class you were referring to? or is there a different Dispatch esc timer that you are referring to?

                          Comment


                            #14
                            WHICKED Take a look at @BarTimer.cs to see how to use DispatcherTimer. NinjaTrader_BrandonH is absolutely right about BeginInvoke and I didn't catch that (as soon as I saw the invoke my mind was focused on getting rid of it, not what kind it was), but the reason I suggested using a dispatcher timer is so you would not have to invoke at all (once the timer is set up to dispatch from the right thread). It's a risk-reduction strategy - locks and invokes are where these sorts of failures happen, so I was suggesting a way to accomplish your goals without regularly having to invoke at all and without using any locks. In this way, it both makes it less likely for a deadlock and also makes it easier to diagnose any issues that remain once these structural considerations have been dealt with.
                            Bruce DeVault
                            QuantKey Trading Vendor Services
                            NinjaTrader Ecosystem Vendor - QuantKey

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bmartz, 03-12-2024, 06:12 AM
                            5 responses
                            32 views
                            0 likes
                            Last Post NinjaTrader_Zachary  
                            Started by Aviram Y, Today, 05:29 AM
                            4 responses
                            13 views
                            0 likes
                            Last Post Aviram Y  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            3 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X