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

2 entry orders help!

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

    Chelsea,

    I got the code to close open positions at the session end.

    Can you provide an example of:

    "You can continue scaling in with new entries in the same direction if you would like. With the unmanaged approach you can submit new orders without the position being closed or previous orders cancelled.

    If you want to exit the position, call SubmitOrderUnmanaged() with a market order in the opposite direction of the position. (For example if the position is Long 5, submit a sell market order with a quantity of 5)."

    Would I also need to create opposing Target and Stop-Loss child orders in OnExecutionUpdate()?

    Comment


      Hello bobperez,

      Scaling in:
      Code:
      if (Position.MarketPosition == MarketPosition.Long && Quantity == 1 /* other condtions to scale in here */)
      {
      SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, string.Empty, "longEntry2");
      }
      Exiting a position:
      Code:
      if (Position.MarketPosition == MarketPosition.Long /* conditions to exit here */)
      {
      SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, string.Empty, "positionExit");
      }
      These are just basic examples. Your logic will depend on what you are doing in the script. For example if you want to reverse the position and have a working stop loss and profit target using OCO working for a position, you would want to either just move the limit behind the market price and allow this to fill and exit the position and submit the entry when the limit order fills in OnExecutionUpdate(), or call CancelOrder() on the stop order and in OnOrderUpdate() detect the order is cancelled, and then submit the entry in the opposite direction.

      "Would I also need to create opposing Target and Stop-Loss child orders in OnExecutionUpdate()?"

      This depends on what you want the script to do. You can submit a stop loss and limit target for any new entry if you want. If the first entry has a stop and limit target and you just want to adjust the quantity of these orders you could do that instead. Yes, this would be in OnExecutionUpdate() when a new entry fills.
      Chelsea B.NinjaTrader Customer Service

      Comment


        Thank you, Chelsea. I'll see if I can get it right with your input.

        Bob

        Comment


          Originally posted by NinjaTrader_ChelseaB View Post
          Hello bobperez,

          Scaling in:
          Code:
          if (Position.MarketPosition == MarketPosition.Long && Quantity == 1 /* other condtions to scale in here */)
          {
          SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, string.Empty, "longEntry2");
          }
          Exiting a position:
          Code:
          if (Position.MarketPosition == MarketPosition.Long /* conditions to exit here */)
          {
          SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, string.Empty, "positionExit");
          }
          These are just basic examples. Your logic will depend on what you are doing in the script. For example if you want to reverse the position and have a working stop loss and profit target using OCO working for a position, you would want to either just move the limit behind the market price and allow this to fill and exit the position and submit the entry when the limit order fills in OnExecutionUpdate(), or call CancelOrder() on the stop order and in OnOrderUpdate() detect the order is cancelled, and then submit the entry in the opposite direction.

          "Would I also need to create opposing Target and Stop-Loss child orders in OnExecutionUpdate()?"

          This depends on what you want the script to do. You can submit a stop loss and limit target for any new entry if you want. If the first entry has a stop and limit target and you just want to adjust the quantity of these orders you could do that instead. Yes, this would be in OnExecutionUpdate() when a new entry fills.
          Hi Chelsea,

          I'm still not getting the results I expect.

          If I have a long OCO position entered with longStopEntry1 = SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.StopMarket, Contracts1, 0, entry_price_L1, ocoString1, "longStopEntry1"); and it has its corresponding child target and stop-loss created in OnExecutionUpdate(), what command do I need to use to close this position?

          Bob

          Comment


            Hello Bob,

            If you already have a working stop and limit exit order for the entry, you could either:

            Move the limit behind the market price so it fills and exits the position immediately. For example changing a sell limit to the bid price minus a few ticks should cause it to fill immediately (unless there is a very large rapid price decrease before the order is accepted by the brokerage).

            ChangeOrder(myLongExitLimitOrderVariableNameHere, myLongExitLimitOrderVariableNameHere.Quantity, GetCurrentBid() - 5 * TickSize, 0);

            ChangeOrder(Order order, int quantity, double limitPrice, double stopPrice)


            OR

            Cancel the stop and limit with CancelOrder(). Then when the orders update as cancelled in OnOrderUpdate() submit a market order to close the position.

            CancelOrder(myLongExitLimitOrderVariableNameHere);

            CancelOrder(Order order)


            In OnOrderUpdate():

            if (order.Name == "myLongExitLimitOrderVariableNameHere" && order.OrderState == OrderState.Cancelled)
            {
            SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.Market, 1, 0, 0, string.Empty, "longMarketExit");
            }



            Note, if you are using Order type variables, these need to be assigned order objects from OnOrderUpdate() and not assigned from the order object returned from the SubmitOrderUmanaged() call.
            The ProfitChaseStopTrailUnmanagedExample example linked below demonstrates assigning order objects to variables from OnOrderUpdate().

            Chelsea B.NinjaTrader Customer Service

            Comment


              Ok...thank you, Chelsea.

              Bob

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by morrnel, 05-12-2024, 06:07 PM
              8 responses
              144 views
              1 like
              Last Post nobchi
              by nobchi
               
              Started by nobchi, Today, 01:05 AM
              0 responses
              2 views
              0 likes
              Last Post nobchi
              by nobchi
               
              Started by Skifree, 06-17-2024, 06:35 PM
              3 responses
              18 views
              0 likes
              Last Post Skifree
              by Skifree
               
              Started by sy2023, 09-15-2023, 08:51 PM
              2 responses
              234 views
              0 likes
              Last Post Crusade12  
              Started by kiss987, 05-16-2018, 02:19 AM
              14 responses
              2,871 views
              0 likes
              Last Post bobperez  
              Working...
              X