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

Async call to get status of a radio button

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

    Async call to get status of a radio button

    Hello,

    I am trying to use the custom sidebar example fetched here in the forum to develop my own indicator, which should take the state of a radio button as input. I have two radio buttons, "Long Position" and "Short position".

    They are created in code and load well. In order to get the checked status of these radio buttons, the code of the logic of the indicator however needs to use the dispatcher, which I can't seem to get to work.

    I wrote the async call like this (it lives in the OnBarUpdate event of the indicator):

    int longShortSwapper = 0; // local variable to hold 1 or -1 for long/short math processing
    bool? longOrShort = false; // local variable to hold the status of the radio button for later checks

    // check if the current object is already on the calling thread
    if (radioLong != null)
    {
    if (radioLong.Dispatcher.CheckAccess())
    {
    // execute action directly
    longOrShort = radioLong.IsChecked;
    }
    // otherwise run the action from the thread that created the object
    else
    {
    // dispatch action to calling thread
    Dispatcher.InvokeAsync(new Action(() => {
    System.Diagnostics.Debugger.Break();
    longOrShort = radioLong.IsChecked;
    }));
    }
    }


    I am sorry if this is too basic, but I am unable to solve this without help...

    Thank you,

    Libor
    Last edited by liborc; 04-18-2017, 11:56 AM.

    #2
    Hello liborc,

    Are you receiving an error message?

    What is the error message?

    If you use the Chart's dispatcher (similar to the CustomSidePanelExample) do you still get an error?
    Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've


    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    }));
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,

      I actually used that very same sample code to start my indicator. I added only the code necessary for the creation of the radio buttons (added 2 grid rows and the buttons).

      The controls as well as the rest of the code load with no error, but the OnBarUpdate event code I wrote just does not get the value of the radioLong radio button.

      My full OnBarUpdate code is very short:

      /// The radio button status code lives here
      protected override void OnBarUpdate()


      {

      bool? longOrShort = false; // local variable to hold the status of the radio button for later checks

      // check if the current object is already on the calling thread
      if (radioLong != null)
      {
      if (ChartControl.Dispatcher.CheckAccess())
      {
      // execute action directly
      longOrShort = radioLong.IsChecked;
      }
      // otherwise run the action from the thread that created the object
      else
      {
      // dispatch action to calling thread
      ChartControl.Dispatcher.InvokeAsync(new Action(() => {
      System.Diagnostics.Debugger.Break();
      longOrShort = radioLong.IsChecked;
      }));
      }
      }

      Print("longOrShort: " + longOrShort.ToString());

      }


      It just does not pick up the value of radioLong at all.

      I am attaching the rest of the code, in case you have the time to have a look at it. I highlighted all my changes to your original code so it should be easy to read.

      Many thanks again for all your help, here and in other threads.

      Libor
      Attached Files

      Comment


        #4
        Hello liborc,

        As soon as I commented out the 'System.Diagnostics.Debugger.Break();' line which does not work in NinjaTrader, and then moved the print to action block of the dispatch so the print is printed after the bool is set, it worked without issue.

        Are you expecting the print outside of the dispatch call to be called after the dispatch call? (It won't, these are actions happening on two different threads)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          thank you so much. The help here, from you and everyone else, is just outstanding. It is a great customer service - most competent and efficient.

          (just to explain: I debug in Visual Studio and there, the Debugger.Break statement is necessary as there is otherwise no way I know of to set up breakpoints to debug the async calls. Also, I used a private variable instead of local to store the result of the property read and it works as expected now.)

          Best,

          Libor

          Comment


            #6
            Hi Libor,

            Below is a link to a video showing how to use break points in VS.
            Demonstrates how to add breakpoints with VS.1) Open project in VS from button on NinjaScript Editor toolbar2) Enable Debugging then recompile in NinjaScript Editor3) In VS click Debug -> Attach to process -> NinjaTrader.exe4) Add breakpoint to script5) Run script
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NRITV, Today, 01:15 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by maybeimnotrader, Yesterday, 05:46 PM
            5 responses
            24 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by quantismo, Yesterday, 05:13 PM
            2 responses
            16 views
            0 likes
            Last Post quantismo  
            Started by frankthearm, Today, 09:08 AM
            6 responses
            27 views
            0 likes
            Last Post frankthearm  
            Started by adeelshahzad, Today, 03:54 AM
            5 responses
            33 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X