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

Access from button-click handler to hosted indicator values: unexpected behaviour

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

    Access from button-click handler to hosted indicator values: unexpected behaviour

    Hello.

    Please, look at attached indicator.

    Indicator:
    - add 2 buttons on chart;
    - hold hosted SMA() indicator (Hosted_Sma = SMA(20)).

    When click on "Hosted_Indicator_[0]" button, it print Hosted_Sma[0] value.
    When click on "Hosted_Indicator_[1]" button, it print Hosted_Sma[1] value.

    Both work not as expected.
    [0]: print wrong value;
    [1]: throw exception.

    (Hosted_Indicator.GetValueAt(CurrentBar) looks working correct)
    Attached Files
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello,

    Thank you for the post.

    When you are working outside of the Event driven context (OnBarUpdate) you would need to use the GetValueAt methods to retrieve data.

    This is commonly seen when using OnRender but is also the same case when using event handlers or other user defined events.



    In this case, rather than using a BarsAgo, you would need to know the Index of the bar and then use GetValueAt to get the Index value.

    In your example, to locate the Index of the CurrentBar, you could do something as simple as storing the CurrentBar to a variable so it can later be used:

    Code:
    private int myCurrentBar = 0;
    protected override void OnBarUpdate()
    {
    	myCurrentBar = CurrentBar;
    }
    later you could print the value:

    Code:
    Print("[0] " + Hosted_Sma.GetValueAt(myCurrentBar));
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank You, Jesse for clarification.

      I see now, that my problems are deeper, than I expect.

      What if I need to set public properties and plot's values of hosted indicator from button-click handler?

      Should I make some method in hosted indicator and call in asynchronous with parameters?
      Last edited by fx.practic; 08-11-2017, 10:54 AM.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        Hello,

        Thank you for the reply.

        For setting NinjaScript properties, you can review the TriggerCustomEvent syntax, this would ensure that the NinjaScript properties line up when you set them.



        Tip: There may be scenarios in which you need to set a Series<T> value outside of one of the core data event methods. In these cases, you can use TriggerCustomEvent() to reliably synchronize the barAgo indexer to the recent current bar being updated.
        Code:
         TriggerCustomEvent(o =>
            {
             Value[0] = 100;
            }, null);


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

        Comment


          #5
          Thanks again.
          And, please, help me to clarify some more details:

          1) Can I read UI elements properties directly from within OnBarUpdate() of indicator (or strategy):
          PHP Code:
          OnBarUpdate()
          {
             
          PlotBrushes[0][0] = My_Button.Forefground
             if( 
          My_Button.Content == "Can Go Short" && Close[0] < Open[0] ) EnterShort(); 

          2) Can I open deals in strategies from within UI event handlers:
          PHP Code:
          My_Button_Click()
          {
             if( 
          My_Button == null ) return; 
             
          EnterLong();

          3) What is the general rule to determine witch indicator's (or strategy's) property (or method) can (and which can't) be accessed directly from within UI event handlers?

          For example, in what way should I think to understand whether I can do this:
          PHP Code:
          My_Button_Click()
          {
             if( 
          My_Button == null ) return; 
             if( 
          Position.MarketPosition != MarketPosition.Flat ) { ExitLong(); }

          Last edited by fx.practic; 08-15-2017, 01:12 PM.
          fx.practic
          NinjaTrader Ecosystem Vendor - fx.practic

          Comment


            #6
            Hello,

            Thank you for the reply.

            Generally, it would be advisable to avoid trying to access UI Elements from code, most commonly you would set up the UI Element based on Properties of the script which your other code could also access. For example:

            Code:
            private Brush myBrushProperty = Brushes.Red;
            
            My_Button.Forefground = myBrushProperty; 
            
            PlotBrushes[0][0] =myBrushProperty;
            If you are toggling the Text of a button to turn something on or off, this would be a good case for a boolean value instead of trying to compare strings. In the Click event, you could just set a variable to true or false:

            Code:
            private bool canGoShort;
            
            voild MyButtonEventHander(...)
            {
                canGoShort = !canGoShort; //this would toggle the bool
            }
            later in your code, you would use the bool instead of the button:

            Code:
            if( canGoShort && Close[0] < Open[0] ) EnterShort();

            If you instead need multiple states such as Red, Green,Yellow etc, you could just make an Enum property: http://ninjatrader.com/support/forum...ead.php?t=3420


            For the second question, you can utilize the button event to submit orders or do other NinjaScript actions, please keep in mind you would likely need to use TriggerCustomEvent to ensure Price data or other properties you use are correct.


            Regarding the last question, As a general rule, I would suggest to try and always use variables instead of trying to access UI items. There are cases where you need to access a UI item to get a value like accessing the ChartTrader. In that case, you did not create the ChartTrader so you would need to access it directly. Because you created your own buttons there's really no reason to try and access the button instance directly. It would be best to not access the button directly and not have to worry about threading errors.


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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by timmbbo, 07-05-2023, 10:21 PM
            4 responses
            158 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by tkaboris, Today, 08:01 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by Lumbeezl, 01-11-2022, 06:50 PM
            31 responses
            817 views
            1 like
            Last Post NinjaTrader_Adrian  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            5 responses
            15 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by swestendorf, Today, 11:14 AM
            2 responses
            6 views
            0 likes
            Last Post NinjaTrader_Kimberly  
            Working...
            X