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

My indicator is not working. Please help.

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

    My indicator is not working. Please help.

    Hi,

    I'm trying to create a very simple chart(indicator) that shows the movement of spread price between 2 contracts. My code is below:

    Code:
    public class SpreadPrice : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "SpreadPrice";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.Orange, "Spread");
                    Symbol = string.Empty;
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Symbol);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[1] < 10)
                    return;
    
                Value[0] = Closes[0][0] - Closes[1][0];
            }
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Spread
            {
                get { return Values[0]; }
            }
    
            [NinjaScriptProperty]
            [Display(Name = "Symbol to Compare", Order = 1, GroupName = "Parameters")]
            public string Symbol
            { get; set; }
            #endregion
    For example, I enter CL 09-20 as the symbol on CL 08-20 chart, to compare the contango.

    However, what I got is a zero line (screenshot attached).

    Would you please help me understand why and how to fix it?

    Thank you!

    #2
    Hello HiddenPhilosopher,

    As a heads up, using dynamic variables in AddDataSeries() is not supported.

    From the help guide:
    "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."


    That said, have you printed the information to the output window to understand the behavior?


    Have you printed the Instruments[0].FullName, Instruments[1].FullName, as well as the Closes[0][0] and Closes [1][0] value?

    What printed to the output window?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello HiddenPhilosopher,

      As a heads up, using dynamic variables in AddDataSeries() is not supported.

      From the help guide:
      "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."


      That said, have you printed the information to the output window to understand the behavior?


      Have you printed the Instruments[0].FullName, Instruments[1].FullName, as well as the Closes[0][0] and Closes [1][0] value?

      What printed to the output window?
      Hi,

      I tried to print out as you said, and got below:

      CL 08-20 & CL 09-20
      40.4 & 40.4

      it looks like it returns the instrument, but it it didn't return the right price.

      Also, when you say dynamic variables in AddDataSeries() is not supported, I actually duplicated this from another user shared app. I have attached the app and its screenshot for your review.

      If it really can't work, is there an alternative way for me to calcualte the spread prices?

      Thank you!
      Attached Files

      Comment


        #4
        Hello HiddenPhilosopher,

        The script you have provided uses unsupported code that is not supported by the NinjaTrader Support team.

        It looks like the values are the same. Have you checked this on a chart?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello HiddenPhilosopher,

          The script you have provided uses unsupported code that is not supported by the NinjaTrader Support team.

          It looks like the values are the same. Have you checked this on a chart?
          Yes. On the chart, the value is for CL 08-20, which is the primary series.

          Anyway, my point is that, is there a way to show spread price between 2 instruments on chart?

          The spread indicator I attached was provided by the customer service in old threads.
          Last edited by HiddenPhilosopher; 07-05-2020, 10:03 PM.

          Comment


            #6
            Hello HiddenPhilosopher

            I am able to use the spread indicators found here:
            Plots the Spread of two instruments The first instrument is the primary chart data series. The secondary instrument is specified by the parameter "Symbol2". The Parameters "Qty1" and "Qty2" are multipliers which are applied to each instrument&apos;s price before adding the two; default values are "Qty1=1" and "Qty2=&dash;1" which will simply plot the difference between [&#8230;]


            And here:
            This is a conversion of the Spread Indicator with Candlesticks. Please contact the original author for any questions or comments.


            Do note that if you are looking ahead at a rollover date that has not rolled over you will need to use the Do not merge policy.


            So I can confirm that this is possible.

            Below is a link to a video.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello HiddenPhilosopher

              I am able to use the spread indicators found here:
              Plots the Spread of two instruments The first instrument is the primary chart data series. The secondary instrument is specified by the parameter "Symbol2". The Parameters "Qty1" and "Qty2" are multipliers which are applied to each instrument&apos;s price before adding the two; default values are "Qty1=1" and "Qty2=&dash;1" which will simply plot the difference between [&#8230;]


              And here:
              This is a conversion of the Spread Indicator with Candlesticks. Please contact the original author for any questions or comments.


              Do note that if you are looking ahead at a rollover date that has not rolled over you will need to use the Do not merge policy.


              So I can confirm that this is possible.

              Below is a link to a video.
              https://drive.google.com/file/d/1dfF...w?usp=drivesdk
              Thank you! The spread indicator does exactly what I need.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Rapine Heihei, Today, 08:19 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by Rapine Heihei, Today, 08:25 PM
              0 responses
              6 views
              0 likes
              Last Post Rapine Heihei  
              Started by f.saeidi, Today, 08:01 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by Rapine Heihei, Today, 07:51 PM
              0 responses
              8 views
              0 likes
              Last Post Rapine Heihei  
              Started by frslvr, 04-11-2024, 07:26 AM
              5 responses
              98 views
              1 like
              Last Post caryc123  
              Working...
              X