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

Error on calling 'OnBarUpdate': key has already been added

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

    Error on calling 'OnBarUpdate': key has already been added

    hi,
    i've created an indicator which runs on a text file (locally on my computer).
    indicator read from local text file per ticker.

    on the first try (on the first ticker), i had no problem but when im trying to run on different tickers im getting this error:

    Error on calling 'OnBarUpdate' method for indicator '******' on bar 0: An item with the same key has already been added.

    needless to say that text file location is general and and Instrument name is added to address in the beginning of run time ('general folder location' + instrument name + ".txt").

    would appreciate your support!

    thx,
    Yoni

    #2
    Hello yonimoti,

    Thank you for your post.

    Without looking at the code I would say it appears either two objects are trying to be assigned to the same index for a data series, or the same object is attempted to be added twice. Although, you could be using an Array or List.

    A good way to find the cause is to break the code down into sections and use liberal Print() methods at points where objects are assigned or called in the code.

    For information on Print() please visit the following link: http://www.ninjatrader.com/support/h.../nt7/print.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Dont understand why is that? When i run a strategy, it doesnt concern about specific index/instrument. Why is it different with indicator?

      So when i open a new graph and run the indicator from scratch (of a new ticker), shouldnt all variables/arrays/etc initialized?
      Last edited by yonimoti; 05-10-2015, 09:22 AM.

      Comment


        #4
        Can you post some sample code that does this?

        I open and read the same file in NT7 - both from the same indicator code. 1 from a chart, and the other is called from a strategy with no issues in market replay.

        Comment


          #5
          here's the code sample of OnBarUpdate + one func before that init the Sentiment dictionary.
          SentimentDic should holds Ticker sentiment per day.


          // Initialize the SentimentDic table which assembled from attributes: {KEY == yyyyMMdd , Value == SentimentRSS Value}
          private void InitSentimentDic()
          {
          string[] stringSeparators = new string[] {" "};
          string myFullTextAddress = myTextAddress + Instrument.FullName + ".txt";
          Print("My text location: " + myFullTextAddress);

          if (File.Exists(myFullTextAddress))
          {
          var lines = File.ReadAllLines(myFullTextAddress);

          for (int i = 0; i < lines.Length; i++)
          {
          var fields = lines[i].Split(stringSeparators,StringSplitOptions.None); // seperate each line to: date --> field[1] and sentiment_value --> field[2]
          SentimentDic.Add(fields[1].ToString().Trim(), Convert.ToDouble(fields[2])); // fields[1] == yyyyMMdd , fields[2] == SentimentRSS Value. Trim func removes all spaces from string
          Print(fields[1]+", "+fields[2]);
          }

          isDictionaryInitialized = true;
          }
          }

          protected override void OnBarUpdate()
          {
          if (IsDictionaryInitialized != true)
          {
          InitSentimentDic();
          foreach (string a in SentimentDic.Keys)
          {
          Print("List of Keys: (" + a+")"+" Value: "+SentimentDic[a]);
          }
          }


          string key = ToDay(Time[0]).ToString();

          if (SentimentDic.ContainsKey(key) == true) // check whether key exists in SentimentDic or not
          {
          AvgSentimentPerDay.Set(SentimentDic[key]);
          Print("ToDay is: " + ToDay(Time[0]));
          Print("Key exists inside sentimentDic and his value is: " + SentimentDic[key]);
          lastSentimentValue = SentimentDic[key];


          // Use this method for calculating your indicator values. Assign a value to each

          }

          // Key doesnt exist in SentimentDic, therefore we'll use our last saved sentiment value
          else
          {
          try
          {
          Print("ToDay is: " + ToDay(Time[0]));
          AvgSentimentPerDay.Set(lastSentimentValue);

          Print("Key doesnt exists inside sentimentDic, so setting last SentimentRSS value: " + lastSentimentValue);
          }

          // Currently not in use: throw exception when KeyPreviousDay doesnt exist or there's no value in ToDay(Time[1])
          // no thrown exception, because we dont use Time[1] for keyPreviousDay variable
          catch (Exception ex)
          {
          Print("Key doesnt exists && this is the first day of Simulation (so also no prev day). Setting Value to 0");
          Print("Expected ex: " + ex.ToString());
          AvgSentimentPerDay.Set(0);
          }


          }

          }
          Last edited by yonimoti; 05-10-2015, 11:52 AM.

          Comment


            #6
            Is SentimentDic something your wrote?

            Is it blowing up on the SentimentDic.Add?

            Comment


              #7
              ok. it seems like i understand what im doing wrong
              thx for the guidance
              i'll try to fix it and keep you up to date.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by algospoke, 04-17-2024, 06:40 PM
              6 responses
              48 views
              0 likes
              Last Post algospoke  
              Started by arvidvanstaey, Today, 02:19 PM
              4 responses
              11 views
              0 likes
              Last Post arvidvanstaey  
              Started by samish18, 04-17-2024, 08:57 AM
              16 responses
              61 views
              0 likes
              Last Post samish18  
              Started by jordanq2, Today, 03:10 PM
              2 responses
              9 views
              0 likes
              Last Post jordanq2  
              Started by traderqz, Today, 12:06 AM
              10 responses
              21 views
              0 likes
              Last Post traderqz  
              Working...
              X