Announcement

Collapse
No announcement yet.

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.


    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.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        51 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        128 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        69 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X