Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot convert to int (CS0029)

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

    Cannot convert to int (CS0029)

    I am getting the error "Cannot implicitly convert type 'NinjaTrader.Cbi.Instrument to 'int'" on this line:

    double todayClose = Closes[instrument][0];

    I haven't been able to figure out what is triggering it. I think this is the relevant code block.

    else if (State == State.Configure)
    {
    AddDataSeries("AAPL", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("AAP", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("AAL", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("A", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0 && CurrentBars[0] > 0)
    {
    foreach (var instrument in Instruments)
    {
    if (!previousCloses.ContainsKey(instrument))
    {
    previousCloses.Add(instrument, 0.0);
    }

    double todayClose = Closes[instrument][0];
    double yesterdayClose = previousCloses[instrument];



    Thanks for any help you can provide.​

    #2
    Hello WolfTrader9,

    Closes requires a BarsInProgress index and not an instrument. You would need to type in a number depending on which series you wanted to select. For example Closes[0][0] would get the primary bars and 0 bars ago on the primary bars.. Closes[1][0] would get the first secondary series at 0 bars ago.You would need to remove the loop and just specify the index you wanted instead.

    I would suggest to remove both the loop and dictionary and just define the code for each instrument, you have 4 total series so that would only be 4 lines of code to get the today close values for each series.

    Comment


      #3
      Jesse,

      I want it to scan the entire NASDAQ (I'll add all the ticker symbols after the code working), so I rewrote the code to output the results to a file. I am trying to figure out how to put the stock symbols in a list to loop. This is what I currently have. However, I am getting a "Parameter is not an attribute class" error. Do you have a better way of doing it?

      else if (State == State.Configure)
      {
      Calculate = Calculate.OnEachTick;
      previousCloses = new Dictionary<Instrument, double>();
      fileWriter = new StreamWriter("StockIncrease.txt");
      }
      }

      // Define an input parameter for the instruments you want to track
      [Parameter("Instruments to Track", "AAPL,AAP,AAL,A", ValueString = "AAPL,AAP,AAL,A")]
      public string InstrumentsToTrack { get; set; }

      protected override void OnBarUpdate()
      {
      // Split the input string into an array of instrument symbols
      string[] instrumentSymbols = InstrumentsToTrack.Split(',');

      // Loop through each instrument
      foreach (string symbol in instrumentSymbols)
      {
      // Get the instrument by symbol
      Instrument instrument = Instruments.GetInstrument(symbol);

      if (instrument != null)
      {
      // Calculate the percent change for the instrument
      double todayClose = Closes[instrument][0];
      double yesterdayClose = Closes[instrument][1];
      double percentChange = (todayClose - yesterdayClose) / yesterdayClose * 100.0;

      // Check if the percent change is greater than or equal to 5%
      if (percentChange >= 5.0)
      {
      Print("Symbol: " + instrument.FullName);
      fileWriter.WriteLine(instrument.FullName);
      fileWriter.Flush();
      }

      previousCloses[instrument] = currentClose;
      }
      }
      }
      protected override void OnTermination()
      {
      fileWriter.Close();
      }
      }
      }​

      Thanks,
      Benjamin

      Comment


        #4
        Hello WolfTrader9,

        There would not be any suggestion that I could make to do that task. The first problem is that trying to subscribe to a whole list of instruments in a script may fail depending on how many instruments you are trying to use. Data providers offer a set number of instruments that can be subscribed to at once and if your script requests more instruments than the limit it will fail to load. If even 1 instrument that you add using AddDataSeries fails that will prevent the whole script from running.

        The second problem is that using AddDataSeries dynamically is not supported, you would need to manually type in each of the instruments you wanted the script to use for it to work correctly.

        https://ninjatrader.com/support/help...=AddDataSeries

        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.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        628 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        359 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        562 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X