Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars is null when using custom indicator inside custom strategy.

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

    Bars is null when using custom indicator inside custom strategy.

    I am attempting to format my code so that I can use my custom indicators inside strategies. When I do this, some variables are null.

    Indicator:

    Code:
    namespace NinjaTrader.NinjaScript.Indicators.Nic
    {
    public class IntradayLevels : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Nic Intraday Levels";
    Name = "Intraday Levels";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    base.OnBarUpdate();
    
    if (Bars == null)
    return;
    Print("Have Bars");
    }
    
    
    }
    }
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    IntradayLevels[] _IntradayLevels;
    
    public IntradayLevels GetIntradayLevels(ISeries<double> input)
    {
    if (_IntradayLevels != null)
    for (int idx = 0; idx < _IntradayLevels.Length; idx++)
    if (_IntradayLevels[idx] != null && _IntradayLevels[idx].EqualsInput(Input))
    return _IntradayLevels[idx];
    return CacheIndicator<IntradayLevels>(new IntradayLevels(), input, ref _IntradayLevels);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    IntradayLevels[] _IntradayLevels;
    public IntradayLevels GetIntradayLevels()
    {
    return indicator.GetIntradayLevels(Input);
    }
    }
    }​

    Strategy :

    Code:
    namespace NinjaTrader.NinjaScript.Strategies.Nic
    {
    public class IntradayLevelsStrat : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Nic IntradayLevelsStrat";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 1;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtd;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 2;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    Levels = GetIntradayLevels();
    }
    else if (State == State.DataLoaded)
    {
    AddChartIndicator(Levels);
    }
    }
    }
    }
    My Questions:
    1. Why is Indicator.Bars null when I load the strategy?
    2. Do I need to include the boilerplate "Get Indicator" caching code? Can I just new() an instance?
    3. What variables are available and not available ?
    4. Is there another way to get the bars property?

    #2
    Originally posted by nicthe View Post
    public class IntradayLevels : Indicator
    {
    [snip]​
    protected override void OnBarUpdate()
    {
    base.OnBarUpdate();
    }
    }
    Calling base.OnBarUpdate is not only not necessary,
    but it is arguably wrong -- it is a callback -- specifically
    designed to be overridden and customized for each
    indicator.

    I mean, not a single callback in any factory supplied
    indicator calls its base method, so why are you
    calling it here?

    Just my 2˘.


    Comment


      #3
      Here is my approach. I create a new instance, add my data bars to it and then call the AddChartIndicator function.

      GitHub is where people build software. More than 150 million people use GitHub to discover, fork, and contribute to over 420 million projects.

      Comment


        #4
        > Calling base.OnBarUpdate is not only not necessary, but it is arguably wrong -- it is a callback

        This is best practice of c#. ​We need to make sure abstractions handle events. The habit of picking and choosing which idioms to choose will lead to errors. That said, it did not cause my issue.

        > Here is my approach. I create a new instance, add my data bars to it and then call the AddChartIndicator function

        Yes. I ended up with this as well. In my Indicator I added an Init method where I pass in the strategy base and delayed initialization until after data is loaded where I switched between the two NinjaBase variables.

        Comment


          #5
          Originally posted by nicthe View Post
          > Calling base.OnBarUpdate is not only not necessary, but it is arguably wrong -- it is a callback

          This is best practice of c#.
          I don't think it is considered best practice
          when using the NinjaScript C# framework.
          I'm pointing out that it's 100% useless and
          wastes time.

          That said, I agree it's not the cause of your
          problem.

          Comment


            #6
            Hello nicthe,

            This would be unexpected.

            May I test an export of your script?

            To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
            1. Click Tools -> Export -> NinjaScript Add-on...
            2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
            3. Click the 'Export' button
            4. Enter the script name in the value for 'File name:'
            5. Choose a save location -> click Save
            6. Click OK to clear the export location message
            By default your exported file will be in the following location:
            • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
            Below is a link to the help guide on Exporting NinjaScripts.
            http://ninjatrader.com/support/helpG...nt8/export.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello WaleeTheRobot,

              This would be unexpected.

              May I test an export of your script?

              To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
              1. Click Tools -> Export -> NinjaScript Add-on...
              2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
              3. Click the 'Export' button
              4. Enter the script name in the value for 'File name:'
              5. Choose a save location -> click Save
              6. Click OK to clear the export location message
              By default your exported file will be in the following location:
              • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
              Below is a link to the help guide on Exporting NinjaScripts.
              http://ninjatrader.com/support/helpG...nt8/export.htm
              I think you meant nicthe.

              Comment


                #8
                WaleeTheRobot,

                My bad. Thanks! :-)
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CaptainJack, 05-29-2026, 05:09 AM
                0 responses
                217 views
                0 likes
                Last Post CaptainJack  
                Started by CaptainJack, 05-29-2026, 12:02 AM
                0 responses
                133 views
                0 likes
                Last Post CaptainJack  
                Started by charlesugo_1, 05-26-2026, 05:03 PM
                0 responses
                147 views
                0 likes
                Last Post charlesugo_1  
                Started by DannyP96, 05-18-2026, 02:38 PM
                1 response
                233 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by CarlTrading, 05-11-2026, 05:56 AM
                0 responses
                192 views
                0 likes
                Last Post CarlTrading  
                Working...
                X