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

Avoid a naked position

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

    Avoid a naked position

    I'm trying to write a managed strategy that behaves similarly to an ATM strategy, with multiple stop loss targets, trailing frequencies, etc. In the process of debugging I've noticed that when the strategy transitions from historical to real time it can run into a scenario where the position is not flat, but there is no stop loss order. Basically a completely naked position.

    I've used several of the sample order management strategies from the forum to build my strategy, and I'm keeping a local copy of the stop loss order.

    i.e.

    private Order stopOrder = null; // This variable holds an object representing our stop loss order.


    I want to write a separate method to check for a stop loss order when the position is not flat. The method will go beyond me just keeping a copy of the most recent stop order. It will be able to make sure there is a stop loss order even if the strategy's stopOrder object is null for some reason. Basically I want 100% insurance that I will always have a stop order in place if the instrument's position is active.

    My assumption is that I will have to enumerate something in the Account object.

    Thank you!

    #2
    Hello bojim,

    Welcome to to the NinjaTrader forums!

    The transition may need to use GetRealtimeOrder() in OnStateChange when State is State.Realtime.
    https://ninjatrader.com/support/help...ltimeorder.htm

    The order variable or array holding the order should be assigned from the order argument in OnOrderUpdate.
    https://ninjatrader.com/support/help...rderupdate.htm

    An example of this controlling order exit prices and shows orders being assigned from OnOrderUpdate. (This always starts real-time flat so does not implement GetRealtimeOrder())
    https://ninjatrader.com/support/foru...269#post802269


    If you want to check if there are working orders from the strategy position, these would need to be saved to a variable or array/list. This could be checked from OnPositionUpdate().
    https://ninjatrader.com/support/help...tionupdate.htm

    But there is an <Account>.Orders collection of working on orders you can loop through. This would be all orders on the account, from all scripts, and manual orders.
    https://ninjatrader.com/support/help...rs_account.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the response. I spent many hours on the forum before asking the question. It sounds like I did not phrase it well enough.

      First of all I am using the GetRealtimeOrder which in this particular this case it is returning null, and secondly I am storing the stop order object during the OnOrderUpdate which again in the specific scenario is not firing for the stop order because it's not there.

      To simplify the question in hopes of you providing the example that will help me and hopefully others in the future.

      How am I supposed to handle the issue of a strategy starting up with a position already in the market with no stop loss order applied to it?

      The entry was from the strategy, but for what ever reason the strategy restarts while not flat, and the original stop order was canceled. How can I detect that there is no stop loss order and it back so that I don't get stuck with a live position with no stop loss applied to it? This does not happen often, but I'm just trying to be extra careful.

      As side note, I'm looking the at the Account.Orders collection now to see if I can get what I need out of it.

      Thank you
      Last edited by bojim; 08-06-2021, 01:04 PM.

      Comment


        #4
        Hello bojim,

        Order objects should be assigned in OnOrderUpdate as best practice. For a complete demonstration for using Order objects, and using GetRealtimeOrder, I suggest modelling after the SampleOnOrderUpdate strategy.

        SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

        How am I supposed to handle the issue of a strategy starting up with a position already in the market with no stop loss order applied to it?

        The entry was from the strategy, but for what ever reason the strategy restarts while not flat, and the original stop order was canceled. How can I detect that there is no stop loss order and it back so that I don't get stuck with a live position with no stop loss applied to it? This does not happen often, but I'm just trying to be extra careful.
        This would involve the start behavior and what the strategy processed with historical data. WaitUntilFlat would be used to start the strategy so it enters live from an entry order when the strategy is in a flat position. (The account should be flat.) ImmediatelySubmit would be used to have a strategy pick up where it left off, assuming the account and strategy position are where you expect them.

        You would want to use ImmediatelySubmit to have a strategy resume a position it was previously managing, and in order for proper target and stop to be placed/resumed, the strategy must be able to calculate the target and stop from historical data. If the active historical orders match the live orders, they will resume, otherwise, the live orders will be cancelled and replaced with the active historical orders. If you use ImmediatelySubmit and all that happens is the live orders cancel, this would mean that the strategy did not calculate orders from historical data, and the logic will need to be debugged to find out why.

        I've included some more general information on start behaviors below. Be sure to review the documentation pages for complete detail.

        Start Behavior general information

        When a strategy is enabled, it processes historical data to determine trades that the strategy would have made on the data that is already on the PC/chart and to determine what position the strategy is in. (Strategy positions are separate from actual Account positions.)

        Wait Until Flat will wait until this virtual/historical position is closed before live orders can be submitted. It ensures that the strategy starts trading live from a flat position. If you enable the strategy when the account is flat, the strategy will waiting until this position calculated from historical data is closed so it is logically making trades starting from an entry signal.

        Immediately Submit automatically submits working orders from when the strategy processed historical data, and assumes the strategy position and account position are where you want it when you enable the strategy. This is typically used to have a strategy resume a position after disabling/enabling. If the strategy already had live orders running, the orders will resume with the new enablement of the strategy if they match the historically calculated orders. If the orders calculated from historical data do not match the live working orders, the live working orders will be cancelled and replaced by those calculated from historical data.

        Sync Account Positions is an additional option that has NinjaTrader submit an order to sync the account position to the position calculated by the strategy. (Not the other way around.)

        Adopt Account Position would be used if you want the strategy to inherit the Account Position on enablement. This requires additional programming.

        If you do not want the strategy to calculate a position from processing historical data. Simply add if (State == State.Historical) return; to the top of your strategy logic so historical processing is skipped. The strategy will then always start from a flat position because it has not calculated any orders.

        Strategy vs. Account Position — https://ninjatrader.com/support/help..._account_p.htm

        Start Behaviors — https://ninjatrader.com/support/help..._positions.htm
        JimNinjaTrader Customer Service

        Comment


          #5
          This is the information I needed, thank you!

          Comment


            #6
            I am still struggling with this topic.

            I'm sorry if I'm repeating myself, but what is the recommended approach when a Strategy has entered the market and is managing a position with a stop order in place, and that strategy restarts for whatever reason? I can't seem to find anything that fits my needs.

            Currently in testing, if I simply press F5 in a chart while the strategy is Long or Short,
            • The actual market position stays Long or Short in the market
            • The existing stop order disappears.
            • The new instance of the strategy has a Market Position of Flat even though the instrument still has a position in the market.

            I do not think I want to use Immediately Submit because it will enter at the wrong time, unless there is a trick to using it that I have not figured out.

            Attached is my base class for handing OnOrderUpdate and OnExecutionUpdate.

            I would like to be able to restart the strategy and have it at least keep the existing stop loss. If that's not possible, then I would like to flatten the existing position so that it's not left in the market with nothing managing its risk.

            Thank you!
            Attached Files

            Comment


              #7
              Hello bojim,

              If you are wanting to disable a strategy and re-enable a strategy, 1 tick intra-bar granularity with the Immediately Submit start behavior is necessary.

              Below is a link to a forum post that goes into detail.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Immediately Submit does not work for me because it causes other behavior that I don't want.

                It seems like this could have been so much easier if you simply offered your users a start up option such as Wait Until flat / Don't Cancel Existing Stop loss.

                Your current approach to this issue is overly complicated compared to other trading systems I've used.

                I'll figure something else out on my own.

                Comment


                  #9
                  Hello bojim,

                  Thank you for your reply.

                  Chelsea is currently out of the office, however, I have put in a feature request to have a start behavior of "Wait Until Flat, Leave account orders" added to the platform. This would function as follows: if there are pending orders and account positions, strategy waits until account is flat, there are no pending orders, strategy is flat, and sees an entry order to enter a position. This request is being tracked under the number SFT-5311.

                  As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted in the Release Notes page of the Help Guide.

                  Release Notes — https://ninjatrader.com/support/help...ease_notes.htm

                  We would also note that that if no stop was submitted and the strategy calculated flat after reloading when using Immediately Submit, it means the strategy did not calculate that order/position from historical data and debugging the strategy's historical processing would get the strategy to submit the stop loss and recognize its position.

                  Please let us know if we may be of further assistance to you.

                  Kate W.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Tim-c, Today, 03:54 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post Tim-c
                  by Tim-c
                   
                  Started by FrancisMorro, Today, 03:24 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post FrancisMorro  
                  Started by Segwin, 05-07-2018, 02:15 PM
                  10 responses
                  1,770 views
                  0 likes
                  Last Post Leafcutter  
                  Started by Rapine Heihei, 04-23-2024, 07:51 PM
                  2 responses
                  31 views
                  0 likes
                  Last Post Max238
                  by Max238
                   
                  Started by Shansen, 08-30-2019, 10:18 PM
                  24 responses
                  945 views
                  0 likes
                  Last Post spwizard  
                  Working...
                  X