Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A thread on threads

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

    A thread on threads

    How much of a concern are threads in NT8 strategy development?
    To be more precise, if I am sticking with relatively simple techniques, do I have to worry about threading, or is it taken care of by Ninjascript? I am planning to do some calculations in OnBarUpdate based on indicators, then Enter (and in some cases Exit) a trade with the managed approach and do some status checking, set variables in OnOrderUpdate, OnExecutionUpdate. I tried to look into it in Help, but threading is confusing to me. Is it possible that more than one of those events are triggered at the same time messing up my status variables? Is it possible that the same event is triggered from more than one thread creating a mess of my status variables? How do I handle it if it is possible?
    Are there some sample scripts how to deal with threading issues if they are a concern in similar simple situations? Help refers to the Educational Resources on the Forum, but I could not find it, could someone please give me a link to it?

    #2
    Hello Titus,

    Threads would basically be of no concern to nearly all NinjaScript code. The only time threading is important would be a complex item that accesses UI controls which may be on a different thread. In those cases you still won't be managing any threads or using threading in any way, just dispatching to the said thread.

    The event driven methods can occur in orders which would present problems of setting class level variables. For example OnBarUpdate is not associated with the order event methods like OnOrderUpdate. Those methods are driven by order events when using a realtime broker. Some brokers also don't follow the same chain of events so OnOrderUpdate and OnExecutionUpdate can be called in different orders than expected.

    Is it possible that more than one of those events are triggered at the same time messing up my status variables?
    Yes, it really depends on what you are seeing happen. As mentioned you could see order events in the middle of a bar while its building and then later have OnBarUpdate called when the data builds a bar.

    Is it possible that the same event is triggered from more than one thread creating a mess of my status variables?
    The same event no, you can see the same override called multiple times for a single order though. OnExecutionUpdate and OnOrderUpdate can be called multiple times for 1 order as its status will update and may part fill.

    I believe a better way to approach your questions here is to not look directly to the help guide/samples but try and provide some details on what you are specifically seeing as the issue. What code are you using and what was your expectation/what did you see happen. With those details we may be able to point out a specific help document or provide more context about what you observed and why that happened.

    You mentioned that your status variables are problematic, for that type of question it would be easier to answer if we know where you defined the variable, how its used and how its set and most important what you expected to happen/what actually happened.

    I look forward to being of further assistance.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Thank you for your quick and thorough reply.

      I do not have any code in NT8 at the moment, I am just getting started with NT8 development, but the Multi-Threading Consideration for NinjaScript section in the documentation got me worried. My biggest worry was how to handle if the same event is triggered twice at the same time, like OnOrderUpdate handling a part fill, and before it is even done, another part fill (for filled) comes in. Or a new OnBarUpdate before the previous has even finished processing. But as you said it won't happen, so why worry about something that isn't going to happen.

      Another worry, but that is just event handling I guess, is one event happening during another event. Let me try to give you an example of what I am thinking about. It may not make much trading sense, but let's just assume it does.

      Let's say I want to enter Long with 10 contracts at the 100 price level. OnBarUpdate takes care of that submitting an entry order for 10 @ 100, status variables set accordingly.
      In OnOrderUpdate I get a part fill of 6 contracts, trying to update the status to show part fill of 6, still waiting for 4 contracts to fill.
      While this is happening, another OnBarUpdate comes in, market now at 102, but the part fill is not reflected in the status yet, so the logic decides the market has moved far enough to try to adjust the entry price for the supposedly still open order of 10 contracts, and update the status to reflect the new situation. So the status is now updated by OnBarUpdate to show that there is an open order for 10 @ 102. Meanwhile OnOrderUpdate finishes updating the status to show 6 filled, 4 still open. Both statuses are out of whack with the real situation, which is by now 6 filled at 100, 10 waiting at 102. How do I deal with something like this? What is the best practice?

      PS. While I was typing all this, I got severaly popups saying invalid server response, and after a while I got kicked out, losing everything I have already typed. Do you know what this is, and why?

      Comment


        #4

        Hello Titus,

        If you are not trying anything yet I would not suggest to look too deeply into the multithreading documentation, that's really only going to apply in certain programming cases. The main take away for order events from that page would be to just use what was passed in from the order event method parameters if you need to know about that specific event. That would be using the items in the overload like:

        Code:
        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment).
        The events otherwise are not going to conflict, your code will be executed for each event.

        Unless your code is extremely slow the chances of having another event called during the middle of another event would be highly unlikely. The code will execute and hit the end of the method very fast so events happening in the middle of logic that is actively executing would be extremely unlikely.

        I couldn't really comment on the server response, was that when submitting the post? It may have been a temporary difficulty, all I could really suggest would be to copy your text before submitting the post in case of difficulties in the future. The forum does have a restore draft feature for when you leave and return but its on a timed interval and may not catch the text in some cases.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Okay, I won't worry much about something that is unlikely to happen.

          I am still unable to locate the "Educational Resources on the Forum" Help is referring to. Do you have a link or any other way to find it?

          Thank you.

          Comment


            #6
            Hello Titus,

            Did you see that mentioned in a specific help guide page or can you provide more context on where you read "Educational Resources on the Forum"?

            This is the only educational resources page I know of that is titled that: https://ninjatrader.com/support/help..._resources.htm
            Some samples used to be held on the forum which are now in the help guide: https://ninjatrader.com/support/help...ce_samples.htm


            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello Jesse,

              Yes, I found that description in the help guide, I copied it (ctrl-c) from there after I could not find anything like that on the forum, to make sure I am asking for the right thing.
              I cannot find the exact page right now, but I found something similar here: https://ninjatrader.com/support/help...operations.htm
              It says: "The NinjaScript Educational Resources Forums feature sample code and tips covering real-world scenarios and commonly used snippets" Clicking the link in that description leads to a blank forum page though.

              I remember a forum section like that from some time ago, I downloaded quite a few samples for NT7, so I tried something similar for NT8, but no luck so far.

              Comment


                #8
                Hello Titus,

                Thank you for the reply.

                The link on that page for the education forum resources would be the items that are now in the help guide. The forum was restructured and those items had been collected into the help guide. https://ninjatrader.com/support/help...ce_samples.htm

                There is also the Tips section which has some other info: https://ninjatrader.com/support/helpGuides/nt8/tips.htm

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by cls71, Today, 04:45 AM
                0 responses
                1 view
                0 likes
                Last Post cls71
                by cls71
                 
                Started by mjairg, 07-20-2023, 11:57 PM
                3 responses
                213 views
                1 like
                Last Post PaulMohn  
                Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                4 responses
                544 views
                0 likes
                Last Post PaulMohn  
                Started by GLFX005, Today, 03:23 AM
                0 responses
                3 views
                0 likes
                Last Post GLFX005
                by GLFX005
                 
                Started by XXtrader, Yesterday, 11:30 PM
                2 responses
                12 views
                0 likes
                Last Post XXtrader  
                Working...
                X