Tyche Trading

First Derivative

Bar Info

Debugging

Derivatives

Matrix Handling

Price Rounding

Regression Lines

Statistics

User Tracking

Example Strategies

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);
}
				
					/// <summary>
/// Calculates the first 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 "Value", but this can easily
/// be changed to work with a custom series.
/// </remarks>
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);
}

/// <summary>
/// Calculates the first derivative of a set of provided values.
/// </summary>
/// <param name="x1"> The first x value on chart. </param>
/// <param name="x2"> The second x value on chart. </param>
/// <param name="y1"> The first y Value on chart. </param>
/// <param name="y2"> The second y Value on chart. </param>
/// <returns> The derivative of the provide inputs from chart. </returns>
private double FirstDerivative(int x1, int x2, double y1, double y2)
{
	return (y2 - y1) / (x2 - x1);
}