Bar Info
Menu
Debugging
Menu
Derivatives
Menu
Matrix Handling
Menu
Price Rounding
Menu
Regression Lines
Menu
Statistics
Menu
User Tracking
Menu
Example Strategies
Menu
First Derivative
Description:
Calculates the first and derivative of each point on the chart and saves it as a series.
Syntax:
FirstDerivative();
FirstDerivative(double x1, double x2, double y1, double y2);
Arguments:
x1: The x value that occurs first in the series.
x2: The x value that occurs second in the series.
y1: The y value that occurs first in the series.
y2: The y value that occurs second in the series.
Notes:
You will need to create your own series in NinjaScript that follows 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);
}
///
/// Calculates the first derivative of a chart using SMA values as inputs.
///
///
/// 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 "Value", but this can easily
/// be changed to work with a custom series.
///
private void FirstDerivative()
{
int smooth = 2;
int period = 10;
// prevents out of bounds errors
if (CurrentBar < period + smooth)
return;
// prevents dividing by 0
if (smooth < 1)
throw new InvalidOperationException
("Derivative smooth must be at least 1");
int x1 = CurrentBar - smooth;
int x2 = CurrentBar;
double y1 = SMA(period)[smooth];
double y2 = SMA(period)[0];
Value[0] = (y2 - y1) / (x2 - x1);
}
///
/// Calculates the first derivative of a set of provided values.
///
/// The first x value on chart.
/// The second x value on chart.
/// The first y Value on chart.
/// The second y Value on chart.
/// The derivative of the provide inputs from chart.
private double FirstDerivative(int x1, int x2, double y1, double y2)
{
return (y2 - y1) / (x2 - x1);
}