Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Print() within OnStateChange() when State.Terminated

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

    Print() within OnStateChange() when State.Terminated

    In NT7, I used to display various custom metrics after a backtest had completed or a live strategy had been stopped. I achieved this by including Print() within OnTermination(). Upon completion of a backtest and stopping a live strategy, it behaved the same way.

    In NT8 though, it seems that this has a different behaviour.

    Code:
    			else if (State == State.Terminated)
    			{
    				Print(TestVariable);	
    			}
    Within the strategy, TestVariable is incremented several times and should end up being non-zero when the strategy is completed.

    Firstly, upon compiling, this seems to be called 3 times.

    Then in backtesting, after clicking Run, this is called once before the backtest starts. When it does this, it seems to print the value that TestVariable had after a previous backtest (i.e. non-zero). At the end of the backtest, this is called again (as expected this time), but prints a zero value (when it should have been non-zero).

    When a strategy is run live however, no problems. No value is printed when the strategy is enabled (unlike when the backtest was run). After disabling the strategy, it returns a non-zero value - perfect.

    So I'm not sure if this is a bug but going from what I was able to do in NT7, and the consistency between behaviours of Print() in OnTermination() between backtesting and live strategies, it seems to me that there might be an issue using Print() in OnStateChange() when State.Terminated in backtesting only.

    #2
    State.Terminated does indeed work a bit differently from OnTermination() as you have observed.

    The behavior you report is expected. You'd see that behavior if you're running the strategy live from a chart as well (correct me if I'm wrong). This is needed to build/clean up resources for the propertygrid. You would not see this number of Terminated calls if you're just operating on the strategies grid.

    What you can do though to resolve that, is only print the value in State.Terminated if the strategy is "IsEnabled" (which it will be throughout the lifetime of the backtest, up until after it is terminated)

    Code:
    if (State == State.Terminated)
    {	
    	if(this.IsEnabled)
    		Print(TestVariable);	
    }
    However IsEnabled is a UI property, so from your strategies grid, that would never get hit. You may need to implement your own flag to track the status of the strategy throughout it lifetime

    Code:
    private bool hasRun = false;
    private int TestVariable = 0;
    
    protected override void OnStateChange()
    {			
    	if ( State == State.Configure)
    	{
    		hasRun = true;	
    	}
    	else if (State == State.Terminated)
    	{	
    		if(hasRun)
    		{
    			Print(TestVariable);
    		}
    	}
    }
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hi,

      Thanks for the solutions. I've tried both of your suggestions, and have integrated both of them into my strategy so regardless of running the strategy from a chart (where IsEnabled works) or from the strategy grid (where using the hasRun variable works), it manages to print the variable.

      However, it's still not clear how I can get this to also print on backtests. For backtests, it seems like the code within IsEnabled is executed, but it prints right after clicking "Run" (i.e. before the strategy begins backtesting). In addition, it also seems to print the value from the previous backtesting run, and doesn't seem to print anything upon completion of the backtest.

      Thanks again for your help.

      Comment


        #4
        I see - there are still cases where State.Configure will be called to clean up the resources. A better suggestion in this case would be to declare that hasRun as true once it has hit DataLoaded, which should insure that bool does not get set until the backtest is ready to run

        Code:
        	else if ( State == State.DataLoaded)
        			{
        				hasRun = true;	
        			}
        SetDefault, Configure, Terminated will be set at various stages through user interaction so if there are resources you do not want to be setup or destroyed during these UI interactions, you will need to be careful where they are being setup. Please let me know if that does not help.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Your solutions have helped solve the problem when running a strategy both from the grid and from a chart.

          Unfortunately, I still can't seem to be able to Print() variables at the end of a backtest.

          Comment


            #6
            Originally posted by wuileng View Post
            Your solutions have helped solve the problem when running a strategy both from the grid and from a chart.

            Unfortunately, I still can't seem to be able to Print() variables at the end of a backtest.
            Hmm, looking at that further, it seems it is just not working on the first run of the strategy in the Strategy Analyzer-> if you run the strategy again, does it work more like the charts/strategies tab?
            MatthewNinjaTrader Product Management

            Comment


              #7
              Sadly it's still a bit different from running strategies from the grid or chart. Despite some additional calls, both the grid/chart strategy would print as expected upon termination.

              What it does do though, in backtests, is print the values of the previous run prior to starting. If your strategy does not generate any output in running, you might miss this, and assume that it Prints as expected upon the second run. This is not ideal for long backtests and also as a minor inconvenience. It means you have to run a strategy once, clear output, run it again then scroll all the way up to find the values.

              Here's what it looks like to me in my output window:

              <Click Run>
              [Strategy runs and prints the output expected while running.]
              <Backtest Completes/Terminates>
              <Click Run>
              [Strategy prints the variables from the previous run's State.Terminated.]
              [Strategy runs and prints the output expected while running.]
              <Backtest Completed/Terminates>

              And the next time I run a backtest, the State.Terminated outputs from the second run would be printed prior to the strategy starting up.

              Comment


                #8
                Thank you for bringing this behavior to my attention, it is fixed in B3 - #8288
                MatthewNinjaTrader Product Management

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Halmix, Today, 07:12 AM
                0 responses
                11 views
                0 likes
                Last Post Halmix
                by Halmix
                 
                Started by Bobin, Today, 06:39 AM
                0 responses
                13 views
                0 likes
                Last Post Bobin
                by Bobin
                 
                Started by KirkHammett, Today, 03:23 AM
                0 responses
                6 views
                0 likes
                Last Post KirkHammett  
                Started by eleven, Yesterday, 10:06 PM
                0 responses
                40 views
                0 likes
                Last Post eleven
                by eleven
                 
                Started by ezrollin, Yesterday, 08:12 PM
                7 responses
                75 views
                0 likes
                Last Post bltdavid  
                Working...
                X