Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 04-23-2012, 04:33 PM   #1
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default 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,
NJA_MC is offline  
Reply With Quote
Old 04-24-2012, 04:03 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 25,298
Thanks: 414
Thanked 1,562 times in 1,528 posts
Default

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.
NinjaTrader_Bertrand is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Bertrand for this post:
Old 04-24-2012, 07:40 AM   #3
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

Quote:
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,
NJA_MC is offline  
Reply With Quote
Old 04-25-2012, 08:19 PM   #4
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

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?
NJA_MC is offline  
Reply With Quote
Old 04-26-2012, 05:05 AM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 25,298
Thanks: 414
Thanked 1,562 times in 1,528 posts
Default

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.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-26-2012, 10:05 AM   #6
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 189
Thanks: 17
Thanked 82 times in 56 posts
Default

This thread might be helpful:

http://www.ninjatrader.com/support/f...ad.php?t=20470
marcow is offline  
Reply With Quote
The following user says thank you to marcow for this post:
Old 04-26-2012, 05:18 PM   #7
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

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.
NJA_MC is offline  
Reply With Quote
Old 04-27-2012, 02:12 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 25,298
Thanks: 414
Thanked 1,562 times in 1,528 posts
Default

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?
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 04-28-2012, 08:16 AM   #9
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

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?
NJA_MC is offline  
Reply With Quote
Old 04-30-2012, 01:26 PM   #10
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,568
Thanks: 111
Thanked 313 times in 299 posts
Default

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
NinjaTrader_Brett is offline  
Reply With Quote
Old 04-30-2012, 01:34 PM   #11
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

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,
NJA_MC is offline  
Reply With Quote
Old 04-30-2012, 01:58 PM   #12
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,568
Thanks: 111
Thanked 313 times in 299 posts
Default

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
NinjaTrader_Brett is offline  
Reply With Quote
Old 04-30-2012, 02:08 PM   #13
NJA_MC
Member
 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 4 times in 3 posts
Default

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:
http://www.ninjatrader.com/support/f...ad.php?t=20470

I don't know if NT came up with a solution here...
NJA_MC is offline  
Reply With Quote
Old 04-30-2012, 02:44 PM   #14
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,568
Thanks: 111
Thanked 313 times in 299 posts
Default

Please simplify down if possible.

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

-Brett
NinjaTrader_Brett is offline  
Reply With Quote
Old 04-30-2012, 04:09 PM   #15
marcow
Senior Member
 
Join Date: Dec 2009
Location: Netherlands
Posts: 189
Thanks: 17
Thanked 82 times in 56 posts
Default

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
marcow is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-instrument strategy PnL jp_kettunen Miscellaneous Support 5 03-18-2010 09:46 AM
Is OnMarketData multi-threaded? evlap General Programming 2 10-24-2009 11:46 AM
Exposing a dictionary blarouche Indicator Development 8 10-20-2009 10:25 AM
NT is multi-threaded? JS999 Suggestions And Feedback 3 04-16-2009 09:03 AM
Multi-Threaded bsamani General Programming 1 02-08-2009 10:24 AM


All times are GMT -6. The time now is 01:31 AM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.