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

Having an issue extracting atm strategy name from chart trader.

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

    Having an issue extracting atm strategy name from chart trader.

    I am having an issue extracting the atm strategy name from the chart trader. I know I can make my own listbox in the parameter listing, but I want to extract it from the chart trader instead of opening it everytime. How od Iget the contract quantity and the strategy name in order to create my order?



    int nContractQuantity = 0;
    string AtmStrategyName1 = "";
    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    NinjaTrader.Gui.Tools.QuantityUpDown
    quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
    nContractQuantity = quantitySelector.Value;
    Print("Contract quantity is : "+ nContractQuantity.ToString());
    }));

    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector selector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlATMStrategySelector") as NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector);
    AtmStrategyName1 = selector.SelectedAtmStrategy.ToString();
    Print("ATMStrategyName is : "+ selector.SelectedAtmStrategy.ToString());
    }));

    ChartControl.InvalidateVisual();


    TriggerCustomEvent(o =>
    {
    Order stopOrder = null;

    stopOrder = myAccount.CreateOrder(Instrument,
    OrderAction.Buy,
    OrderType.StopMarket,
    TimeInForce.Day,
    nContractQuantity,
    0,
    0,
    string.Empty,
    "Entry",
    null);

    // Submits our entry order with the ATM strategy named "myAtmStrategyName"
    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(AtmStrategyName1, stopOrder);
    myAccount.Submit(new[] { stopOrder });
    }, null);

    nTradeType = 2;
    Draw.TextFixed(this, "TradeType", "Buy Activated", TextPosition.BottomLeft);
    }

    #2
    Hello ballboy11,

    Thanks for your post.

    This isn't documented, but I think it would be easiest to access it from ChartControl.OwnerChart.ChartTrader.

    I.E.

    Code:
    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
        Print(ChartControl.OwnerChart.ChartTrader.AtmStrategy.Template);
        Print(ChartControl.OwnerChart.ChartTrader.Quantity);
    }));
    Let us know if there is anything else we can do to help.
    JimNinjaTrader Customer Service

    Comment


      #3
      That worked but I am still getting an error this is the code:
      The error is the following Indicator'myTextIndicator': Error on calling 'onBarUpDate' method on bar 16000. The calling thread cannot acces this object because a different thread owns it.

      int nContractQuantity = 0;
      string AtmStrategyName1 = "";



      ChartControl.Dispatcher.InvokeAsync((Action)(() =>
      {

      nContractQuantity = ChartControl.OwnerChart.ChartTrader.Quantity;
      AtmStrategyName1 = ChartControl.OwnerChart.ChartTrader.AtmStrategy.Te mplate;

      Print(ChartControl.OwnerChart.ChartTrader.AtmStrat egy.Template);
      Print(ChartControl.OwnerChart.ChartTrader.Quantity );
      }));







      ChartControl.InvalidateVisual();


      TriggerCustomEvent(o =>
      {
      Order stopOrder = null;

      stopOrder = myAccount.CreateOrder(Instrument,
      OrderAction.Buy,
      OrderType.StopMarket,
      TimeInForce.Day,
      nContractQuantity,
      0,
      0,
      string.Empty,
      "Entry",
      null);

      // Submits our entry order with the ATM strategy named "myAtmStrategyName"
      NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(AtmStrategyName1, stopOrder);
      myAccount.Submit(new[] { stopOrder });
      }, null);

      nTradeType = 2;
      Draw.TextFixed(this, "TradeType", "Buy Activated", TextPosition.BottomLeft);
      }

      //#############

      Comment


        #4
        Hello ballboy11,

        When I wire this up, I'm not hitting such an error. Please test what I have attached and compare with what you are doing. Debugging prints can be used to see where exactly the error is getting thrown.

        If you have questions on the error received, please modify my example so it demonstrates the issue in clear and simple terms, and I will be happy to comment. Please ensure you export the modification to this example so it can be imported and tested.

        Exporting as Source Code - https://ninjatrader.com/support/help...tAsSourceFiles

        I look forward to assisting.
        Attached Files
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks. I tested and it worked. I found out that wasn't the issue the issue is one of my objects and I don't understand why I would get this error.


          lblTextBlockOutput.Text = "Trade Activated";

          This is a
          private System.Windows.Controls.TextBlock lblTextBlockOutput;

          The funny thing is if I use my button click handlers I can change the lblTextBlockOutput.Text
          This textBlockOutput is in my ChartTrader.

          If I have logic and want to enter the trade above, I wanted to change the label to trade submitted. but this is the error I get.

          2020-12-16 18:52:20:178|3|4|Indicator 'MyIndicator': Error on calling 'OnBarUpdate' method on bar 16337: The calling thread cannot access this object because a different thread owns it.

          When that happes I actually have to repair the database in order for the indicator to work again. If I take out the line

          //lblTextBlockOutput.Text = "Trade Activated"; My indicator works no problem.

          //This actually works.
          private void BuyButton_Click(object sender, RoutedEventArgs e)
          {
          if(sTradeType == "Buy Logic Trade")
          {
          sTradeType = "Trade Activated";
          lblTextBlockOutput.Text = "Trade Activated";
          nTradeType = 99;

          }
          }


          //this does not work and it is in onbarupdate
          if(Close[0]> dChosenPrice)
          {

          sEntryType = "Enter Long Market";
          // lblObjectSelected.Text = "Enter Long Market"; having this added will make the indicator crash

          nTradeType = 0;
          CallStrategy();


          Any Solutions?

          Comment


            #6
            Hell ballboy11,

            I will need a simplified example that is in a compliable state in order to test and comment. Otherwise I am left to guess. The only thing I may point out here is that UI elements will exist on a UI thread. OnBarUpdate runs from an instrument thread, so if you want to modify a UI element, you will need to use a dispatcher.

            Multi Threading Considerations - https://ninjatrader.com/support/help...-threading.htm

            If you are still stuck, please modify the script in post #4 so it demonstrates the issue and attach it here so I may comment further.

            We look forward to assisting.
            Last edited by NinjaTrader_Jim; 12-17-2020, 06:52 AM.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by libertyworkc, Today, 03:09 AM
            0 responses
            1 view
            0 likes
            Last Post libertyworkc  
            Started by SusanBarnesa, Today, 03:00 AM
            0 responses
            2 views
            0 likes
            Last Post SusanBarnesa  
            Started by visentialsmax, Today, 02:27 AM
            0 responses
            1 view
            0 likes
            Last Post visentialsmax  
            Started by visentialsmax, Today, 02:26 AM
            0 responses
            3 views
            0 likes
            Last Post visentialsmax  
            Started by JorgaShaw, Today, 02:03 AM
            0 responses
            1 view
            0 likes
            Last Post JorgaShaw  
            Working...
            X