Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy works in strategy analyzer but doesn't trade in Market replay

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

    Strategy works in strategy analyzer but doesn't trade in Market replay

    I'm helping a client with a strategy and can't get it to trade in Market replay for some strange reason. In strategy analyzer it works like intended.

    Once I pop it on the chart, it shows previous trades, trailing, exits and the output shows print messages accordingly, but once the chart starts to move it just doesn't take a single trade and it's like no signals at all are working.
    Click image for larger version

Name:	works.jpg
Views:	621
Size:	37.4 KB
ID:	1252771

    It's based on an indicator that shows the large B in the picture and the indicator works in Market replay. I even tried using the indicator code inside the strategy but I get the same results. Working in analyzer but not in playback. The SampleMAcrossover works (that thing always does..).

    I've narrowed the problem down to bullSig / bearSig never becoming TRUE even though the indicator works in market replay. But when I enabled the strategy, previous Prints in the output shows that the signals work like intended.

    Click image for larger version

Name:	output.jpg
Views:	524
Size:	13.0 KB
ID:	1252772

    I use this code in OnBarUpdate:

    Code:
                
    if (Position.MarketPosition == MarketPosition.Flat )
                {
                    if (bullSig[0] )
                    {
                        EnterLong(_posSize, LongName);
                    }
                    else if (bearSig[0])
                    {
                        EnterShort(_posSize, ShortName);
                    }
    
                }
                else if (Position.MarketPosition != MarketPosition.Flat)
                {
                    //trailing stops
                    if (BarsSinceEntryExecution() > 0)
                    {
                        MoveTrailingStop();
                    }
                }​
    This is run next:
    Code:
            //this gets called when an order gets filled on the exchange
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                //If order is opened and filled, set stops
                if (IsEntryOrderFilled(execution))
                {
                    //Set stop and profit
                    SetInitialStops(execution);
                }
            }
    
            private bool IsEntryOrderFilled(Execution execution)
            {
                return execution.Order.OrderState == OrderState.Filled;
            }​

    #2
    Hello Borgen,

    From the details it sounds like your script is working historically but is working differently in realtime. To better understand that you would need to add more prints to see what specifically is happening different between those two modes.

    If the bullSig and bearSig are related to the problem you would have to look at the code surrounding how those two series are being set.

    Its possible that scripts work differently in realtime and historical for a variety of reasons, one common reason would be using a different Calculate setting. Historical always runs OnBarClose and in realtime you can also use OnEachTick or OnPriceChange which may affect the outcome.

    Comment


      #3
      Borgen did the Log have anything to say when executing?

      Comment


        #4
        No, the Log is empty unfortunately, Trace orders doesn't give out anything either. I have it set to OnEachTick since I want stops to move fast when trailing.
        I run the indicator code inside the strategy now and it is just a normal series, nothing out of the ordinary. Then it just looks for the setups and change the index to True when it finds it.
        Am I missing some setting or syntax for historical versus Real-time here?

        Code:
              
        if (State == State.DataLoaded)
        {​        
               bullSig = new Series<bool>(this, MaximumBarsLookBack = MaximumBarsLookBack.Infinite);
               bearSig = new Series<bool>(this, MaximumBarsLookBack = MaximumBarsLookBack.Infinite);​

        Comment


          #5
          Hello Borgen,

          That is how you define a series, that needs to be in data loaded. I would still suggest adding prints to OnBarUpdate to find what line is having an error.

          Comment


            #6
            Yeah, I've pretty much used Prints all over the place. I noticed that when I placed a Print inside a
            "if (IsFirstTickOfBar)​" it doesn't work (I used that inside OnBarUpdate according to what the documentation says)
            .
            The indicator works flawlessly and I have a built in debugger in it so I know the values in it are ok. There is just something with going to playback that messes things up with the strategy. I've been troubleshooting this thing for so long now that I'm beginning to think it is something corrupted in the platform. I updated to version 8.0.28 the other day and it wouldn't be the first time I've had to re-install due to the platform acting up.

            Comment


              #7
              Hello Borgen,

              Unfortunately there are not enough details here to know what the problem would be, to find the answer would require that you continue to debug the script to find the specific problem.

              It is common that a script will perform different in historical vs realtime for a variety of reasons, that can involve the selected settings for the strategy and also how the logic is coded. Without stepping through the code by using prints it would be impossible to say what the specific cause is that makes the series you used work differently in realtime. Investigating where you set the series would be the first step to find out why its not being set to the expected value.

              If your IsFirstTickOfBar condition is not working that may be due to where you have that in your logic, I would suggest to make a more simple test where you are only using that condition in OnBarUpdate without any other code to confirm its working as you coded it.


              Comment


                #8
                I don't know what details you want though.
                If I have this line of code running from the indicator it works, but if I run it from OnBarUpdate inside the strategy it doesn't.
                Code:
                protected void testing()
                {       
                            if (IsFirstTickOfBar )
                            {
                                Print("Test");
                            }​
                }
                * If I add the indicator to Market replay it shows signals AND generates new ones.
                * If I add the indicator in the strategy using AddChartIndicator, it shows Historic signals but doesn't generate new ones.
                * If I add the indicator to the strategy using "indicator = myIndicator(insert params here); " and call the public series index for signals, it shows Historic signals but doesn't generate new ones.

                * If I run the strategy on Historical data it takes trades
                * If I run the strategy on Market replay it doesn't.

                Can you explain why the indicator works in market replay/real time, but if used inside a strategy it doesn't? Do i need to specify anything about real-time/historical data?
                Can you explain what the difference is between running the strategy in historical data and why it would work there (Not just the Calculate.OnBarClose)?

                I'm not an expert on your platform and what the differences are between playback mode versus strategy analyzer, but I've coded hundreds of trading bots for clients on other platforms without having issues like what I'm having with Ninjatrader.
                If I run a backtest in Metatrader for example it works just the same as in running it on real-.time. I've gone through your documentation to see if I can find anything about this issue but failed to find anything. This is very frustrating and troubleshooting with print statements doesn't give out any clues here.

                Comment


                  #9
                  Hello Borgen,

                  When you post a piece of code like you had there are not any details on how that is executing in contrast to the rest of your script. At best I would be able to make observations like you used NinjaScript syntax and there are not any obvious C# syntax issues. To know how that code actually works would require to run the script and use prints to observe how its working.


                  If I have this line of code running from the indicator it works, but if I run it from OnBarUpdate inside the strategy it doesn't.
                  Did you put that as the first line in the strategy also? If you have that later in your code your earlier code may be blocking that condition from being run.

                  Can you explain why the indicator works in market replay/real time, but if used inside a strategy it doesn't? Do i need to specify anything about real-time/historical data?
                  ​No, this is something you would need to find out by adding prints into your logic to observe how the strategy and indicator process in each test. Looking at a piece of code which is only a part of the overall code is not enough information to know how it would run in each use case. Part of the strategy development process is debugging the script in the use cases where you want to use it. If you see differences you would at that point need to add prints to help let you see what the strategy is seeing to know why those results are different.

                  Can you explain what the difference is between running the strategy in historical data and why it would work there (Not just the Calculate.OnBarClose)?
                  ​A strategy running in historical data uses just the OHLC of the bar, there is no granularity. That always runs OnBarClose. In real time you have 3 options, OnBarClose, OnPriceChange or OnEachTick. If you use either of the two other options in realtime the bar has more granularity which can change how your code executes depending on what you programmed.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, 03-13-2026, 05:17 AM
                  0 responses
                  86 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  151 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  79 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  52 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  59 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X