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 variables that are not plots within a Strategy

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

    Access variables that are not plots within a Strategy

    • Following the example in the SampleBoolSeries file downloaded below, let's say that the double ExposedVariable needs to be accessed from within a Strategy.

      How can this be done?

      I am getting an error when declaring: private double SampleBoolSeries.ExposedVariable;
    • I have also changed State==DataLoaded. Can you take a look at the code and amend it so ExposedVariable is accessible? Thanks in advance!
    • namespace NinjaTrader.NinjaScript.Strategies

      {

      public class ExposedVariableTestUnlocked : Strategy

      {



      private double SampleBoolSeries.ExposedVariable;







      protected override void OnStateChange()

      {

      if (State == State.SetDefaults)

      {

      Description = @"Enter the description for your new custom Strategy here.";

      Name = "ExposedVariableTestUnlocked";

      Calculate = Calculate.OnBarClose;

      EntriesPerDirection = 1;

      EntryHandling = EntryHandling.AllEntries;

      IsExitOnSessionCloseStrategy = true;

      ExitOnSessionCloseSeconds = 30;

      IsFillLimitOnTouch = false;

      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;

      OrderFillResolution = OrderFillResolution.Standard;

      Slippage = 0;

      StartBehavior = StartBehavior.WaitUntilFlat;

      TimeInForce = TimeInForce.Day;

      TraceOrders = true;

      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;

      StopTargetHandling = StopTargetHandling.PerEntryExecution;

      BarsRequiredToTrade = 20;

      // Disable this property for performance gains in Strategy Analyzer optimizations

      // See the Help Guide for additional information

      IsInstantiatedOnEachOptimizationIteration = true;

      ExposedVariable = 0;

      }

      else if (State == State.Configure)

      {

      }

      else if (State == State.DataLoaded)

      {

      double SampleBoolSeries;

      }

      }




      protected override void OnBarUpdate()

      {

      if (BarsInProgress != 0)

      return;




      if (CurrentBars[0] < 1)

      return;




      // Set 1

      if (Close[0] != Open[0])

      {

      Print(CurrentBars[0].ToString() + @" Close0 value is = " + Close[0].ToString());

      Print(CurrentBars[0].ToString() + @" ExposedVariable value is = " + SampleBoolSeries.ExposedVariable.ToString());

      }



      }

      }

      }

    #2
    Hello roblogic,

    Thank you for the post.

    I am not certain I understand the scope of what you are trying to do here, the sample you linked to does specifically what you asked as the last print in the strategy.

    Following the example in the SampleBoolSeries file downloaded below, let's say that the double ExposedVariable needs to be accessed from within a Strategy.
    Code:
    Print(SampleBoolSeries().ExposedVariable);
    This is from the strategies code, it is printing the value of ExposedVariable from the indicator. Is this not what you are trying to do?

    The error you are seeing is because private double SampleBoolSeries.ExposedVariable; is not valid, you cannot define an indicator property as a strategy property in this way.



    I look forward to being of further assistance.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks. It works now!.

      So basically to call an exposed variable from an indicator it only requires to call it from OnBarUpdate() There is no need to add anything on state.DataLoaded and state.Configure. Correct?

      Comment


        #4
        Hello roblogic,

        Correct, this is a good point which I can provide some more detail on.

        DataLoaded is only letting us know that data has been loaded, however, if you wanted to access values such as price values or indicators, that is all done past the DataLoaded point in OnBarUpdate or the other data event-driven overrides. You can consider DataLoaded more of a one time step for extra configuration.

        There is also a difference in the syntax you had used that resulted in an error and the syntax in this sample, part of that is how you call the indicator:

        Code:
         SampleBoolSeries()
        Another part is where you call the indicator.

        Because the value is coming from the indicator and that indciator needs to process to produce a value, it could not be set as a property in the way that you had shown and needs to be called later in logic. You can still create variables which are indicators if you do it correctly. The SampleMACrossOver strategy that comes with NinjaTrader is an easy way to see how this is done. That strategy actually does use DataLoaded to setup the indicators but it shows the correct way to do this. The values are later accessed in OnBarUpdate.


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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by f.saeidi, Today, 11:02 AM
        1 response
        1 view
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by geotrades1, Today, 10:02 AM
        4 responses
        11 views
        0 likes
        Last Post geotrades1  
        Started by rajendrasubedi2023, Today, 09:50 AM
        3 responses
        15 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by lorem, Today, 09:18 AM
        2 responses
        11 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by geddyisodin, Today, 05:20 AM
        4 responses
        30 views
        0 likes
        Last Post geddyisodin  
        Working...
        X