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

Sync Issues

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

    Sync Issues

    Hi,

    I have a strategy that sets stops and targets based on lines drawn on a chart. I have a routine that check that these will not trigger unwanted orders when the stargegy starts by setting the lines above / below the current notional value of a pairs trade.

    Unfortunately, the various operations do not appear to be completed in the correct order. I think this is a syncronising problem and I need to wait until one routine is complete before initiatin the next. Can someone please provide an example of how this can be done. I don't really undestand the example provided in the help.

    Thanks

    Ian

    #2
    Hello IanC28,

    Thank you for your reply.

    Could you provide a sample of your code that does not seem to be executing in the order you expect?

    What operations are not completing in the correct order?

    Which example in the help guide are you referring to?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      Thanks for the quick response.

      The help guide is https://ninjatrader.com/support/help...t8/?editor.htm entitiled Multi-Threading Consideration for NinjaScript

      The routines are quite involved.

      I basically so the following:

      1. get the saved stop and target values I also have a buy or sell values to initiate new orders. These values are parameters for the strategy.
      2. compare them to the current price and adjust them so targets above price and stops below price
      3. draw lines on the chart based on the adjusted values.
      4. initiate the checking of price compared to the lines on the chart
      5. if a stop, target, buy or sell is triggered perform that action.

      Problem is step 4 and 5 are triggered before 1 -3 complete and because 2 is not done the values that trigger 5 have not been moved out of the way of the current price when the strategy is started.

      Hope this helps if not I can send the whole strategy / indicators, but I would rather not.

      Thanks

      Ian

      Comment


        #4
        Hello IanC28,

        Thank you for your reply.

        I would need to see at least a reduced sample code to be able to tell you why your actions are not triggering in the order you expect. You may omit any code unnecessary to reproduce the behavior. If you'd rather not post that on the forums, you can send it in directly to platformsupport [at] ninjatrader [dot] com. Please include a link to this post in the body of the email and " 2443229 ATTN Kate W. " in the subject line.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi Kate,

          I can't send a snippet. I will send the indicator and stratgey later, I will need some time to do that.

          Can you please give more detail on the help specifically what goes inside the ChartControl.Dispatcher.InvokeAsync. How would I queue the routines it they are not related to chart commands

          if(State==State.Historical)
          {
          if(ChartControl!=null)
          {
          // add some text to the UserControlCollection through the ChartControls dispatcher
          ChartControl.Dispatcher.InvokeAsync(newAction(()=>{
          UserControlCollection.Add(newSystem.Windows.Controls.TextBlock{
          Text="\nAdded by the ChartControl Dispatcher."
          });
          }));
          }
          }


          Thanks

          Ian

          Comment


            #6
            Hello IanC28,

            Thank you for your reply.

            Actions inside the ChartControl.Dispatcher.InvokeAsync call will execute in order. Actions outside this will not be affected by the async call and as the async call is processing, it will go on to continue through the code after the Async call.

            There's a larger example of using this to add custom controls to the chart here:

            https://ninjatrader.com/support/help...collection.htm

            I will keep an eye out for your strategy.

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hello IanC28,

              Thank you for your patience.

              I've also sent you this via email but am posting here in case it may assist another user with similar issues.

              Your error is occurring not because things are happening out of sequence, but because you're trying to initialize the lines from OnStateChange, and Draw methods will not work correctly if they are called from the OnStateChange() method. That's also what's triggering the weird behavior I was seeing where they would vanish from the Drawing Objects collection and freeze up the chart.

              What I would recommend instead, if you just need to use the initialize method once, would be to set up a bool and do the initialization on your first run through of OnBarUpdate — set the bool to false in State.SetDefaults, then check that it's still false, initialize the lines, and set the bool to true. You'll want to remove the calls to InitializeLines() in State.DataLoaded and State.RealTime.

              I also found that I got some index errors unless I set the Current bar check to 30 bars. Here's what that all looks like in OnBarUpdate:

              Code:
              protected override void OnBarUpdate()
              {
                          try
                          { 
                                if(CurrentBar==1) LastTime = DateTime.Now;
                                if ((CurrentBars[1] < 20) || 
                                    (CurrentBars[2] < 20) || 
                                    (CurrentBars[3] < 20) || 
                                    (CurrentBar < 30))
                                          return;
              
                           if(LinesInitialized == false)
                           {
                                 InitialiseLines(281);
                                 LinesInitialized = true;
                           }
                           // more logic goes here
              }
              Here's a link to our help guide that has further information about working with draw objects:

              https://ninjatrader.com/support/help...t8/drawing.htm

              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by terkk, Today, 01:16 PM
              3 responses
              21 views
              0 likes
              Last Post Zigfried  
              Started by TraderElegante, Today, 12:14 PM
              1 response
              20 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by dgee949, Today, 11:44 AM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_JustinD  
              Started by NinjaTrader_Kate, Today, 12:01 PM
              0 responses
              20 views
              0 likes
              Last Post NinjaTrader_Kate  
              Started by efeuvejota01, Today, 11:42 AM
              4 responses
              37 views
              0 likes
              Last Post efeuvejota01  
              Working...
              X