Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

I can't use atmStrategySelector.SelectedAtmStrategy in OnOrderUpdate

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

    I can't use atmStrategySelector.SelectedAtmStrategy in OnOrderUpdate

    Good avening

    Could you help me, please?

    I have used Addon Framework to create an order replicator from one account to another. In this section of the code I have entered two conditions so that if the ATM Strategy is selected, the order is sent with the selected ATM and when nothing has been selected, the order is sent without ATM Strategy but I am receiving an error message that I share more down. I'd like to know if you are able to help me with a posible solution for this issue. Thank you.

    Code:
    private void OnOrderUpdate2(object sender, OrderEventArgs e)
    {
    try
    {
    #region MANEJO DE ORDENES A MERCADO
    //Entra ordenes a mercado solamente
    if (entryOrder == null)
    {
    if ((e.Order.Name == "" && e.OrderState == OrderState.Submitted && e.Order.IsMarket) || (e.Order.Name == "Entry" && e.OrderState == OrderState.Submitted && e.Order.IsMarket))
    {
    
    if (atmStrategySelector.SelectedAtmStrategy != null)      [THIS IS THE PART OF THE CODE THAT SEND ME AN ERROR]
    {
    Dispatcher.InvokeAsync(() =>
    {
    outputBox.AppendText(string.Format("{0}Entrada Copiada con ATM", Environment.NewLine));
    entryOrder = accountSelector.SelectedAccount.CreateOrder(Instru ment, e.Order.OrderAction, e.Order.OrderType, tifSelector.SelectedTif, qudSelector.Value, e.LimitPrice, e.StopPrice, string.Empty, "Entry", null);
    selectedAtmStrategy = NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(atmStrategySelector.SelectedAtmStrategy, entryOrder);
    });
    }
    else
    {
    Dispatcher.InvokeAsync(() =>
    {
    outputBox.AppendText(string.Format("{0}Entrada copiada sin ATM", Environment.NewLine));
    entryOrder = accountSelector.SelectedAccount.CreateOrder(Instru ment, e.Order.OrderAction, e.Order.OrderType, tifSelector.SelectedTif, qudSelector.Value, e.LimitPrice, e.StopPrice, string.Empty, "Entry", null);
    accountSelector.SelectedAccount.Submit(new[] { entryOrder});
    });
    }
    }
    }
    Below is the error that I have receiving in the outbox:
    Code:
    AddOnFramework_Working - OnOrderUpdate2 Exception: System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
    at System.Windows.Threading.Dispatcher.VerifyAccess()
    at System.Windows.DependencyObject.GetValue(Dependenc yProperty dp)
    at NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector.get_SelectedAtmStrategy()
    at NinjaTrader.Gui.NinjaScript.AddOnFramework_Working Tab.OnOrderUpdate2(Object sender, OrderEventArgs e) in c:\Users\Jeziel\Documents\NinjaTrader 8\bin\Custom\AddOns\AddOnFramework_Working.cs:line 1881
    Attached Files

    #2
    Hello jleira,

    Thank you for the post.

    From the given details I don't see what specifically may be incorrect. The addon sample alone uses a dispatcher in OnOrderUpdate and also the output text so that should not be an issue. Have you isolated which of the lines in that dispatcher are needed to see the error?


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

    Comment


      #3
      Hi jesse

      The line below is the one that is sending an error. For some reason
      Code:
      atmStrategySelector.SelectedAtmStrategy
      cannot be accessed from OnOrderUpdate2. I sent it in the attached image, there you can see the line of the error and the line where the error is.
      Code:
      if (atmStrategySelector.SelectedAtmStrategy != null)  [THIS IS THE PART OF THE CODE THAT SEND ME AN ERROR]
      { Dispatcher.InvokeAsync(() =>
      {
      outputBox.AppendText(string.Format("{0}Entrada Copiada con ATM", Environment.NewLine));
      entryOrder = accountSelector.SelectedAccount.CreateOrder(Instru ment, e.Order.OrderAction, e.Order.OrderType, tifSelector.SelectedTif, qudSelector.Value, e.LimitPrice, e.StopPrice, string.Empty, "Entry", null);
      selectedAtmStrategy = NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(atmStrategySelector.SelectedAtmStrategy, entryOrder);
      }); }
      Please see the error sended
      Code:
      AddOnFramework_Working - OnOrderUpdate2 Exception: System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
      at System.Windows.Threading.Dispatcher.VerifyAccess()
      at System.Windows.DependencyObject.GetValue(Dependenc yProperty dp)
      at NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector.get_SelectedAtmStrategy()
      at NinjaTrader.Gui.NinjaScript.AddOnFramework_Working Tab.OnOrderUpdate2(Object sender, OrderEventArgs e) in c:\Users\Jeziel\Documents\NinjaTrader 8\bin\Custom\AddOns\AddOnFramework_Working.cs:line 1881

      Comment


        #4
        Hello jleira,

        Thank you for the post.

        I tried the addon sample from the help guide and copy/pasted the code you have however I did not see the threading error. It was able to submit the order. Have you tried to re import the sample from the help guide and then modify it again with what the atm part? If so I would suggest uploading that sample so I can test it with the code to see the error as I was unable to see it from the given details.


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

        Comment


          #5
          Sorry, let me explain better. I have added a second Account Selector and created a different event handler for that selector (OnOrderUpdate2, OnExecutionUpdate2 and OnPositionUpdate2, etc) to handle the events of the second account. When I submit an order to the master account, the trade is copied to the second account. But I want the selected ATM to be launched in the account to be copied and therefore a condition is added to detect if the ATMStrategy Selector of the slave account is selected "if (atmStrategySelector.SelectedAtmStrategy! = Null)", but now when sending a order from the master account, I get the error that I showed you.

          What do you need me to send you so that you understand the problem? I attach a image show you the metods that I have created to handle the second accoud which is the Master Account and every trade that is submited by chart trader is detected and copied to the Slave account.

          In the link below I have share the problem in a short video. Please, watch it

          https://youtu.be/_V8bibNdga8

          Please, forgive me, I am using google traductor
          Attached Files
          Last edited by jleira; 03-01-2021, 05:06 PM.

          Comment


            #6
            Hello jleira,

            For scripting questions we always suggest to form a simple code sample that you can and attach with your post, that would be so we have the full context of the code used and can try it. We generally suggest to avoid copy/pasting code/images of code or providing videos.

            If you are adding a whole other set of logic for another account selector etc I would suggest to first try and see if you can reproduce the error in the general addon sample that we provide in the help guide. That gives us a known working starting point for you to build on and also allows us to avoid your other unneeded custom code you may have added.

            A possible solution would be to just use a class level AtmStrategy variable to store the selected strategy whenever the selection is changed. That could be done from the atmStrategySelector.SelectionChanged area. That avoids the problem by not accessing the UI control and avoids having a dispatcher.

            I look forward to being of further assistance.


            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello Jesse

              I could see what was the problem. I needed to put AtmStrategySelector.Selected Account in a Dispatcher.InvokeAsync. Everything was solve doing this.

              Thank you Jesse. Have a good day.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ttodua, Today, 03:05 PM
              0 responses
              2 views
              0 likes
              Last Post ttodua
              by ttodua
               
              Started by TigerKiger, Today, 01:12 PM
              0 responses
              12 views
              1 like
              Last Post TigerKiger  
              Started by tonynt, Today, 12:46 PM
              0 responses
              7 views
              0 likes
              Last Post tonynt
              by tonynt
               
              Started by ntdev, Today, 12:19 PM
              0 responses
              4 views
              0 likes
              Last Post ntdev
              by ntdev
               
              Started by RubenCazorla, Today, 12:02 PM
              0 responses
              5 views
              0 likes
              Last Post RubenCazorla  
              Working...
              X