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

Multi-threaded.. Key in dictionary: 'PnL'

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

    Multi-threaded.. Key in dictionary: 'PnL'

    Hi,

    I have a fairly complex strategy that uses a timer and MarketData updates. I am trying to debug the following errror:

    **NT** Error on calling 'OnMarketData' method for strategy 'XXXXXXXXXXX/93f924f11b9f479caea25cf72ea3b31d': Item has already been added. Key in dictionary: 'PnL' Key being added: 'PnL'

    I am hoping you can tell me what NinjaTrader functions might e setting this key 'PnL' so I can lock code segment to avoid this collision.

    Thanks,

    #2
    NJA_MC, this would normally not be a default OnMarketData() event arg - http://www.ninjatrader.com/support/h...aeventargs.htm

    Do you write to a custom DB here in your script?

    You could look into adding try / catch blocks to isolate further the code section giving rise to the issues seen.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      NJA_MC, this would normally not be a default OnMarketData() event arg - http://www.ninjatrader.com/support/h...aeventargs.htm

      Do you write to a custom DB here in your script?

      You could look into adding try / catch blocks to isolate further the code section giving rise to the issues seen.
      I will give the try/catch blocks to help isolate the issue. I don't use any DB scripts, but I am using unmanaged orders and some can be placed/updated within the "OnMarketData" or "timed event". I was hoping to find out what functions might update a dictionary key by that name. I suspect it might be your "execute" of a closing position. Most likely updating statistics for the session. I will try to focus my looking in that area. Please let me know if you can help narrow my search further.

      Regards,

      Comment


        #4
        Okay,

        Kinda Stuck, I am starting to think this might be a side-affect of using Replay with "TriggerCustomEvent()" functions. Do you know if they are compatible. I know you guys do something a little different in replay mode. I am wondering if I am chasing a ghost and will not see the issue with a real-time run.

        I seem to get almost Random errors with this code segment under Replay as I don't want to spend weeks checking to see if this is a problem real-time.

        Code:
                public void SetupTimer(int m_milliseconds)
                {
                    if (Historical)
                        return;    // Ignore timer requests in Historical Mode
                    
                    Print(" SetupTimer: "+m_milliseconds+" ms");
                    if (m_timer==null)
                    {
                        Print(" NEW TIMER SetupTimer: "+m_milliseconds+" ms");
                        m_timer = new System.Timers.Timer();
                        //m_timer.Elapsed += (sender, args) => TimerEventProcessor("myCallHere", args); 
                        m_timer.Elapsed+=new ElapsedEventHandler(TimerEventProcessor);
                    }
                    if (!m_timer.Enabled)
                    {
                    m_timer.Interval=m_milliseconds;
                    m_timer.AutoReset=true;     // Must Start() for each interval;
                    m_timer.Start();
                    Print(" Vol:STARTED    SetupTimer: "+m_milliseconds+" ms");
                    }
                }    
               
                void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
                {      
                    try
                    {
                        TriggerCustomEvent(MyCustomHandler, 0, myObject);
                    }
                    catch (Exception e)
                    {
                        Print("Exception CustomHandler "+e.ToString());
                    }
                }
                
                private void MyCustomHandler(object state)
                {
        
                }
        Here are some of the random errors I am getting, I call the setup with 100 ms timing:
        4/25/2012 9:57:20 PM|3|128|Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/5b6c2ac3e2c745ca9f5d4405a01c26f2': More than 100 subsequent user events
        or
        4/25/2012 9:36:12 PM|3|128|Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/842fb6b7c3624543a3115011bab59cfb': Object reference not set to an instance of an object.

        I have tried this without the Try/Catch with the same results.. Seems like the random result might be do to funny timing with the Replay Connection at high rates of speed? Is there a way to confirm this without spending weeks testing?

        Comment


          #5
          For this setup, please try with a higher .MaxProcessedEvents in Initialize(), this would be an undocumented property that could help you here testing the trigger event with higher speeds in replay. I don't feel this would be an issue realtime, as it's likely just seen in high speed replay as a large # method calls / events are queued up before control is returned to NT.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            This thread might be helpful:

            Comment


              #7
              Hi,

              I tried setting that parameter to 1000. Still getting problems with the same shell code, at about the same point when it was the default 100. I was expecting it to be 10 times longer or somewhere around there if this had an impact. This time I got this error:
              **NT** Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/bd4314bf54644255b5dd2dfe6b5af4a8': Collection was modified; enumeration operation may not execute.

              The strange thing is the Try/Catch doesn't trigger on any of these error, so I can't stop the damage which is the Unmanaged Orders stop being processed.

              Comment


                #8
                Hi NJA_MC, you're running into a different error now, the queued up custom event chain is resolved with the higher max processed setting you use. It looks to me like the code is not prepared to run this quick event wise in a multi threading environment - do you see this at all when running slower in your replay?
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Hi Bertrand,

                  Speed does seem to affect the frequency but the errors seem to be random. I have a few new errors being cause by simply the CustomerTimerFunction:
                  **NT** Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/f25b3354922744508726c3391430a9e9': Object reference not set to an instance of an object.
                  **NT** Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/f25b3354922744508726c3391430a9e9': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


                  I did add back in some of the code in the timed event which may give new perspective on the issue:
                  Error on calling 'OnBarUpdate' method for indicator 'TradersDynamicIndexzt379': Item has already been added. Key in dictionary: 'My down arrow275' Key being added: 'My down arrow275'
                  **NT** Error on calling 'OnOrderStatus' method for strategy 'GHPVolatilityBreakOut01/73f10ea00aea444980c098056552b14e': Collection was modified; enumeration operation may not execute.

                  It seems like this might be a Replay issue, is there a way I might be able to force better synchronization. I have found these commands:
                  Account.SimulationReset();
                  Account.SimulationSynchronize();


                  Can I add the Synchronization function or something similar before the call to the timer function to see if that helps get NT ready for the CustomerTimer even?

                  Comment


                    #10
                    Hello,

                    This is Brett assisting for Bertrand who will be out of the office this week.

                    I am starting over new on your ticket therefor I have gone over it but i may be restating something Bertrand stated apologize for the inconvenience.

                    Here is what I am interested in.

                    What specifically is causing the log error:

                    **NT** Error on triggering custom event for strategy 'GHPVolatilityBreakOut01/f25b3354922744508726c3391430a9e9': Object reference not set to an instance of an object.

                    To isolate this if it was my code is first start pulling out everything I do in the custom method I am calling.Can you please create a new test indicator and then delete all the code out of the method and then test. Does the fact of just the timer event there cause the issue or not?

                    If not then continue by adding in half the lines of code back or whatever is easiest to test on. Does it happen now?

                    Specifically I'm interested what exact line of code in the custom method is giving us issue or is it not related to the code in the custom method and is instead related to the custom event NinjaTrader method.

                    -Brett

                    Comment


                      #11
                      Hi Brett,

                      That is current where I am at which is what doesn't make sense. As you can see in post #4, there is a shell of a code which just calls the TriggerCustomEvent with nothing in the event. So just be basic NT functions are creating the error (which vary from time to time).

                      Regards,

                      Comment


                        #12
                        Can you please post this strategy so I can run tests on my side and the if the test conditions are not already defined please let me know what settings and time and date to reply and I will do some tests to attempt to replicate the problem.

                        I understand its random however I would need to run some tests on my side to get it into development and would need reproduce it at least once on my side.

                        -Brett

                        Comment


                          #13
                          I will see if I can make a generic version I can post, if not I may need to email you the code privately as it has proprietary code (which is quite complex, used for placing orders).

                          In the mean time, it does look like this thread had the same/similar problem:


                          I don't know if NT came up with a solution here...

                          Comment


                            #14
                            Please simplify down if possible.

                            If this is not possible you can email me to support at ninjatrader dot com ATTN: Brett.

                            -Brett

                            Comment


                              #15
                              sorry NJA_MC to hijack your thread, I posted a suggestion in the suggestions thread for a native NinjaTrader Timer Method, see http://www.ninjatrader.com/support/f...ad.php?t=49494

                              Marco

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by stafe, Yesterday, 08:34 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by jclose, Yesterday, 09:37 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by firefoxforum12, Yesterday, 08:53 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by kmunroe478, Yesterday, 05:39 PM
                              2 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              115 responses
                              2,700 views
                              1 like
                              Last Post kevinenergy  
                              Working...
                              X