Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Running custom strategy and indicator through Strategy Analyzer

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

    Running custom strategy and indicator through Strategy Analyzer

    I have a custom strategy that uses an indicator.

    In Initialize(), I have
    Code:
    Add(MyIndicator());
    And in OnBarUpdate(), I have a signal condition which incorporates MyIndicator.


    I have placed the following print statement in OnBarUpdate() in the indicator …
    Code:
    If (this.CurrentBar == 100) Print (Current Bar + “|” + MyIndicator.Default[0]);
    Whenever, I run the strategy through Strategy Analyzer with the Output Window open, the print statement is executed twice.

    This implies that Strategy Analyzer runs the indicator twice over bar 100.

    Is that correct?

    The issue I particularly want to address is that the printed value of MyIndicator[0] (a double) is not the same in these two passes.

    Why could that be?

    #2
    Hello AnotherTrader,

    Are you adding another series inside of your Initialize method? Or are you Optimizing your Strategy that could be running your strategy with different variables?
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi, JC

      Thanks for the response.

      I am not optimizing, just backtesting.

      Originally posted by NinjaTrader_JC View Post
      ... Are you adding another series inside of your Initialize method? ...
      Am not sure what you mean by the above statement.

      Do you mean am I adding other indicators within Initialize()? I am adding further indicators.
      Last edited by AnotherTrader; 04-08-2014, 02:11 PM.

      Comment


        #4
        Hello AnotherTrader,

        I am referring to another data series like for example if you set your strategy to run on a 1 Minute period and you add a 5 minute period to your strategy.

        If you want you may post your full Initialize method and I can take a look at it.
        JCNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JC View Post
          Hello AnotherTrader,

          I am referring to another data series like for example if you set your strategy to run on a 1 Minute period and you add a 5 minute period to your strategy.

          If you want you may post your full Initialize method and I can take a look at it.
          Many thanks.

          Code:
           /// <summary>
                  /// Initialize()
                  /// </summary>
                  protected override void Initialize()
                  {
          			try
          			{
          				this.TraceOrders = false;
          				base.CalculateOnBarClose = true;
          				this.ExitOnClose = false;
          				this.EntriesPerDirection = 3;
          				this.BarsRequired = 100;
          				
          				Add(this.MySessionHours(this.EndHh, this.EndMm, this.EndSs, this.StartHh, this.StartMm, this.StartSs));
          				
          				Add(this.MyIndy1(this.sat_length, this.sat_maType, this.stopAfterTarget[0], false, this.sat_smooth));
          				Add(this.MyIndy1(this.sat_length, this.sat_maType, this.stopAfterTarget[1], false, this.sat_smooth));
          				Add(this.MyIndy1(this.sat_length, this.sat_maType, this.stopAfterTarget[2], false, this.sat_smooth));
          				
          				this.superTrend = this.MyIndy1(this._length, this._maType, this._multiplier, false, this._smooth);
          				this.superTrend.ShowArrows = false;
          				this.superTrend.Plots[0].Pen.Width = 3f;
          				this.superTrend.Plots[1].Pen.Width = 3f;
          				this.superTrend.AutoScale = false;
          				this.superTrend.PaintPriceMarkers = false;
          				
          				this.Add((IndicatorBase)this.superTrend);
          				this.Add(MyIndy1(this._length, this._maType, this._multiplier, false, this._smooth));
          				this.Add(this.ATR(this.ATRLength));
          				
          				switch (this.MAType)
          				{
          					case dhMAType.EMA:
          						EMA ema = this.EMA(this.maLength);
          						ema.Plots[0].Pen.Color = Color.Black;
          						ema.AutoScale = false;
          						ema.PaintPriceMarkers = false;
          						ema.CalculateOnBarClose = false;
          						this.Add((IndicatorBase)ema);
          						break;
          						
          					case dhMAType.HMA:
          						HMA hma = this.HMA(this.maLength);
          						hma.Plots[0].Pen.Color = Color.Black;
          						hma.AutoScale = false;
          						hma.PaintPriceMarkers = false;
          						hma.CalculateOnBarClose = false;
          						this.Add((IndicatorBase)hma);
          						break;
          						
          					case dhMAType.SMA:
          						SMA sma = this.SMA(this.maLength);
          						sma.Plots[0].Pen.Color = Color.Black;
          						sma.AutoScale = false;
          						sma.PaintPriceMarkers = false;
          						sma.CalculateOnBarClose = false;
          						this.Add((IndicatorBase)sma);
          						break;
          						
          					case dhMAType.TMA:
          						TMA tma = this.TMA(this.maLength);
          						tma.Plots[0].Pen.Color = Color.Black;
          						tma.AutoScale = false;
          						tma.PaintPriceMarkers = false;
          						tma.CalculateOnBarClose = false;
          						this.Add((IndicatorBase)tma);
          						break;
          				}
          				
          				this.Add(PeriodType.Minute, 60);
          				this.Add(this.TertiaryPeriodType, this.TertiaryPeriod);
          			}
          			
          			catch(Exception ex)
          			{
          				mrLog(this.Name + "|Error in Initialize() method|" + ex.ToString(), LogLevel.Error);
          			}
                  }

          Comment


            #6
            Hello AnotherTrader,

            Thank you for your response.

            The Initialize() method contains two additional bar series added to the script so it is likely causing the double print:
            Code:
            				this.Add(PeriodType.Minute, 60);
            				this.Add(this.TertiaryPeriodType, this.TertiaryPeriod);
            In the OnBarUpdate() is the Print() statement secluded to a specific BarsInProgress?

            Comment


              #7
              Originally posted by NinjaTrader_PatrickH View Post
              Hello AnotherTrader,

              Thank you for your response.

              The Initialize() method contains two additional bar series added to the script so it is likely causing the double print:
              Code:
              				this.Add(PeriodType.Minute, 60);
              				this.Add(this.TertiaryPeriodType, this.TertiaryPeriod);
              In the OnBarUpdate() is the Print() statement secluded to a specific BarsInProgress?
              Thanks for the response.

              I have been able further to pin down the occurrence. The attached example is a single timeframe strategy that uses a single indicator. There is a print statement added to the indicator to print a line to the output window at line 30 of the chart.

              Please run the attached strategy through strategy analyzer WITH THE STRATEGY ANALYZER CHART TAB OPEN. (You'll need at least 30+ bars)

              In the output window you will see the print statement is printed twice each time the strategy is run.

              Why twice?
              Attached Files
              Last edited by AnotherTrader; 04-09-2014, 02:15 AM.

              Comment


                #8
                Hello AnotherTrader,

                This is because when you run your Backtest inside of the Strategy Analyzer, it is going to run your strategy to get your results which include your indicator. When the Chart tab is selected, NinjaTrader is going to load your Indicator once again to display it visually since it visually it was not loaded yet to save resources.
                JCNinjaTrader Customer Service

                Comment


                  #9
                  A followup question on this: It will be really good to have some sort of documents of how to integrate Indicators with Strategies, especially in the backtest environment.

                  I have a similar test, a very simple strategy (stra1), adding a very simple indicator (ind1).

                  Stra1 primary is 15min, add a secord timeframe as 1 min.
                  Ind1 itself add a 5min timeframe.

                  stra1 is
                  PHP Code:
                  protected override void OnBarUpdate()
                          {
                              
                  double tmpx;
                              if (
                  BarsInProgress 1)
                                  
                  tmpx myind1[0];
                              else
                                  Print(
                  "sta / " BarsInProgress " / " CurrentBar " / " Time[0]);
                          } 
                  ind1 is :
                  PHP Code:
                  protected override void OnBarUpdate()
                          {
                              Print(
                  "\t===> ind / " BarsInProgress " / " CurrentBar " / " Time[0]);
                          } 
                  running backtest on 15min bar, the output looks like this:
                  sta / 1 / 5719 / 3/20/2014 9:53:00 PM
                  sta / 1 / 5720 / 3/20/2014 9:54:00 PM
                  sta / 1 / 5721 / 3/20/2014 9:55:00 PM
                  sta / 1 / 5722 / 3/20/2014 9:56:00 PM
                  sta / 1 / 5723 / 3/20/2014 9:57:00 PM
                  sta / 1 / 5724 / 3/20/2014 9:58:00 PM
                  sta / 1 / 5725 / 3/20/2014 9:59:00 PM
                  ===> ind / 0 / 429 / 3/20/2014 10:00:00 PM
                  ===> ind / 1 / 656 / 3/20/2014 10:00:00 PM
                  ===> ind / 1 / 657 / 3/20/2014 10:05:00 PM
                  ===> ind / 1 / 658 / 3/20/2014 10:10:00 PM
                  sta / 1 / 5726 / 3/20/2014 10:00:00 PM
                  sta / 1 / 5727 / 3/20/2014 10:01:00 PM
                  sta / 1 / 5728 / 3/20/2014 10:02:00 PM
                  sta / 1 / 5729 / 3/20/2014 10:03:00 PM
                  sta / 1 / 5730 / 3/20/2014 10:04:00 PM
                  sta / 1 / 5731 / 3/20/2014 10:05:00 PM
                  sta / 1 / 5732 / 3/20/2014 10:06:00 PM
                  sta / 1 / 5733 / 3/20/2014 10:07:00 PM
                  sta / 1 / 5734 / 3/20/2014 10:08:00 PM
                  sta / 1 / 5735 / 3/20/2014 10:09:00 PM
                  sta / 1 / 5736 / 3/20/2014 10:10:00 PM
                  sta / 1 / 5737 / 3/20/2014 10:11:00 PM
                  sta / 1 / 5738 / 3/20/2014 10:12:00 PM
                  sta / 1 / 5739 / 3/20/2014 10:13:00 PM
                  sta / 1 / 5740 / 3/20/2014 10:14:00 PM
                  ===> ind / 0 / 430 / 3/20/2014 10:15:00 PM
                  ===> ind / 1 / 659 / 3/20/2014 10:15:00 PM
                  ===> ind / 1 / 660 / 3/20/2014 10:20:00 PM
                  ===> ind / 1 / 661 / 3/20/2014 10:25:00 PM
                  sta / 1 / 5741 / 3/20/2014 10:15:00 PM
                  sta / 1 / 5742 / 3/20/2014 10:16:00 PM
                  sta / 1 / 5743 / 3/20/2014 10:17:00 PM
                  So the question is: is the output window buffer differently for strategy and indicator? How can a bar is executed ahead of 'current time'?

                  both source codes and the output is attached.
                  Attached Files

                  Comment


                    #10
                    Hello balancenv,

                    Thank you for your post.

                    The bar is not executed ahead of time, it is the bar's time stamp at close. The backtest processed the 15 minute, then the 5 minute and finally the 1 minute bar series for the last 15 minutes continuing on for the rest of the bars.

                    Please refer to the following link for how bar data is referenced in multi-time frame and bar series scripts: http://www.ninjatrader.com/support/h...nstruments.htm

                    Comment


                      #11
                      Hello balancenv,

                      Thank you for your patience.

                      I was able to produce the same results with your sta1 and ind1 on the 15 minute chart. So in this strategy you force a 5 minute version of your ind1 to update on every 15 minute close, and then the strategy on every minute. The ind1 provides it's values on the 15 minute close when these bars are available and then your 1 minute bar updates for the same time period. The times are the time stamps of the bar closes.

                      For example, in the following we have the indicator updating it's value on the 15 minute close and then calling the indicator that uses the timeframe of 5 minute to update as well, then the strategy updates for the last 15 1 minute bars that would be included in the 15 minute bar close.
                      ===> ind / 0 / 994 / 4/15/2014 10:30:00 AM
                      ===> ind / 1 / 2982 / 4/15/2014 10:30:00 AM
                      ===> ind / 1 / 2983 / 4/15/2014 10:35:00 AM
                      sta / 1 / 14741 / 4/15/2014 10:30:00 AM
                      sta / 1 / 14742 / 4/15/2014 10:31:00 AM
                      sta / 1 / 14743 / 4/15/2014 10:32:00 AM
                      sta / 1 / 14744 / 4/15/2014 10:33:00 AM
                      sta / 1 / 14745 / 4/15/2014 10:34:00 AM
                      sta / 1 / 14746 / 4/15/2014 10:35:00 AM
                      sta / 1 / 14747 / 4/15/2014 10:36:00 AM
                      sta / 1 / 14748 / 4/15/2014 10:37:00 AM
                      sta / 1 / 14749 / 4/15/2014 10:38:00 AM
                      sta / 1 / 14750 / 4/15/2014 10:39:00 AM

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by i019945nj, 12-14-2023, 06:41 AM
                      3 responses
                      60 views
                      0 likes
                      Last Post i019945nj  
                      Started by TraderBCL, Today, 04:38 AM
                      2 responses
                      17 views
                      0 likes
                      Last Post TraderBCL  
                      Started by martin70, 03-24-2023, 04:58 AM
                      14 responses
                      106 views
                      0 likes
                      Last Post martin70  
                      Started by Radano, 06-10-2021, 01:40 AM
                      19 responses
                      610 views
                      0 likes
                      Last Post Radano
                      by Radano
                       
                      Started by thanajo, 05-04-2021, 02:11 AM
                      4 responses
                      471 views
                      0 likes
                      Last Post tradingnasdaqprueba  
                      Working...
                      X