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

Questions About Unmanaged Orders

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

    Questions About Unmanaged Orders

    Hello,

    After use managed approach for a while i see, i encounter obstacles because of NT's order management rules. For escape from this obstacles i decide to use unmanaged approach and read user guide and related forum posts about it. But i see there is no detailed information about subject especially about OrderAction & OrderType.

    Below i'll share what i understand and if i'm wrong please warn/inform me.

    1. When we flat and want to go long we'll use OrderAction.Buy
    2. When we long and want to close it we'll use OrderAction.Sell
    3. When we flat and want to go short we'll use OrderAction.SellShort
    4. When we short and want to close it we'll use OrderAction.BuyToCover
    5. If our buy order filled and want to create stop loss order we'll use OrderAction.Sell & OrderType.StopMarket/StopLimit
    6. If our sell order filled and want to create stop loss order we'll use OrderAction.Buy & OrderType.StopMarket/StopLimit
    7. If our buy order filled and want to create take profit order we'll use OrderAction.Sell & OrderType.Limit
    8. If our sell order filled and want to create take profit order we'll use OrderAction.Buy & OrderType.Limit
    9. If our buy/sell order filled and want to create both stop loss and take profit order at the same time we'll use above orders but this time we'll give same OCO number for both SL&TP orders and this OCO number must be unique for each buy/sell orders.
    10. StopMarket orders fills immediately but StopLimit orders pending and when limit order hit then market order will create.
    11. MIT order also pending orders and after limit price hit, market order creating and position open from the most reasonable price.

    * If all above is right. So i write a strategy and use only OrderAction.Buy and OrderAction.Sell for open/close my positions never used SellShort & BuyToCover. The strategy works well, why? how? Am i missing something?

    * When use managed approach simply EnterLong reversing short position vise versa. When i use unmanaged orders how can i do it properly? What is my options?

    Thanks,
    Aytac
    Last edited by aytacasan; 03-07-2017, 07:47 AM.

    #2
    Thank you for your questions aytacasan.

    To your questions, you are correct in these, except for 6 and 8, these should be buy-to-cover. Generally :

    • Buy enters a long position, and Sell closes a long position
    • Sell Short enters a short position, and Buy to Cover closes a short position

    As to why you are able to enter short positions with sell orders, not all data feed providers are as strict as each other. Sending a Sell rather than Sell Short order to some data feed providers will open a short position, and with others, will not. When you take the unmanaged approach, you have a greater amount of control over how orders operate, and are taking on the responsibility of learning the ins and outs of each of your data providers and any nuances. These order types are defined by brokerages and exchanges.


    Generally, then, if you discipline yourself to follow the two bullets I outlined, you will have code that works with any data feed provider that Ninja supports.


    An example which will illustrated the differences is reversing a position. This is a four step process :


    1. Record your market depth in a variable
    2. Place a sell or buy-to-cover order to close your position
    3. Confirm this order has been closed
    4. Place a buy or sell-short order to open a new position with the information you recorded earlier

    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Jessica,

      Thanks for your clarification. Unmanaged order types now clear for me. But i scared about reverse current order. It looks like a painful job. Is there any detailed info and/or reliable sample about it?

      Thanks,
      Aytac

      Comment


        #4
        We do have samples covering each of the four steps.

        Saving variables to a class is a simple enough topic to provide a sample here for,

        Code:
        [FONT=Courier New]
        private int Depth;
        protected override void OnBarUpdate()
        {
            Depth = Position.Quantity == 0 ? Depth : Position.Quantity;
        }[/FONT]
        Placing unmanaged orders is covered here,


        Finally, there are examples for verifying orders have been submitted here,
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hi Jessica,

          I have reviewed before the links that you pointed. But i do again. But don't understand first step which is you said simple enough.

          1. Record your market depth in a variable
          >>> I think my market dept mean is right now what is my current position size/quantity. If so why i'm not only use Position.Quantity. Purpose is use same quantity of closed position for the very next order? or because of close position order may fill patrial and we have to track this situation?
          2. Place a sell or buy-to-cover order to close your position
          >>> We'll use SubmitOrderUnmanaged method for close current position
          3. Confirm this order has been closed
          >>> We'll check if position fully closed within OnExecution event.
          4. Place a buy or sell-short order to open a new position with the information you recorded earlier
          >>> We'll use SubmitOrderUnmanaged method for open an opposite position.

          Thanks,
          Aytac

          Comment


            #6
            I am happy to clarify further.

            I think my market dept mean is right now what is my current position size/quantity. If so why i'm not only use Position.Quantity. Purpose is use same quantity of closed position for the very next order? or because of close position order may fill patrial and we have to track this situation?
            The purpose of this is to use the same quantity as your previous position to place your next order. The reason we can't read Position.Quantity "live" is because this variable will become zero when your position is closed. This is why we're keeping track of what it was when your position was open.

            Please let us know if there are any other ways we can help.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              I see Jessica,

              So first step is unnecessary for me because i have money management rules and position size changing per trade. Position size is not fixed instead variable. After close current position, reverse position size may increase (because previous trade close with profit) or may decrease (because previous trade close with loss) or maybe the same (profit/loss not so big and can not effect the size). Anyway i understand you completely.

              About third step. I modify managed strategy code to unmanaged. And don't check current position is closed or not. And test it on market replay, see the strategy working correct. According to sample code which is below;

              Code:
              			if (CurrentBar < BarsRequiredToTrade)
              				return;
              			
              			if (Position.MarketPosition == MarketPosition.Long)
              			{
              				if (BullsInControl)
              				{
              					SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "Exit Long");
              				}
              			}
              			else if (Position.MarketPosition == MarketPosition.Short)
              			{
              				if (BearsInControl)
              				{
              					SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Market, Position.Quantity, 0, 0, "", "Exit Short");
              				}
              			}
              			
              			if (Position.MarketPosition == MarketPosition.Flat)
              			{
              				if (BullsInControl)
              				{
              					var quantity = CalculateQuantity(...);
              					if (quantity > 0)
              					{
              						SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, quantity, 0, 0, "", "Long Entry");
              					}
              				}
              				else if (BearsInControl)
              				{
              					var quantity = CalculateQuantity(...);
              					if (quantity > 0)
              					{
              						SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Market, quantity, 0, 0, "", "Short Entry");
              					}
              				}
              			}
              But when the subject is real time trading i think strategy won't work as expected. Because strategy working Calculate.OnBarClose and "close current position" will take time, so i can not open opposite position or maybe late one bar and reverse position will be open next bar close.

              The first solution comes to my mind is run strategy on each tick or price change. Are you have any recommendation about that? Maybe i have to open opposite order inside OnExecuteOrder event after see full filled close current position order?

              Thanks,
              Aytac
              Last edited by aytacasan; 03-08-2017, 08:58 AM.

              Comment


                #8
                In situations like this I highly recommend creating a multi-timeframe strategy, and using a "trade clock" series to place trades on. This will ensure all your trades are placed immediately, rather than your having to wait for the next bar. As an aid, I am providing a very basic and stripped down strategy which uses this approach.
                Attached Files
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  In your sample strategy working OnBarClose but using 1 tick secondary data series. Is this mean it will work same as strategy working OnEachTick, am i right?

                  But i think you mean that; if we have strategy that working 5 min chart and working on bar close. Then we can add more sensitive data series (for example 1 min secondary data series) for sending orders and still we can use 5 min for trade decisions. This way;

                  1. Strategy will work with better performance vs strategy that working on each tick.
                  2. We have chance for manage orders five times not once according to 1&5 min data series.

                  I hope i understand you correctly.

                  Comment


                    #10
                    You are correct on both counts. Doing things this way will give you some of the benefits of an OnEachTick strategy, while still allowing you to write a better performing OnBarClose strategy.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Dear JessicaP

                      I was really interested in checking your example as I was myself having the same problem aytacasan had. I have tested your sample code "Example1673820" and I have a few questions if I may.

                      If you are checking your position as well as a condition trigger for the trade, i.e.
                      ================================================== ===========
                      if (Position.MarketPosition == MarketPosition.Short && CrossAbove(smaFast, smaSlow, 1))
                      ExitShort(TradeClock, DefaultQuantity, "", "");
                      if (Position.MarketPosition == MarketPosition.Flat && CrossAbove(smaFast, smaSlow, 1))
                      EnterLong(TradeClock, DefaultQuantity, "");
                      if (Position.MarketPosition == MarketPosition.Long && CrossBelow(smaFast, smaSlow, 1))
                      ExitLong(TradeClock, DefaultQuantity, "", "");
                      if (Position.MarketPosition == MarketPosition.Flat && CrossBelow(smaFast, smaSlow, 1))
                      EnterShort(TradeClock, DefaultQuantity, "");
                      ================================================== ===========
                      The problem is that Position.MarketPosition only changes onbarupdate of the main bar, even when you are sending the orders to the secondary bar 1. Hence, it will not change to Flat on the same onbarupdate. Plus, on the next onbarupdate when you are indeed flat the cross is not longer true as it happened on the previous bar.
                      I do not understand how your example solves aytacasan's problem.
                      My only solution is to change the strategy to Calculate.OnEachTick and changing the logic to check one previous bar.

                      Thank you for your help

                      Comment


                        #12
                        Thank you for your questions bertochi. As I understood aytacasan's question, we wanted to make trade decisions at the close of every bar, but we also wanted to be able to make sure that those trades were executed before the next bar closes. In this situation, Position.MarketPosition updating at the close of a bar is adequate.

                        It sounds like you are encountering a different situation, where you need feedback as far as when your position is updated intra-bar, but would still like to avoid using Calculate.OnEachTick. If this is the case, I would like to recommend using OnExecutionUpdate to detect updates on trades, and to store this information in a variable in a manner similar to that in post 4. You can learn more about OnExecutionUpdate, with code samples available, here

                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you for your reply.

                          I am indeed using OnExecutionUpdate as well as OnPositionUpdate to trigger a Long or Short, after an exit has taken place. But working with TD Ameritrade there are issues, such as orders showing as part filled when they are actually fully filled, which your support team are fully aware and are working to to hopefully fix this in the hopefully not so distant future. This TDA/NT bug is being tracked as NTEIGHT-9838.
                          At the moment the only Manual fix to the TDA issues is to restart NT8 as well as resetting the database to get rid of those orders that were showing as part filled or working but that were actually fully filled.
                          Never mind about this, I was only interested in your code example.
                          Thank you

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          4 responses
                          21 views
                          0 likes
                          Last Post alifarahani  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          16 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by PhillT, Today, 02:16 PM
                          2 responses
                          7 views
                          0 likes
                          Last Post PhillT
                          by PhillT
                           
                          Started by Kaledus, Today, 01:29 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by frankthearm, Yesterday, 09:08 AM
                          14 responses
                          47 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Working...
                          X