Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to distinguish state between backtesting and realtime?

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

    How to distinguish state between backtesting and realtime?

    Hi there,
    I have a line that sends telegram message when an order is executed.

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (State == State.Realtime)
        {​
        SendMessage(execution.Order.Name + " @"+price.ToString()+ "\r\n" + Time[0]);
        }
    }

    I want to prevent it to send every msg when backtesting. I just want to get msg when I run this strategy in realtime trades.
    That's why I put the line (State == State.Realtime) but apparently it doesn't work.

    How can I distinguish the strategy's state b/w backtesting and real-time so that I can put logic differently by the state?
    Last edited by rocketman7; 04-24-2024, 02:17 AM.

    #2
    Hello rocketman7,

    Checking the State property would be correct. OnExecutionUpdate() runs when an order fills (which may not be when it was submitted).

    Are you printing this property and seeing a state of historical in the output window?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,
      Thanks for the quick response.

      I guess if (State==State.Realtime) condition works in the OnExecutionUpdate Class. But I also have this condition in the OnBarUpdate override as there should be where positions are entered and it doesn't work. If I run backtest the strategy, all printings in the (State == State.Realtime) in the OnBarUpdate() are printed in the output windows. So if I put SendMessage Line there, every telegram message from past backtesting are sent.

      Thanks,

      Comment


        #4
        Hello rocketman7,

        That may be due to the conditions.

        Print the State property at the top of OnBarUpdate() above any conditions.

        Print(string.Format("{0} | State: {1}", Time[0], State));

        Temporarily comment out all other prints. Compile. Clear the output window and re-run the script.

        Save the output to a text file (right-click the output window, select Save as), and attach the output text file with your next post.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Can't test on the realtime, but when I run backtesting after adding the line you provided, it shows "State: Historical".

          Comment


            #6
            Hello rocketman7,

            This means it's working correctly. Backtest is historical.

            Now that we know the property is functioning correctly, print the time of the bar and all values used in your condition along with labels.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Oh, thanks a lot! Now it works on the other lines.
              It definitely didn't work before, so is it fixed because the line you told me to add? Should I leave that line at the top of OnBarUpdate()?

              Comment


                #8
                Hello rocketman7,

                The print is not changing anything. This is informing you of what is.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                52 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                130 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                70 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                44 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                48 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X