Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to close the fake position from historical data in live trading

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

    How to close the fake position from historical data in live trading

    I am trying to implement my strategy live.

    I loaded data series of 1 minute for 360 days. As I enabled the strategy, the system applied my strategy to all the data loaded and the last position is a long entry order. However, since this is on the historical data, this should be a fake position only for me to see how my strategy works. Going forward, the data is live. however, my strategy still tries to close that position when all conditions are met even though it is a fake one. this resulted in my order getting rejected and my strategy got disabled.

    How should I fix it? Is there a way to cancel the last open position in the historical data?


    #2
    Hello wyifei6688,

    Thank you for your post.

    When a strategy is enabled, the platform keeps track of the Strategy Position and the Account Position in order to follow the start behavior setting for syncing these positions. For more details:If you want to avoid having a historical position altogether, one option is to program your strategy to only process its logic on realtime data. This can be done as simply as adding the following to the beginning of OnBarUpdate():
    Code:
    if (State== State.Historical)
    return;
    Otherwise, you could consider setting your strategy start behavior to "Adopt account position" to ignore the historical virtual position and start with the account position instead:


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

    Comment


      #3
      Any way to achieve this when using Strategy Builder, to avoid reading the historical data when enabled?
      My new strategy always shows a Virtual position when enabled, and no matter what I do it doesn't get closed.

      1. I used StartBehavior = "Wait Until Flat" But that doesn't help.
      The closing orders aren't accepted, because of below error.

      ===
      00:51:00 SET4PBLong2
      4/4/2025 12:51:01 AM Strategy 'My01STTrade/348430100': Entered internal SubmitOrderManaged() method at 4/4/2025 12:51:01 AM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=18512.25 StopPrice=0 SignalName='tL1' FromEntrySignal='iL1'
      4/4/2025 12:51:01 AM Strategy 'My01STTrade/348430100': Ignored SubmitOrderManaged() method at 4/4/2025 12:51:01 AM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=18512.25 StopPrice=0 SignalName='tL1' FromEntrySignal='iL1' Reason='SignalName does not have a matching FromEntrySignal to exit'
      4/4/2025 12:51:01 AM Strategy 'My01STTrade/348430100': Entered internal SubmitOrderManaged() method at 4/4/2025 12:51:01 AM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=-6.25 SignalName='sL1' FromEntrySignal='iL1'
      4/4/2025 12:51:01 AM Strategy 'My01STTrade/348430100': Ignored SubmitOrderManaged() method at 4/4/2025 12:51:01 AM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=-6.25 SignalName='sL1' FromEntrySignal='iL1' Reason='SignalName does not have a matching FromEntrySignal to exit'​
      =======

      2. I tried StartBehavior = "Immediately Submit"
      This does do immediately submit, but then the virtual and real-time tracking are out of synch. The newly submitted 'Buy to Open' real orders actually end up 'Buy to Close' existing open Virtual short positions. Then the real 'Sell To Close' ones result in 'Sell to Open' for virttual tracking. Completely messing up.

      Is going the programming/coding way only way to avoid getting stuck with Historical/virtual open positions as suggested here and many other threads that I have seen. Please advise.

      Comment


        #4
        Hello pankaj_future,

        Using the Strategy Builder, the only choice would be to only allow orders in real-time.

        In the Conditions for entry orders select Misc > Current state on the left, Equals in the Center, Misc > State, set to Real Time.


        If you want to exit the position on the last historical bar the script would have to be unlocked and coded by hand.

        if (State == State.Historical && CurrentBar == Count - 2)
        {
        ExitLong();
        ExitShort();
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Great! "State=State.Realtime". This is exactly what I was looking for. Have added that condition now, it looks to be working for now. thanks.

          Comment


            #6
            This workaround works superb for real time.

            But now have a new problem - with this condition, the Strategy does not run on Strategy Analyzer. I do not understand whether the Strategy Analyzer trades are considered Virtual or Real, but it is not able to deal with those open virtual positions it seems.

            I tried removing the State=Realtime condition before running the Strategy analyzer - but it then faces the virtual position problem again, it executes only one side of Trades (Shorts only)

            My Strategy without this condition always ends-up with a Short virtual position. Any help to exit all virtual positions before the strategy the real time data ?

            Comment


              #7
              Hello pankaj_future,

              Yes, by preventing orders in historical (backtest) no orders will appear in the strategy analyzer.

              Note, I've already suggested to exit the open historical position on the last historical bar. It would be up to you to decide to actually do that.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              55 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              37 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              17 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              19 views
              0 likes
              Last Post TheRealMorford  
              Started by Mindset, 02-28-2026, 06:16 AM
              0 responses
              49 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Working...
              X