Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculations in OnStartUp()

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

    Calculations in OnStartUp()

    Hey guys,

    Two questions:

    I'm running a strategy that does a spread calculation between two intruments, with the code in OnStartUp() and the custom methods in OnBarUpdate(). Is this a good use for OnStartUp()? The spread plots as desired in the strategy.

    But... in OnStartUp I've also tried to code a zscore plot using the spread as its input. The code is commented out below as it won't compile. Any pointers on what I'm doing wrong?

    Thanks!

    PHP Code:
    
    protected override void OnStartUp()///////////////////////////////////////////////////////////
    {
    //Do something else...
    } 
    private void SpreadCalculator()
    {
    spreadRough = (Closes[0][0]*quotedQuantity)-(Closes[1][0]*hedgeQuantity);
    spread = Math.Round (spreadRough, 2);
    }
    
    private void SpreadPlotter()
    {
    StrategyPlot(0).Value.Set(spread); 
    }
    
    //private void ZScoreCalculator()
    //{
    //SMA mySMA = SMA(spread, zPeriod);
    //StdDev myStdDev = StdDev(spread, zPeriod);
    
    //zscore = (spread-mySMA)/(myStdDev);
    //}
    
    private void GoLongSpread()
    {etcetc} 
    

    #2
    MXASJ,

    What you have pasted has nothing to do with OnStartUp() as all of your code is outside in other methods and such. Why your ZScoreCalculator doesn't work would need to be addressed directly. Please provide the exact error messages.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      My errors involved operations with doubles and DataSeries which have been corrected since my post. If anyone is interested here is the code for plotting a ZScore in a strategy (or my interpretation of the ZScore). In this case I'm plotting the ZScore of another DataSeries called spread:

      PHP Code:
      in Variables
      ...
      private int zPeriod = 14; //Loopback period for z-score calculation
       
      private double spreadRough;
      private DataSeries spread;
      private double zscore;
      private DataSeries zplot;
       
      in Initialize()
      ...
      Add(StrategyPlot(0));
      Add(StrategyPlot(1));
      StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
      StrategyPlot(0).PanelUI = 2;
      StrategyPlot(1).Plots[0].Pen.Color = Color.Red;
      StrategyPlot(1).PanelUI = 3;
      spread = new DataSeries(this);
      zplot = new DataSeries(this);
       
      in OnStartUp()
      ...
      private void ZScoreCalculator()
      {
      double i,j;
      i=SMA(spread, 14)[0];
      j=StdDev(spread,14)[0];
      zscore = ( (spread[0]-i) / j );
      zplot.Set(zscore);
      }
       
      private void SpreadPlotter()
      {
      StrategyPlot(0).Value.Set(spread[0]);
      StrategyPlot(1).Value.Set(zplot[0]);
      }
       
      ...
       
      In OnBarUpdate()
       
      SpreadCalculator();
      ZScoreCalculator();
      SpreadPlotter();
      ... 
      
      Is there a way to change the name of the StrategyPlots in the chart panels in NT7?

      Comment


        #4
        MXASJ,

        Just create yourself another indicator with whatever name you want and have it done exactly the same way as StrategyPlot. Then in your code call that indicator instead of StrategyPlot.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I am using this

          [ Add(StrategyPlot(0));
          StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
          StrategyPlot(0).PanelUI = 2;
          CalculateOnBarClose = true;
          myDataSeries = new DataSeries(this, MaximumBarsLookBack.Infinite); ]


          And I get the error the name StrategyPlot does not exist in the current context .... any idea why ?

          Comment


            #6
            Hello,

            Since you are asking about NT7, I will have someone reply to you on Monday. Thank you for your patience.
            DenNinjaTrader Customer Service

            Comment


              #7
              gabga100,

              You actually have to have an indicator called "StrategyPlot" for this to work. Please install it from the reference samples section of the forum.

              When running a strategy on a chart you may find the need to plot values onto a chart. If these values are internal strategy calculations that are difficult to migrate to an indicator, you can use the following technique to achieve a plot. NinjaTrader 8 With NinjaTrader 8 we introduced strategy plots which provide the ability
              Josh P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              595 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              554 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X