• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

getting proper OrderID

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

    getting proper OrderID

    I have a strategy that will place orders, but there is a scenario that I have that if a user has an order in play and then starts the strategy it will take the users orderID as the first transaction. When flat then the strategy will take over automatically placing orders

    How do I get the orderID that the user placed so I can close that order in the strategy if needed?

    #2
    Hello ballboy11,

    Thank you for your post.

    You can use the Account class to pull the Orders collection. Please visit the following link for more information: https://ninjatrader.com/support/help...ount_class.htm

    Please let me know if you have any questions.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      I am close I have my auto entries working perfectly but I don't know how to close my manual entry this is what I have. This scenario is if the user placed an order THEN turned on the strategy.


      if(bManualOrder==true)
      {
      Print("Manual order is true");
      ExitLong();
      ExitShort();
      }

      i know in my logic i can get to my manual order but I don't know how to close I am manually using an atmstrategy called AtmStrategyTemplate.

      Lastly, it is a very simple strategy it will only have 1 contract. I just added exitlong and exit short because i wasn't sure how to close.
      Last edited by ballboy11; 03-08-2018, 06:54 PM.

      Comment


        #4
        how to find an active order in a strategy that the strategy did not place

        what I want is have a strategy that the user can place the order then start the strategy.

        My scenario would be this.

        Stopped out
        Profit target
        Strategy closes order.


        after that scenario is finished the strategy will place orders.

        condition is managed. i can get the strategy to place, cancel and close orders but I can not find the manual order to close.

        Comment


          #5
          Hello ballboy11,

          Thank you for your response.

          If you utilize the logic to pull the account orders for the instrument the strategy is running on you would then need to use the Account functions for CreateOrder() and Submit(). The managed approach functions for strategies (such as ExitLong() and ExitShort()) will not work to close the account position as they focus on the Strategy position. Please visit the following link for information on account versus strategy position: https://ninjatrader.com/support/help..._account_p.htm

          CreateOrder(): https://ninjatrader.com/support/help...reateorder.htm
          Submit(): https://ninjatrader.com/support/help...-us/submit.htm

          Please let me know if you have any questions.
          Patrick H.NinjaTrader Customer Service

          Comment


            #6
            if the order was created BEFORE the strategy started will create order work?

            Comment


              #7
              Originally posted by ballboy11 View Post
              if the order was created BEFORE the strategy started will create order work?
              Hello ballboy11,

              Thank you for your response.

              The Account Orders collection needs to be used to capture the order if you wish to manage it. If you need to manage a position then you would use Create and Submit.

              Please let me know if you have any questions.
              Patrick H.NinjaTrader Customer Service

              Comment


                #8
                i am not sure if i am in the the right area for closing an order


                Print("I am in the account loop");

                DateTime tCheckTime = new DateTime(2018, 01, 01);
                foreach (Order order in Position.Account.Orders)
                {
                // Print(String.Format("Order placed: {0} - {1}", order.Name, order.OrderAction));

                if (order.OrderState == OrderState.Filled)
                {
                if (tCheckTime < order.Time)
                {
                lManualOrderID = order.Id;
                tCheckTime = order.Time;

                }

                Print(order.Name);
                }
                if(order.OrderState == OrderState.Working)
                {
                lManualTargetID = order.Id;
                }

                if(order.OrderState == OrderState.Accepted)
                {
                lManualStop1ID = order.Id;
                }


                // }
                Order stopOrder;
                stopOrder = Account.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, 1400, "", "stopOrder", Core.Globals.MaxDate, null);

                Account.Submit(new[] { stopOrder });


                myAccount.Submit(new[] { stopOrder });

                Comment


                  #9
                  Hello ballboy11,

                  Thank you for your patience.

                  To cancel an order on the account for the instrument that the strategy is running on you can use the following:
                  Code:
                  		private Account acc;
                  		
                  		protected override void OnStateChange()
                  		{
                  			if (State == State.SetDefaults)
                  			{
                  				Description									= @"Enter the description for your new custom Indicator here.";
                  				Name										= "TestGrabAccountOrder";
                  			}
                  			else if (State == State.Configure)
                  			{
                  				lock (Account.All)
                  					acc = Account.All.FirstOrDefault(a => a.Name == "Sim101");
                  			}
                  			else if (State == State.Realtime)
                  			{
                  				if (acc.Orders.Count != 0)
                  				{
                  					acc.CancelAllOrders(Instrument);
                  				}
                  			}
                  		}
                  My example cancels the orders when the script begins processing Realtime data, however you can change the place that you call the CancelAllOrders in your script.

                  Please let me know if you have any questions.
                  Patrick H.NinjaTrader Customer Service

                  Comment


                    #10
                    thanks i will give it a try, the statement

                    acc = Account.All.FirstOrDefault(a => a.Name == "Sim101");

                    the search criteria is Sim101, how would I get the other account?
                    or do I just do

                    acc = Account.All.FirstOrDefault(a => a.Name != "Sim101");

                    Comment


                      #11
                      Hello ballboy11,

                      Thank you for your response.

                      Within the quotations you would put the name of the account. So "Sim101" becomes "YourLiveAccountName".

                      Please let me know if you have any questions.
                      Patrick H.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the cancel all orders it worked great but the last thing to do is close the active order this is what is showing. The targets and the stops close out but the active order is still running.
                        Attached Files

                        Comment


                          #13
                          I figured how to close my position. In a strategy if a manual strategy is entered and then the strategy starts MarketPosition will display flat eventhough there is a position activated.

                          I can close the targets and stops AND I can close the postion. by CreateOrder AND Submit.

                          There is a scenario that I do have an issue with. If a target is hit OR a stop is hit AND at the time I close the order with CreateOrder it will of course make one. What I need to do is NOT Create the order. If MarketPosition is flat in a strategy because it can not find the Manual order what trigger do I use in order to see if the target was hit. i tried these triggers.

                          protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
                          {
                          Print(" in OnAccountItemUpdate");
                          }

                          protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
                          {

                          }

                          protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                          Cbi.MarketPosition marketPosition, string orderId, DateTime time)
                          {
                          Print("in onExecutionUpdate");
                          }



                          protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                          int quantity, int filled, double averageFillPrice,
                          Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
                          {
                          Print("In Order update");
                          }

                          protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                          int quantity, Cbi.MarketPosition marketPosition)
                          {
                          Print("in On Position Update");
                          }

                          All will not show if the target or stop hit except OnAccountItemUpdate. This one activates every tick when there is a positon active.

                          Comment


                            #14
                            Hello ballboy11,

                            Thank you for your response.

                            It looks like you are mixing the Strategy functions with the Account functions. You actually want to use the OrderUpdate of the Account class to see when orders on the account fill. Please visit the following link for more information and an example on how to use the function: https://ninjatrader.com/support/help...rderupdate.htm

                            Please let me know if you have any questions.
                            Patrick H.NinjaTrader Customer Service

                            Comment


                              #15
                              I am going through the orders create order etc. and this is where I am having the issue.

                              I can not find my manually entered order if I place the Manual order BEFORE the strategy has started

                              All I want to do is close my Manual order at a specific time if my order has not hit the target OR hit the stoploss.

                              This is the logic in the area

                              {


                              //ISSUES: CAN NOT FIND MANUAL ORDER POSITION SIZE IF IT LONG OR SHORT
                              // IT ALWAYS STATES MARKET POSITION IS FLAT IF MANUAL ORDER IS ENTERED
                              // IF MANUAL POSITION IS CLOSED BY TARGET OR STOP CREATE ORDER WILL
                              // CREATE A SELL.
                              // NEED TO FIND POSITION SIZE POSITION DIRECTION CLOSE MANUAL POSITON
                              //SCENARIO OF AUTOTRADE HAS NO ISSUES


                              int tHour = Trade1_EndTime.Hour;
                              int tMinute = Trade1_EndTime.Minute;

                              Print("In Closing section");
                              // CLOSE ALL ORDERS IF MANUAL OR STRATEGY DRIVEN
                              if (tHour == tMarketHour && tMinute == tMarketMinute)
                              {
                              bTrade1Complete = true;
                              // IF A MANUAL ORDER DO SCENARIO ELSE DO CLOSE STRATEGY SCENARIO
                              if (bManualOrder == true)
                              {
                              if (acc.Orders.Count != 0)
                              {
                              //THIS SECTION WORKES FOR CANCELLING ALL TARGETS AND STOPS
                              acc.CancelAllOrders(Instrument);

                              //MANUAL ORDER MARKET POSTION IS ALWAYS FLAT EVEN IF THERE IS AN ACTIVE POSITION
                              // if (Position.MarketPosition == MarketPosition.Long)
                              // {
                              Print("marketPosition is " + Position.MarketPosition );

                              Order stopOrder;
                              stopOrder = acc.CreateOrder(Instrument,
                              OrderAction.Sell,
                              OrderType.Market,
                              OrderEntry.Automated,
                              TimeInForce.Day,
                              1, 0, 0, "", "",
                              Core.Globals.MaxDate, null);

                              acc.Submit(new[]{stopOrder});
                              // }
                              // else if (Position.MarketPosition == MarketPosition.Short)

                              }
                              }
                              else
                              {
                              //THIS WORKS PERFECTLY IF THERE IS NO MANUAL ORDER ENTERED BEFORE THE STRATEGY HAS STARTED
                              if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
                              {
                              Print("in close time");
                              AtmStrategyClose(atmStrategyId);
                              }
                              }
                              atmStrategyId = String.Empty;
                              orderId = String.Empty;
                              }
                              }




                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by supermht, Today, 07:59 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post supermht  
                              Started by mohdhm, Today, 05:13 PM
                              1 response
                              12 views
                              0 likes
                              Last Post nkhoi
                              by nkhoi
                               
                              Started by 3lliot, Today, 05:55 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post 3lliot
                              by 3lliot
                               
                              Started by Fargo, Today, 04:57 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post Fargo
                              by Fargo
                               
                              Started by Johnny Santiago, Today, 03:57 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post Johnny Santiago  
                              Working...
                              X