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

ISeries<T> Can't Handle Custom Objects?

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

    ISeries<T> Can't Handle Custom Objects?

    I think I may have found an issue with ISeries<T>. I am using ISeries<T> with a custom object/class that I pass back to a calling strategy.

    If I DON'T always pass back an object in ISeries<T>, it creates phantom objects. It seems that when ISeries<T> is used with custom objects, it must ALWAYS be set to an instance of the object (or NULL). That is, you can't leave ISeries<T> empty for the current bar.

    Below is sample code for the indicator and calling strategy (full code is attached). I created a simple indicator that draws a blue box at 3am each day. If a box was drawn, it sets the custom ISeries<Box> value for the current bar. If not, ISeries<Box>[0] remains null.

    This will cause the phantom boxes to be drawn. If the one line is uncommented in the code below (where indicated), the phantom images disappear. See attached screenshots for examples.

    Is this a bug or am I doing something wrong in my code?

    INDICATOR CODE
    Code:
        public class BoxIndicator : Indicator
        {
            public class Box
            {
                public Box() : this(0, 0, 0, 0) { }
                public Box(double dblTopPrice, double dblBottomPrice, int intStartIndex, int intEndIndex)
                {
                    TopPrice = dblTopPrice;
                    BottomPrice = dblBottomPrice;
                    StartIndex = intStartIndex;
                    EndIndex = intEndIndex;
                }
    
                public double TopPrice { get; private set; }
                public double BottomPrice { get; private set; }
                public int StartIndex { get; private set; }
                public int EndIndex { get; private set; }
            }
    
            private Series<Box> _sBoxSeries;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Draws a box on the chart";
                    Name = "BoxIndicator";
                    IsOverlay = true;
                    IsSuspendedWhileInactive = true;
                }
                else if (State == State.DataLoaded)
                {
                    _sBoxSeries = new Series<Box>(this);
                }
            }
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 4) return;
    
                DateTime dtNow = Time[0];
                Box objBox = null;
    
                // Draw a box at 3am each day
                if (dtNow.Hour == 3 && dtNow.Minute == 0) objBox = new Indicators.BoxIndicator.Box(Close[0], Open[0], CurrentBar - 3, CurrentBar);
    
                // !!! UNCOMMENT THIS LINE AND IT WORKS PROPERLY !!! _sBoxSeries[0] = new Box();  
               // OR  _sBoxSeries[0] = null;
                if (objBox != null)
                {
                    // We have a box, so output it to series for consumption by other indicators/strategies
                    _sBoxSeries[0] = objBox;
    
                    // If we're sited on a chart, draw the actual box
                    if (ChartControl != null)
                    {
                        Draw.Rectangle(this, "Box" + objBox.EndIndex.ToString(), false, objBox.EndIndex - objBox.StartIndex, objBox.TopPrice, 0, objBox.BottomPrice, Brushes.Blue, Brushes.Blue, 15);
                    }
                }
            }
    
            #region Properties
    
            [Browsable(false), XmlIgnore()]
            public Series<Box> GetBox
            {
                get
                {
                    return _sBoxSeries;
                }
            }
    
            #endregion
        }

    STRATEGY CODE
    Code:
        public class IssueWithCustomISeriesStrat : Strategy
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Reproduces the issue with using custom objects as ISeries<T> value.";
                    Name = "IssueWithCustomISeriesStrat";
                    Calculate = Calculate.OnBarClose;
                    EntriesPerDirection = 1;
                    IsInstantiatedOnEachOptimizationIteration = true;
                }
            }
    
            protected override void OnBarUpdate()
            {
                BoxIndicator.Box objBox = BoxIndicator().GetBox[0];
    
                if (objBox != null)
                {
                    Draw.Rectangle(this, "Box" + objBox.EndIndex.ToString(), false, objBox.EndIndex - objBox.StartIndex, objBox.TopPrice, 0, objBox.BottomPrice, Brushes.Blue, Brushes.Blue, 15);
                }
    
            }
        }
    Attached Files
    Last edited by MarkWise; 03-23-2017, 02:49 PM.

    #2
    It looks like your code.

    Please re-review... http://ninjatrader.com/support/helpG...s/?seriest.htm


    Checking for Valid Values
    It is possible that you may use a Series<T> object but decide not to set a value for a specific bar. However, you should not try to access a Series<T>value that has not been set. Internally, a dummy value does exists, but you want to check to see if it was a valid value that you set before trying to access it for use in your calculations. *Please see IsValidDataPoint() more information.


    Try using IsValidDataPoint instead of a NULL check on the strategy side (should "work again"), or assign it to NULL as in your working code branch.

    Comment


      #3
      I tend to agree with Sledge.

      It will be easier to answer questions about strategies not producing expected results if we could answer as many of the following questions as possible. Could you take a few minutes to review these questions? I am happy to explain further any of the below questions and the rationale behind asking them.


      • Do you see expected results when running the same test environment on a 14 period SMA indicator?
        • By expected results, I mean that the SMA indicator plots to a screen and has a greater frequency than a 50 period SMA
      • Who are you connected to? This is displayed in green on lower left corner of the Control Center window.
      • Are you connected to your data feed provider when running this test?
      • What instrument(s) (and expiry if applicable) have you selected?
      • What Data Series Type have you selected? Example: Tick, Minute, Day
      • What dates are you reviewing on your chart?
      • For our notes: we noticed you are using a single timeframe, single instrument strategy
      • Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
      Jessica P.NinjaTrader Customer Service

      Comment


        #4
        I was able to solve it by initializing the ISeries<> object to null. I was thinking that since this was an object type, the internal "reset state" would be null by default. It seems I'm wrong here.

        But, I'm up and running without issue now.

        Thanks all!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, Today, 11:37 AM
        2 responses
        12 views
        0 likes
        Last Post junkone
        by junkone
         
        Started by frankthearm, Yesterday, 09:08 AM
        12 responses
        43 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        35 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        35 views
        0 likes
        Last Post love2code2trade  
        Working...
        X