• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Close Historical Order Manually

  • 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

    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


      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.

      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?


        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.


          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


            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.


              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


              Latest Posts


              Topics Statistics Last Post
              Started by Mberg, Today, 05:13 PM
              0 responses
              Last Post Mberg
              by Mberg
              Started by Barry Milan, Today, 01:33 PM
              2 responses
              Last Post marcus2300  
              Started by NW Trader, Today, 03:42 PM
              2 responses
              Last Post NW Trader  
              Started by ralett, Today, 01:42 PM
              1 response
              Last Post NinjaTrader_ShawnB  
              Started by dkbyond, Yesterday, 04:11 PM
              1 response
              Last Post NinjaTrader_ChrisL