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

Using an indicator to run an external program if orders and market position is flat

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

    Using an indicator to run an external program if orders and market position is flat

    I have an indicator which runs an external bat file on each bar close using Process.Start. This part works ok.

    But I only want the Process.Start command to run if there are no pending orders and the market position is flat.

    I tried as below but get "Error on calling 'OnBarUpdate' method on bar 6304: Object reference not set to an instance of an object."

    //Add your custom indicator logic here.
    if(State == State.Historical)
    return;
    if (accountPosition.MarketPosition == MarketPosition.Flat)
    {
    //Print(string.Format("{0} accountPosition.MarketPosition"));
    Process.Start(@"C:\Users\paulb\Documents\MyNTstuff \runmacro.bat");
    }​


    Thanks
    Last edited by paulbishy; 12-07-2022, 12:17 PM.

    #2
    Hello paulbishy,

    Has accountPosition been assigned a position object?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea B.

      Thanks for prompt reply.

      Im no C# programmer but im sure its something relatively simple.

      Do you mean this ?

      public class barcloserunmacrobat_withOrder : Indicator
      {
      private Position accountPosition;
      private List<Order> changeOrdersArray, submitOrdersArray;
      private double currentPtPrice, currentSlPrice;
      private Order entryBuyMar****rder, profitTargetOrder, stopLossOrder;
      private Account submissionAccount;​


      Thanks
      Paul
      Last edited by paulbishy; 12-07-2022, 12:58 PM.

      Comment


        #4
        Hello Paul,

        You need an account object assigned to the variable.

        Below is a link to the help guide.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          Happy New Year, hope you had a good break.

          Due to work commitments and Xmas I am having another look at trying to get this working.

          At the end of the day, I just want the line "Process.Start(@"C:\Users\paulb\Documents\MyNTstuf f \runmacro.bat");" to run of the account is flat or there are no orders.

          How can I get a complete indicator written with the above logic ?

          Thanks
          Paul

          Comment


            #6
            Hello Paul,

            This is a dos command line to run a batch script. Far outside of what is supported by NinjaTrader.

            What do you want to do?

            Are you trying to trigger some action when you click a button in an addon window in NinjaTrader?

            Previously you were asking about an accountPosition variable.

            You needed to find an account and assign that to a variable, then find the account.Position. Did you get that part worked out?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              The dos command part works fine.

              I just need to check before the dos command so that the dos command doesnt run if the account has open orders or is already in a position

              I cannot get the account.Position to work. Dos command always runs

              Something like this:

              If account.Position equal to flat and no open orders
              then
              run dos command
              end

              Paul
              Last edited by paulbishy; 01-06-2023, 11:22 AM.

              Comment


                #8
                Paul,

                I would not be able assist with the dos command.

                Lets focus on the account.

                Previously you were asking about an accountPosition variable.

                You needed to find an account and assign that to a variable, then find the account.Position. Did you get that part worked out?​
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,

                  Understood.

                  No, I have not been able to get the account.Position to work.

                  Is there any complete template indicator which you can provide which provides the account.Position ?
                  I can then place the dos command after this.

                  Thanks
                  Paul

                  Comment


                    #10
                    Hello Paul,

                    By template do you mean a NinjaScript example?

                    I am going to assume you are not looking for an actual chart template or script template file.


                    Below is a link to an example indicator that assigns a Account object to an Account variable. (ProfitChaseStopTrailIndicatorExample_NT8)
                    https://ninjatrader.com/support/foru...ing#post802269
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks Chelsea

                      I have added Orders and Account object but still struggling to get the logice together to check if there any open Orders or Positions.

                      Any help appreciated

                      Thanks
                      Paul
                      Attached Files

                      Comment


                        #12
                        Hello Paul,

                        Once you have an account assigned to a variable, loop through the <Account>.Orders.

                        Below is a link to the help guide on <Account>.Orders which includes a code sample.
                        https://ninjatrader.com/support/help...rs_account.htm

                        Below is a link to the help guide for the Order object.
                        https://ninjatrader.com/support/help.../nt8/order.htm

                        If the <Order>.OrderState is OrderState.Working or the <Order>.OrderState is OrderState.Accepted, then you know the order is working.
                        Code:
                        foreach (Order order in myAccount.Orders)
                        {
                            if (order.OrderState == OrderState.Working || order.OrderState == OrderState.Accepted)
                           {
                              // this order is a working order. change a bool to true, assign the order to a variable, or take the action you would like to take when finding a working order.
                           }
                        }​
                        Loop through the <Account>.Positions.

                        Below is a link to the help guide on <Account>.Positions which includes a code sample.
                        https://ninjatrader.com/support/help...ns_account.htm

                        Below is a link to the help guide on the Position object.
                        https://ninjatrader.com/support/help...8/position.htm

                        If the <Position>.MarketPosition is not MarketPosition.Flat, the position is open and is either long or short.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Chelsea,

                          Many thanks for your support, I now have the logic working.

                          But I now have a 2nd query.

                          Im using the playback tool to testout my indicator but I need the playback to pause momenteraly while my indicator makes some calculations and then playback to continue once the indicator has completed its calculations.
                          Is this possible ?

                          Thanks
                          Paul

                          Comment


                            #14
                            Hello Paul,

                            Hit the pause button on the Playback controller.

                            If you are asking if you can control playback through code, there would not be a supported method for this.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by lorem, Today, 09:18 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post lorem
                            by lorem
                             
                            Started by hazylizard, Today, 08:38 AM
                            4 responses
                            10 views
                            0 likes
                            Last Post hazylizard  
                            Started by geddyisodin, Today, 05:20 AM
                            2 responses
                            18 views
                            0 likes
                            Last Post geddyisodin  
                            Started by Max238, Today, 01:28 AM
                            5 responses
                            47 views
                            0 likes
                            Last Post Max238
                            by Max238
                             
                            Started by giulyko00, Yesterday, 12:03 PM
                            3 responses
                            13 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Working...
                            X