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

SMA with custome data series [INPUT] assembly missing

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

    SMA with custome data series [INPUT] assembly missing

    Hello.

    I am creating SMA with formulated data series. I encounter error compiling below for [INPUT].with error assembly reference missing.
    My version is 8.0.26 I cant find Ninja.Trader.Gui.Tools.dll and assuming this is the reason for the error.
    I had my NT installed newly. How can fix?


    Code:
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.Strategies;
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class CustomSMAIndicator : Indicator
    {
    [Input]
    public double GoldPriceMultiplier = 2.0; // Multiplier for gold price
    
    [Input]
    public string GoldInstrument = "GC 12-23"; // Gold instrument
    
    [Input]
    public string SilverInstrument = "SI 12-23"; // Silver instrument
    
    [Input]
    public double SmaPeriod = 14; // SMA period
    
    private SMA goldSMA;
    private SMA silverSMA;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Custom SMA Indicator";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Tools.ScaleJustification.Right;
    IsSuspendedWhileInactive = true;
    BarsRequiredToPlot = 2;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(GoldInstrument, BarsPeriodType.Day, 1);
    AddDataSeries(SilverInstrument, BarsPeriodType.Day, 1);
    
    goldSMA = SMA(GoldInstrument, (int)SmaPeriod);
    silverSMA = SMA(SilverInstrument, (int)SmaPeriod);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0)
    {
    double goldPrice = goldSMA[0];
    double silverPrice = silverSMA[0];
    double priceDifference = (goldPrice * GoldPriceMultiplier) - silverPrice;
    Value[0] = priceDifference;
    }
    }
    }
    }
    
    [HASHTAG="t3322"]region[/HASHTAG] NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial https://forum.ninjatrader.com/search?searchJSON=%7B%22tag%22%3A%22region%22%7Dclass Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private CustomSMAIndicator[] cacheCustomSMAIndicator;
    public Custhttps://forum.ninjatrader.com/search?searchJSON=%7B%22tag%22%3A%22region%22%7DomSMAIndicator CustomSMAIndicator()
    {
    return CustomSMAIndicator(Input);
    }
    
    public CustomSMAIndicator CustomSMAIndicator(ISeries<double> input)
    {
    if (cacheCustomSMAIndicator != null)
    for (int idx = 0; idx < cacheCustomSMAIndicator.Length; idx++)
    if (cacheCustomSMAIndicator[idx] != null && cacheCustomSMAIndicator[idx].EqualsInput(input))
    return cacheCustomSMAIndicator[idx];
    return CacheIndicator<CustomSMAIndicator>(new CustomSMAIndicator(), input, ref cacheCustomSMAIndicator);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.CustomSMAIndicator CustomSMAIndicator()
    {
    return indicator.CustomSMAIndicator(Input);
    }
    
    public Indicators.CustomSMAIndicator CustomSMAIndicator(ISeries<double> input )
    {
    return indicator.CustomSMAIndicator(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.CustomSMAIndicator CustomSMAIndicator()
    {
    return indicator.CustomSMAIndicator(Input);
    }
    
    public Indicators.CustomSMAIndicator CustomSMAIndicator(ISeries<double> input )
    {
    return indicator.CustomSMAIndicator(input);
    }
    }
    }
    
    #endregion
    
    ​

    #2
    Hello Marble,

    The code you provided is using items that are not valid like [Input], did you try to use an external tool to generate this code?

    An additional note is that AddDataSeries does not work with user inputs, you need to hard code the instrurments into the AddDataSeries statements.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,
      It was created using visual studio.

      So how can I can I enable below "GoldInstrument" as input string in property panel?

      AddDataSeries(GoldInstrument, BarsPeriodType.Day, 1);

      Comment


        #4
        Hello Marble,

        I would not suggest using visual studio for code suggestions if however you are generating code is using [Input] because would be incorrect. You can delete this file and then use the Indicator wizard to generate the script over again. In the wizard you can define any user inputs you needed and then generate the file.

        AddDataSeries should look like the following to hard code it, you would not have any inputs for the instruments in the property panel.

        AddDataSeries("GC 12-23", BarsPeriodType.Day, 1);

        This is noted in 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.
        https://ninjatrader.com/support/help...ghtsub=adddata


        You additionally need to use AddPlot if you are going to plot a value.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello I have update the indicator with AddDataSeries and AddPlot as attached code file.

          Compile error does not occur. But facing issue - Plotted line for Value[0] is straight line with value of (Inst1SMA[0] * Multip1) missing (Inst2SMA[0] * Multip2) and also,
          ​ (Image attached, see bottom panel, pink line.

          Data load seems fine for both instrument from the two top panels. Not error is warned in control log, script editor....
          =====
          Code:
          // Calculate the SMA-based difference between the instruments
          double priceDifference = (Inst1SMA[0] * Multip1) - (Inst2SMA[0] * Multip2);
          
          // Plot the SMA-based difference on the chart using the Value property
          Value[0] = priceDifference;​
          ===========

          How can I fix this display and 2nd instrument calculation issue?

          the full code.

          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
          public class GC2 : Indicator
          {
          
          private Series<double> Inst1SMA;
          private Series<double> Inst2SMA;
          private int SmaPeriod;
          
          
          
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Indicator here.";
          Name = "GC2";
          Calculate = Calculate.OnEachTick;
          IsOverlay = true;
          DisplayInDataBox = true;
          DrawOnPricePanel = true;
          DrawHorizontalGridLines = false;
          DrawVerticalGridLines = false;
          PaintPriceMarkers = false;
          ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Overlay;
          //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;
          
          
          string Inst1 = "GC 12-23";
          string Inst2 = "SI 12-23";
          double Multip1 = 2.0;
          double Multip2 = 60.0;
          SmaPeriod=1;
          
          // Add the plot here
          AddPlot(Brushes.HotPink, "PriceDifference");
          
          }
          else if (State == State.Configure)
          {
          AddDataSeries(Inst1, Data.BarsPeriodType.Second,1, Data.MarketDataType.Last);
          AddDataSeries(Inst2, Data.BarsPeriodType.Second, 1, Data.MarketDataType.Last);
          
          
          // Initialize the Series for SMA
          Inst1SMA = new Series<double>(this);
          Inst2SMA = new Series<double>(this);
          
          }
          
          }
          
          protected override void OnBarUpdate()
          {
          // Calculate the SMA for the first instrument (GC 12-23)
          if (BarsInProgress == 0)
          {
          if (CurrentBars[0] > 0 && Closes[0].Count > 0)
          {
          double inst1Close = Closes[0][0];
          Inst1SMA[0] = (Inst1SMA[1] * (SmaPeriod - 1) + inst1Close) / SmaPeriod;
          }
          }
          // Calculate the SMA for the second instrument (SI 12-23)
          else if (BarsInProgress == 1)
          {
          if (CurrentBars[1] > 0 && Closes[1].Count > 0)
          {
          double inst2Close = Closes[1][0];
          Inst2SMA[0] = (Inst2SMA[1] * (SmaPeriod - 1) + inst2Close) / SmaPeriod;
          
          }
          }
          // Calculate the SMA-based difference between the instruments
          double priceDifference = (Inst1SMA[0] * Multip1) - (Inst2SMA[0] * Multip2);

          // Plot the SMA-based difference on the chart using the Value property
          Value[0] = priceDifference;


          }
          Attached Files

          Comment


            #6
            Hello Marble,

            The value you are plotting is equating to 0, you would need to check the math you are doing and make sure that results in a value.

            Plotting has to occur from BarsInProgress == 0, because you are using multiple instruments that wont sync up bars you need to add your other plotting conditions in the BarsInProgress 0 condition.

            Regarding the AddDataSeries and using variables, again we do not suggest doing that because it is inconsistent when the correct data is loaded. You should always use hard coded values in the AddDataSeries syntax, if you choose to use variables you run the risk of incorrect data loading or failures in various tools or use cases. The variables Inst1 and Inst2 are runtime variables.

            JesseNinjaTrader Customer Service

            Comment


              #7
              How should I update to reflect Plotting has to occur from BarsInProgress == 0?
              I still dont see why it is equating to 0.

              Comment


                #8
                Hello Marble,

                To plot from BarsInProgress 0 you need to set the value from that condition. You already have a BarsInProgress 0 condition:

                if (BarsInProgress == 0)
                {​
                all of your plotting logic here
                }

                You can remove the if (BarsInProgress == 1 ) condition.

                Another note is that you are using Series<double> and not plots, you need to set the plots and not the series. You can remove the series Inst1SMA and Inst2SMA. Please use the help guide to learn about plotting before editing the script, you need to either create public properties to set the plots (use the guidance i suggested in post 4) or use the Values collection to set plots.


                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jess, when you say Plotting, do you mean one line below or which part are you referring?
                  AddPlot(Brushes.HotPink, "PriceDifference");

                  Comment


                    #10
                    Hello Marble,

                    That is one part of plotting, did you review the samples in the help guide page for AddPlot so you can better understand all parts of plotting?
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Jesse,
                      I hava read below link but which other lines are you referring other than one line I have asked above?
                      https://ninjatrader.com/support/help...ml?addplot.htm



                      what are you referring as "all of your plotting logic here"?

                      Comment


                        #12
                        Hello Marble,

                        The help guide has samples that show what code you need for plotting. AddPlot only adds a plot, that does not help to set the plot. You need to review the samples near the bottom of the page so you can see how to set plots.

                        "all of your plotting logic here" means put your code that sets the plots in that area.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          I am confused where is the issue is
                          1) If I scroll back the lines are somewhat ok - except wrong calculation. (see attched)
                          But if I scroll to the latest, the line gets flattend as in my previous post with price data showing result of series 1 calculation.
                          what is the cause of flattened line only for the latest data screen?


                          2) I am still unclear why equation is 0 for Value and solution. Please elaborate.
                          // Calculate the SMA-based difference between the instruments
                          double priceDifference = (Inst1SMA[0] * Multip1) - (Inst2SMA[0] * Multip2);

                          // Plot the SMA-based difference on the chart using the Value property
                          Value[0] = priceDifference;​

                          Comment


                            #14
                            Hello Marble,

                            The code you provided has plotting logic happening for all series, you need to make sure that plotting logic only exists in your BarsInProgress 0 condition to make a consistent plot. Plots are associated only with your primary instrument on the chart. Different instruments have different number of bars so setting the plot for all instruments will provide random results.

                            If you are getting 0's you need to rreview the math you are doing and the values you are using in that statement. You can use prints to observe values while your script runs.


                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              What is the reason the lines plotted are drawn (except caluculation includes series inst1) but flattens out for the lastest screen?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,968 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post rbeckmann05  
                              Started by rhyminkevin, Today, 04:58 PM
                              4 responses
                              58 views
                              0 likes
                              Last Post dp8282
                              by dp8282
                               
                              Started by iceman2018, Today, 05:07 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post iceman2018  
                              Working...
                              X