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

Referencing an exposed variable on FirstTickOfBar in a real time strategy

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

    Referencing an exposed variable on FirstTickOfBar in a real time strategy

    Hello!
    I want to reference an exposed variable (not a DataSeries) of a custom indicator in a real time strategy (COBC = false) at the close of each bar (FirstTickOfBar). Since it's a variable and not a DataSeries that is referenced, the indicator output can't be shifted with a bars ago index of [1]. How would I correctly reference this variable in the strategy to return an output value for the close of each bar? Should the current or the previous variable value be exposed?

    When a strategy calls an indicator at FirstTickOfBar, will the referenced indicator output be returned before or after the OnBarUpdate() method of the indicator is processed for that first tick? Does the Update() method in the "getter" affect this behaviour somehow?

    Best Regards,
    Poseidon


    Indicator:
    Code:
            // Variables
            private double exposedVariable;
            
            protected override void OnBarUpdate()
            {
                exposedVariable = xyz;
            }
            
            //Properties
            [Browsable(false)]
            [XmlIgnore()]
            public double ExposedVariable
            {
                get { 
                         Update();         // Ensure our exposed variable is up-to-date.
                         return exposedVariable;
                      }
            }
    Strategy:

    Code:
           protected override void Initialize()
            {
                CalculateOnBarClose    = false;
            }
            
            protected override void OnBarUpdate()
            {
                if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat)
                {
                    if ( myIndicator.exposedVariable > Close[0] );
                    EnterLong("long entry");
                }
            }

    #2
    Poseidon, including Update() in the 'getter' would ensure the variable is updated before the OnBarUpdate() is processed - you would want to expose the previous value then to get the value of the last closed bar.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks for your prompt reply. Do I understand your answer correctly if I asign the expose variable as in this code example...

      Code:
              protected override void OnBarUpdate()
              {
                     xyz = (do some calculations here); // Calculate the current variable value
      
                    exposedVariable = xyz;    // Asign the value to the exposed variable
              }
      ....but NOT as in this example?
      Code:
              protected override void OnBarUpdate()
              {
                  exposedVariable = xyz;    // Asign the [COLOR=Red]previous[/COLOR] variable value to the exposed varaible before we calculate the current value
             
                  xyz = (do some calculations here); // Calculate the current variable value
      
              }
      /Best Regards
      Poseidon

      Comment


        #4
        Poseidon, this will likely not work as it's all happening in one bar update call running on each tick. You could sequence the value updates for example via CurrentBar checks, or it's likely easier altogether to work with a series here that you .Set as needed with your values.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Do you think that neither of my previous two code examples will work, although the strategy only calls the indicator at FirstTickOfBar? If the first example doesn't work, shouldn't the secons example be fine? (The reason for my inital question is that I want to expose a custom struct from an indicator. That's why I don't use a Series variable.)
          Last edited by poseidon_sthlm; 04-20-2011, 08:12 AM.

          Comment


            #6
            Poseidon, my speculation here without a test scenario would be that it would not matter, as both are happening in the same bar update - is your struct value / trigger changing that frequently? To be sure you would need print the exposed value at the time you're trying to work with it in your strategy and then ensure it's matching what you would need for correct signals taking place.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by betsuni_123, Today, 04:20 PM
            0 responses
            2 views
            0 likes
            Last Post betsuni_123  
            Started by Aquila_Welokk, 04-29-2024, 01:14 PM
            2 responses
            22 views
            0 likes
            Last Post Aquila_Welokk  
            Started by samish18, 04-17-2024, 08:57 AM
            22 responses
            107 views
            0 likes
            Last Post samish18  
            Started by Ashkam, 04-29-2024, 09:28 AM
            2 responses
            26 views
            0 likes
            Last Post Ashkam
            by Ashkam
             
            Started by Felix Reichert, 04-26-2024, 02:12 PM
            6 responses
            41 views
            0 likes
            Last Post Felix Reichert  
            Working...
            X