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

Close Historical Order Manually

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

    Close Historical Order Manually

    I've added a button to the chart on my Unmanaged strategy to force the strategy to go flat. The button works, but when the strategy starts with a historical position and I click the button to close the historical position, it doesn't do it until the next bar closes. This is not the case for Live orders.

    I've attached a small strategy to illustrate the problem:

    Start the strategy on a 1 minute chart with the Bar timer indicator.
    The strategy has an open historical long position when it starts.
    Click the Exit button while keeping an eye on the market position in the strategies tab on the control center
    You will observe that the strategy does not go flat until the bar closes

    I've added another button to submit a live order to show that this behavior does not occur with live orders.

    Attached Files

    #2
    Thanks for your post kevinenergy, and thank you for providing the concise example.

    This looks to be expected behavior for simulated order fills. Since the strategy is using Calculate.OnBarClose, the simulator will be set to process orders on bar closes. If we change the Calculate mode to OnEachTick, then we can see the simulated order fill to close the virtual/historical position on the next tick.

    Since changing the Calculate mode will change the indexing and processing of the strategy, you could use IsFirstTickOfBar and BarsAgo references of 1 therein to mimic the same behavior as Calculate.OnBarClose. I've included a link below for the thread's reference.

    IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

    If you have any additional questions, I'll be happy to assist you further.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim, thanks for the response.

      I've tested my sample strategy on the Simulator account Sim101 - when I click on the Long/Exit buttons on I'm able to submit and close Realtime orders and these get executed immediately. There is no waiting for the bar to close. It seems that it is just the Historical strategy position that can't be closed mid-way through the bar.

      I also tried adding the following, to see if I could close the Historical Position before realtime processing, but this had ZERO effect. The order was totally ignored.

      Code:
      if (State == State.Transition && Position.MarketPosition != MarketPosition.Flat)
      {
            SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, 1, 1, 1, _ocoId, "ClosePosition");
      }
      I also tried TriggerCustomEvent, but this didn't work either.

      I'm afraid CalculateOnEachTick is overkill, for this requirement. I will just wait till the bar closes. But I would question whether this really is expected behavior, because once the strategy is finally flat, the buttons work in realtime without requiring the bar to close before they submit. Why is the behavior different when there is an open historical position?




      Comment


        #4
        By the way... remember you were helping me with market orders and stop orders being filled simultaneously a couple of months back, in this thread:



        You will see in the example strategy above that in my Exit market order I am using the same OCO ID as my target and stop. This is what the Managed approach should be doing under-the-hood to protect us from overfills etc - but it does not!! I've had to implement an unmanaged strategy solely to get overfill protection for strategy exits; no other reason.

        Comment


          #5
          Hello kevinenergy,

          I'm having trouble understanding the main goal here. If you want to the strategy to immediately start submitting orders with realtime data right away, why not use ImmediatelySubmit, AdoptAccountPosition, or have the strategy ignore historical processing with if (State == State.Historical) return;? Personally, I think advising the user about the position and then setting an option to ignore historical processing and restarting the strategy would be the best way forward.

          It certainly is possible to close the position manually from a button, and you can still do so on the first bar if you use a different calculate mode. If you check for IsFirstTickOfBar, then strategy actions will be done the same as they would have with OnBarClose without additional processing for each tick. This approach is really working around the designed start behaviors. If we can make a good case for why this approach is necessary over the approaches mentioned above, we can certainly bring up a question with Development regarding the closing the position before the first bar closes with OnBarClose.

          To address post #4, there several situations that can occur due to an overfill, and generally how these should be handled would depend greatly on how the strategy works, which is why IgnoreOverFill and your own overfill handling are reserved for the Unmanaged Approach. We also have tracked your interest for using OCO with the Managed Approach with SFT-3564. Is there something else that you wanted us to note for that feature request?

          I look forward to being of further assistance.
          JimNinjaTrader Customer Service

          Comment


            #6
            Hey Jim,

            Sorry for the late reply.

            Basically, the use case I'm talking about is as follows: I want to switch my strategy on, and take a look at the state-of-play in the markets, and then make an educated decision about whether to immediately get into a position or not. Or alternatively go flat, and have my strategy take the next "Entry Signal" without needing to wait for the "Go Flat" signal first. I'm not quite ready for ImmediatelySubmit/Adopt account position etc.... I'm still working with a degree of manual intervention - particularly at the start. It s frustrating that all forms of manual intervention are locked out until the close of the first realtime bar but after that first bar everything works as expected without requiring bars to close. I also found that the requests got queued up and I ended up with lots of positions on the close of the first realtime bar.

            Yes I realise that CalculateOnTick is an option, and I note your point about checking for first tick to ensure that the processor load doesn't spike needlessly but this seems like a hack to get my strategy to get flat - I don't need tick data and its a pretty big change if all my bar indexes are going to change by +1. So would rather live with the delay for now.

            So that is my use case If you feel that this is a good enough reason, feel free to raise the question with development. I do believe that if real orders and positions can be altered mid-bar while State is Realtime, that Historical ones should be too. For now I can live with it.





            Comment


              #7
              Hello kevinenergy,

              I had put a question forward internally noting your use case, and this has been confirmed to be expected for OnBarClose and closing the historical position before the first realtime bar closes.

              I have opened a feature request to consider changing this in the future, and that feature request ticket ID is SFT-3673.

              For suggested work arounds, I would recommend the following:
              1. Use OnEachTick and IsFirstTickOfBar with BarsAgo 1 references
              2. Change your workflow to enable the strategy on the Sim101 account to see how the strategy will process historical data, submit an order to your live account, and then use AdooptAccountPosition to adopt that position when enabling the strategy on the live account.
              If there is anything else I can do to help, please let me know.
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by chbruno, Today, 04:10 PM
              0 responses
              3 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              6 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by frslvr, 04-11-2024, 07:26 AM
              9 responses
              127 views
              1 like
              Last Post caryc123  
              Working...
              X