# Second Derivative

Bar Info

Debugging

Derivatives

Matrix Handling

Price Rounding

Regression Lines

Statistics

User Tracking

Example Strategies

## Second Derivative

Description:

Calculates the first and second derivative of each point on the chart and saves it as a series.

Syntax:

`SecondDerivative();SecondDerivative(double dev1, double dev2);`

Arguments:

dev1: The first derivative that occurs first in the sequence.

dev2: The first derivative that occurs second in the sequence.

Notes:

You will need to create your own series in NinjaScript that follow along with the developement of each bar.

This is done by creating the series, and then when setting the instance you feed it the parameter ‘this’.

ie.

`if (State == State.Configure) {     mySeries = new Series(this);}`
```				```
/// <summary>
/// Calculates the second derivative of a chart using SMA values as inputs.
/// </summary>
/// <remarks>
/// To speed up the process you can add a public SMA indicator with
/// the desired period, and access its values rather than creating two indicators
/// at the point of calculation.
/// Each derivative is being saved in the NinjaScript series' "Values", but this can easily
/// be changed to work with a custom series.
/// </remarks>
private void SecondDerivative()
{
int smooth1	= 2;
int smooth2	= 1;
int period	= 10;

// prevents out of bounds errors
if (CurrentBar < period + smooth1 || CurrentBar < period + smooth2)
return;

// prevents dividing by 0
if (smooth1 < 1 || smooth2 < 1)
throw new InvalidOperationException
("Derivative smooth must be at least 1");

int x1		= CurrentBar - smooth1;
int x2		= CurrentBar;
double y1	= SMA(period)[smooth1];
double y2	= SMA(period)[0];

Values[0][0] = (y2 - y1) / (x2 - x1); // first derivative
Values[1][0] = (Values[0][smooth2] - Values[0][0]) / smooth2; // second derivative
}

/// <summary>
/// Calculates the second derivative from two provided first derivative values.
/// </summary>
/// <param name="dev1"> First first derivative value. </param>
/// <param name="dev2"> Second first derivative value. </param>
/// <returns> The second derivative. </returns>
private static double SecondDerivative(double dev1, double dev2)
{
return (dev2 - dev1) / 2;
}
```
```