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

Jeff's Augen's Volatility Spike code

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

    Jeff's Augen's Volatility Spike code

    In Jeff Augen's excellent book, "The Volatility Edge in Option Trading", there's a very simple yet powerful indicator he used to show the price spike in terms of historical volatility, which I think is very useful in option trading.
    Coming from Thinkorswim, I've enjoyed this indicator for a while and tried my first attempt to code in Ninjascript. Here is a very good explanation and the final verion of the ToS code is

    Code:
    declare lower;
    input length = 20;
    def closeLog = Log(close[1] / close[2]); 
    def sDevd = stdev(closeLog, length) * sqrt(length/(length-1));  
    def m= SDevd * close[1];
    
    plot spk = (close[0] - close[1]) / m;
    I thought the implementation is fairly straightforward (with a bit of adjustment back to a strong type language..)

    Code:
    protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
    			double sdev;
                            double tmpx;
    
    			if (CurrentBar < 2)
    				return;
    			
    			CloseLog.Set (Math.Log(Close[1]/Close[2]));
    			
    			if (CurrentBar > len ) 
    			{
    				sdev =	StdDev(CloseLog, len)[0] * Math.Sqrt(len/(len-1));
                                    tmpx = (Close[0] - Close[1]) / (sdev * Close[1]); 
                                    Value.Set(tmpx); 
    				Plot0.Set( Value[0] ); 
            	}
    		}
    Two issues bother me on this code,
    1. the result is a bit off as compared to ToS's calculation, for example, On AAPL daily chart, the last few days are (date, NT, ToS)

    12/5, -2.75, -2.68
    12/6, 0.571, 0.557

    Since the difference is not significant for me (normally you want to spot big spike), so I can live with this. But

    2. The NT version does not calculate the last bar, even though I've set it “on bar close' to true.

    I've tried using VisualStudio to step through (a very good exercise for new programmers!). It seems the last bar (in this case 12/7's bar) was never executed.

    Any suggestion on why, or where I did wrong in this code?

    Thanks.
    Attached Files

    #2
    Hello balancenv,

    Who are you connected to? This is displayed by green on lower left corner of the Control Center window.

    What interval is selected for AAPL? Example: 1 Min, 1 Day, etc...

    What do you have Calculate on Bar Close (COBC) set to?

    Note that you may want to use Print() statements to verify values are what you expect and see how your code is being processed as well inside the Tools -> Output Window - Debugging your NinjaScript code.

    For Example:
    Code:
    Print("Time: "+Time[0]+" len: "+len);
    if (CurrentBar > len ) 
    {
    	sdev =	StdDev(CloseLog, len)[0] * Math.Sqrt(len/(len-1));
            tmpx = (Close[0] - Close[1]) / (sdev * Close[1]); 
            Value.Set(tmpx); 
    	Plot0.Set( Value[0] ); 
    }
    Happy to be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Originally posted by balancenv View Post
      In Jeff Augen's excellent book, "The Volatility Edge in Option Trading", there's a very simple yet powerful indicator he used to show the price spike in terms of historical volatility, which I think is very useful in option trading.
      Coming from Thinkorswim, I've enjoyed this indicator for a while and tried my first attempt to code in Ninjascript. Here is a very good explanation and the final verion of the ToS code is

      Code:
      declare lower;
      input length = 20;
      def closeLog = Log(close[1] / close[2]); 
      def sDevd = stdev(closeLog, length) * sqrt(length/(length-1));  
      def m= SDevd * close[1];
       
      plot spk = (close[0] - close[1]) / m;
      I thought the implementation is fairly straightforward (with a bit of adjustment back to a strong type language..)

      Code:
      protected override void OnBarUpdate()
              {
                  // Use this method for calculating your indicator values. Assign a value to each
                  // plot below by replacing 'Close[0]' with your own formula.
                  double sdev;
                              double tmpx;
       
                  if (CurrentBar < 2)
                      return;
       
                  CloseLog.Set (Math.Log(Close[1]/Close[2]));
       
                  if (CurrentBar > len ) 
                  {
                      sdev =    StdDev(CloseLog, len)[0] * Math.Sqrt(len/(len-1));
                                      tmpx = (Close[0] - Close[1]) / (sdev * Close[1]); 
                                      Value.Set(tmpx); 
                      Plot0.Set( Value[0] ); 
                  }
              }
      Two issues bother me on this code,
      1. the result is a bit off as compared to ToS's calculation, for example, On AAPL daily chart, the last few days are (date, NT, ToS)

      12/5, -2.75, -2.68
      12/6, 0.571, 0.557

      Since the difference is not significant for me (normally you want to spot big spike), so I can live with this. But

      2. The NT version does not calculate the last bar, even though I've set it “on bar close' to true.

      I've tried using VisualStudio to step through (a very good exercise for new programmers!). It seems the last bar (in this case 12/7's bar) was never executed.

      Any suggestion on why, or where I did wrong in this code?

      Thanks.
      If you set COBC to true, you are directing that the value be calculated on the Close of the bar. that means that the last bar's indicator value can never be shown, because while the bar is in progress, it has not yet closed. If you want to show the developing last bar, then COBC will have to be false, so that it calculates even while the bar has not yet closed.

      Comment


        #4
        Thanks for the quick note.

        COBC is set true indeed, and debug mode can see all the variable states so no need for print statements.

        This is on AAPL daily chart, and I ran the code on 12/8, why the last bar (12/7) still not closed?

        Comment


          #5
          Originally posted by balancenv View Post
          Thanks for the quick note.

          COBC is set true indeed, and debug mode can see all the variable states so no need for print statements.

          This is on AAPL daily chart, and I ran the code on 12/8, why the last bar (12/7) still not closed?
          12/8 is not a trading day. The bar closes when it receives the first tick that does not belong to its dataset. No tick can be received on a non-trading day.

          Comment


            #6
            Thanks for the note. Very helpful for new comer.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ageeholdings, Today, 07:43 AM
            0 responses
            6 views
            0 likes
            Last Post ageeholdings  
            Started by pibrew, Today, 06:37 AM
            0 responses
            4 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            14 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            12 views
            0 likes
            Last Post burtoninlondon  
            Working...
            X