• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
    Patrick H.NinjaTrader Customer Service

    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 RobertoPaez, Today, 06:47 PM
              0 responses
              4 views
              0 likes
              Last Post RobertoPaez  
              Started by catinabag, Today, 06:45 PM
              0 responses
              3 views
              0 likes
              Last Post catinabag  
              Started by darkgreen, Today, 03:35 PM
              0 responses
              7 views
              0 likes
              Last Post darkgreen  
              Started by GTBrooks, Today, 02:44 PM
              3 responses
              14 views
              0 likes
              Last Post GTBrooks  
              Started by GLFX005, Today, 12:22 PM
              4 responses
              41 views
              0 likes
              Last Post NinjaTrader_Kate  
              Working...
              X