Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 order submit lifecycle?

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

    NT8 order submit lifecycle?

    It appears that in NT8 in a C# strategy, to submit an order you must do this:

    1. Get the Account. It looks like you do this using the known account name, so if the account is named "John Smith Trading", then:

    Account myAccount = Account.All.FirstOrDefault(a => a.Name == "John Smith Trading");

    2. Create the order you want to submit:

    Order stopOrder = myAccount.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, TimeInForce.Day, 1, 0, 1400, "myOCO", "stopOrder", null);

    3. Put the order into an array and submit it using Submit()

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

    My questions are:
    1. How do you ever know that the order has indeed been submitted? Is there a return value on Submit, or are there any callbacks that get called when the order gets submitted successfully? What are these callbacks? Where are they documented?
    2. How do we know if there is an error? For example, suppose you submit a BUYSTOP at a price below the current market? That would result in an error.
    3. How do you know when a stop or limit order gets filled? Is there a callback that gets called? What is this callback? Where is it documented?

    So basically I need a tutorial on whatever lifecycle methods or procedures exist for submitting and order, knowing if submission is successful, knowing when an order gets filled, etc.

    #2
    Hello westofpluto,

    The code you have provided is for the addon approach not NinjaScript Strategies, but would be one way to submit an order.
    https://ninjatrader.com/support/help...nt8/submit.htm

    With NinjaScript Strategies orders are submitted with order methods such as EnterLong() or SubmitOrderUnmanaged().
    https://ninjatrader.com/support/help...er_methods.htm
    https://ninjatrader.com/support/help...rderupdate.htm

    Orders submitted through the addon approach would be placed outside of the strategy and would not affect the strategy performance or order event methods.

    You can check the state of an order in the Addon approach by assigning the order to a variable and checking the OrderState.
    https://ninjatrader.com/support/help.../nt8/order.htm
    https://ninjatrader.com/support/foru...720#post820720

    Or you can assign a method handler to the OrderUpdate event and get updates about all orders, and check the OrderEventArgs.Error for errors.
    https://ninjatrader.com/support/help...rderupdate.htm

    Last, below is a link to a forum post with helpful information about getting started with NinjaScript.
    https://ninjatrader.com/support/foru...040#post786040
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Ok, so it looks like I want to use the Managed Orders functions (or possibly the Unmanaged order functions, see question at the end) that I can use inside onBarUpdate. I do NOT want to use the "addon approach". All I care about is entering an order to open a position inside the onBarUpdate method inside a Strategy.

      So inside onBarUpdate, I want to enter a stop or limit order to open a position, like this for example using managed orders:

      protected override void OnBarUpdate() {
      EnterLongLimit(GetCurrentBid());
      }

      Now, please correct me if I am wrong, but it looks like to manage the order I have to implement this lifecycle function inside my strategy:

      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity , int filled,
      double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) {
      // my code goes here
      }


      It also looks like I need to handle the situation of partial fills, so I also need to implement this method:

      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      // my code goes here
      }

      Am I correct so far?

      Do you have sample code to do the following?:
      1. How do I check to see if the order was rejected?
      2. How do I check to see that the order was filled? Can I check this in OnOrderUpdate, or only in OnExecutionUpdate?
      3. How would I check to see if the order expired (eg suppose I submit a DAY order and the DAY ended, so the order expired)?
      4. If I submit multiple orders, how do I know which order triggered OnOrderUpdate? Is there some sort of Order ID or Order Name on the Order object? How do I specify this order name when I submit the order?

      Additional question:
      When an order gets filled, I want to set OCO orders for stoploss at one price and takeprofit at another. So for example, suppose I enter a BUYSTOP order like this:

      EnterLongStopMarket(0, true, 5, 3300.0, "my order name");

      to buy 5 contracts of ES futures at price 3300.00.

      When the order gets filled, the OnExecutionUpdate method gets called. At this point, how would I enter both ExitLongStopMarket() as my stoploss and ExitLongLimit() as my profit target and link these as OCO orders?

      Or is it the case that I must use Unmanaged orders to do OCO? I see an oco parameter for submitting unmanaged orders, but no such property for submitting managed orders. And if I must use unmanaged orders, how do I keep the orders live until canceled? It looks like I can do that with managed orders but not unmanaged orders? Please explain how to do this.
      Last edited by westofpluto; 09-28-2020, 12:04 AM.

      Comment


        #4
        Hello westofpluto,

        From a NinjaScript Strategy, yes an order would be managed from OnOrderUpdate(), or at least assigned to a variable for later.

        Below is a link to the help guide.
        https://ninjatrader.com/support/help...rderupdate.htm

        The SampleOnOrderUpdate reference sample has example code.
        https://ninjatrader.com/support/help...and_onexec.htm

        The <order>.OrderState will be OrderState.PartFilled when part filled and will be OrderState.Filled when filled.
        If an order expires it is cancelled and will be OrderState.Cancelled.
        https://ninjatrader.com/support/help.../nt8/order.htm

        The <order>.Name will have the signal name of the order.

        The signalName is provided to the entry method call as a parameter.
        EnterLong(string signalName)

        For OCO, this would require using the unmanaged approach, generating an OCO ID string and submitting this as the string oco parameter to SubmitOrderUnmanaged.
        Below is a link to an example ProfitChaseStopTrailUnmanagedExample_NT8.
        https://ninjatrader.com/support/foru...269#post802269
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Let me rephrase the question on exits since I think I got steered in the wrong direction. When I enter a position, I want to set both a stoploss and a take profit. For example, suppose I enter a long position of 5 contracts on ES at 3400 - when that gets filled I want to set a stoploss of 5 contracts at price 3390 and I want to simultaneously set a takeprofit of 5 contracts at price 3420. In the OCO discussion above, I think NinjaTrader_ChelseaB was thinking about entry orders, not exit orders. To be clear, I want to set stoploss and take profit as exit orders. Yes under the hood these are OCO orders, but I don't think I have to do the unmanaged approach for these! The documentation says that I can use both SetStopLoss() and SetProfitTarget() one after the other to set up a OCO exit order.

          Suppose that in OnBarUpdate I enter a long position of 5 contracts as follows:

          EnterLong(5,"LONG_ENTRY");

          In OnOrderUpdate, I can check if the order passed in is "LONG_ENTRY" and if so, I can save the order into a member variable in my strategy. Let's call it myEntryOrder.

          As soon as that order gets filled, I want to set both a stoploss and takeprofit. To do that I need the average fill price for myEntryOrder. I get this from myEntryOrder.AverageFillPrice. I want my stoploss at price (average fill price - 10 points) and my take profit at (average fill price + 20 points):

          double stopPrice = myEntryOrder.AverageFillPrice - 10.0;
          double targetPrice = myEntryOrder.AverageFillPrice + 20.0;

          SetStopLoss("LONG_ENTRY", CalculationMode.Price, stopPrice, false);
          SetProfitTarget("LONG_ENTRY", CalculationMode.Price, targetPrice);

          Questions:
          1. Is it indeed true that I can set stoploss and takeprofit dynamically like this? So I do not need to use the unmanaged approach?
          2. Where exactly is the best place to submit these orders? It appears to me that OnOrderUpdate is a good place to store the entry order when it is filled, but (at least for the unmanaged approach), the recommended place to set stoploss is OnExecutionUpdate. Where should I put the code to enter dynamic stoploss and takeprofit? Do you have sample code that does this?

          Comment


            #6
            Actually, after further reading, it now appears I can submit the stoploss and take profit in OnBarUpdate at the same time I submit my entry order. Is that true?

            Suppose I am entering a long using a Buy Stop Market (so price has to move up for the order to be triggered). My code would be something like this:

            //
            // Must set stoploss and takeprofit before entering, since these apply to entries after setting
            //
            int ticksPerPoint = 4;
            int stopTicks = 10*ticksPerPoint;
            int takeProfitTicks = 20*ticksPerPoint;
            SetStopLoss("MY LONG ENTRY", CalculationMode.Ticks, (double)stopTicks, false);
            SetProfitTarget("MY LONG ENTRY", CalculationMode.Ticks, (double)takeProfitTicks);

            //
            // Now enter using buy stop
            //
            int contracts = 5;
            double entryPrice=3400.0;
            EnterLongStopMarket(contracts, entryPrice, "MY LONG ENTRY");

            Is this correct? Can I indeed do this all at once in OnBarUpdate without having to worry about doing anything inside OnOrderUpdate or OnExecutionUpdate?
            Please confirm or correct me if I am wrong.

            Also, note that I am using the version that explicitly names the entry signal (MY LONG ENTRY). Does SetStopLoss and SetTakeProfit apply only to entry MY LONG ENTRY? Is it true that I still need to call SetStopLoss and SetTakeProfit before calling EnterLongStopMarket in this case?
            Last edited by westofpluto; 10-08-2020, 01:14 AM.

            Comment


              #7
              Hello westofpluto,

              A stop loss and profit target can be added to a script with SetStopLoss() and SetProfitTarget() before an entry is placed.

              Below are links to the help guide.
              https://ninjatrader.com/support/help...ofittarget.htm
              https://ninjatrader.com/support/help...etstoploss.htm

              Set methods use OCO internally so you will not need to manage that.

              An unlocked strategy can use bar information for prices supplied to Set methods. In the Strategy Builder this would be accomplished with Exit orders instead of set methods.
              https://ninjatrader.com/support/foru...269#post802269
              https://ninjatrader.com/support/foru...596#post806596

              The 'From entry signal' of the exit or set method will need to match the 'Signal name' of the entry.
              The Strategy Builder 301 training video discusses.
              https://www.youtube.com/watch?v=HCyt...tu.be&t=52m38s

              Set methods can be set in OnBarUpdate, and it is recommended to set these before placing the entry order. They can also be dynamically updated after the position is entered.
              When using Exit methods or the unmanaged approach, the exits would be submitted in OnOrderUpdate() or OnExecutionUpdate() after the entry order has filled.


              Last, I am including a link to a forum post with helpful information about getting started with NinjaScript and C#.
              https://ninjatrader.com/support/foru...040#post786040
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TheCatTrader, Today, 04:52 PM
              0 responses
              4 views
              0 likes
              Last Post TheCatTrader  
              Started by localappleseed, Today, 04:10 PM
              0 responses
              8 views
              0 likes
              Last Post localappleseed  
              Started by Ragstoriches, Today, 03:13 PM
              1 response
              13 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by michaelsh, Today, 03:21 PM
              1 response
              15 views
              0 likes
              Last Post NinjaTrader_ChrisL  
              Started by Ragstoriches, Today, 02:53 PM
              1 response
              14 views
              0 likes
              Last Post NinjaTrader_Emily  
              Working...
              X