Click or drag to resize
Technical analysis examples

Technical analysis examples

This topic contains the following sections:

Following examples show how to use technical analysis (TA) functions.

TA functions work with time series. You can use any time series Series<DateTime,double> or Series<DateTime,decimal>, but we just use trading systems equity in these examples.

Used equity data include commission and subscription fees.

Simple moving average

Simple moving averages chart.

Simple moving averages
Int64 systemId = ENTER_SOME_TRADING_SYSTEM_ID_HERE;

// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity);

// Moving averages
var ma10 = C2TALib.MA(equity, 10);
var ma30 = C2TALib.MA(equity, 30);

// Create a chart object
ITimeSeriesChart chart = new TimeSeriesChart();
chart.Name = "Simple moving averages example";

// Add equity to the chart
chart.Add(equity, "Equity", Color.Blue);

// Add simple moving averages to the chart
chart.Add(ma10, "MA(10)", Color.Red);
chart.Add(ma30, "MA(30)", Color.Green);

CHART = chart;
MA(10), MA(30)
SMA
Exponential moving average

Exponential moving averages chart.

Exponential moving averages
Int64 systemId = ENTER_SOME_TRADING_SYSTEM_ID_HERE;

// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity);

// Moving averages
var ema10 = C2TALib.EMA(equity, 10);
var ema30 = C2TALib.EMA(equity, 30);

// Create a chart object
ITimeSeriesChart chart = new TimeSeriesChart();
chart.Name = "Exponential moving average example";

// Add equity to the chart
chart.Add(equity, "Equity", Color.Blue);

// Add exponential moving averages to the chart
chart.Add(ema10, "EMA(10)", Color.Red);
chart.Add(ema30, "EMA(30)", Color.Green);

CHART = chart;
EMA(10), EMA(30)
EMA
Bollinger bands

Bollinger bands chart.

Bollinger bands
Int64 systemId = ENTER_SOME_TRADING_SYSTEM_ID_HERE;

// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity);

// Calculate Bollinger bands
var bbtop = C2TALib.BBandTop(equity, 15, 2.0);
var bbbot = C2TALib.BBandBot(equity, 15, 2.0);

// Create a chart object
ITimeSeriesChart chart = new TimeSeriesChart();
chart.Name = "Bollinger bands example";

// Add equity to the chart
chart.Add(equity, "Equity", Color.Blue);

// Add upper BBand 
chart.Add(bbtop, "BBTop", Color.Red);

// Add lower BBand 
chart.Add(bbbot, "BBBot", Color.Red);

CHART = chart;
BollingerBands(15, 2.0)
BBands Example
MACD

MACD chart.

MACD
Int64 systemId = ENTER_SOME_TRADING_SYSTEM_ID_HERE;

// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity).AsDoubles();

// macdResults is a tuple of <macd,signal,histogram> series
var macdResults = C2TALib.MACD(equity);

// Create a chart object
ITimeSeriesChart chart = new TimeSeriesChart();
chart.Name = "MACD example";

// Add MACD
chart.Add(macdResults.Item1, "MACD", Color.Red);

// Add Signal
chart.Add(macdResults.Item2, "Signal", Color.Blue);

// Add equity divided by 10 to have it in the same chart
chart.Add(equity / 10, "Equity", Color.Green);

CHART = chart;
MACD
MACDExample
Linear and polynomial regression

Linear trend line and a polynomial regression of the 4th order.

MACD
// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity);

// Linear regression line 
var linearRegLine = C2TALib.LinearRegLine(equity);

// Polynomial regression line 
var polynomialRegLine = C2TALib.PolynomialReg(equity, 4);

// Create a chart object
ITimeSeriesChart timeSeriesChart = new TimeSeriesChart();
timeSeriesChart.Name = "Linear and polynomial regression";

timeSeriesChart.Add(equity, "Equity", Color.Blue);
timeSeriesChart.Add(linearRegLine, "Trend line", Color.Red);
timeSeriesChart.Add(polynomialRegLine, "PolynomialReg", Color.Green);

CHART = timeSeriesChart;

H5 = "Tabular data";

var eq_lin_zip = from z in equity.Zip(linearRegLine).Observations
                 select new
                 {
                     Date = z.Key,
                     Equity = z.Value.Item1.Value,
                     Linear = Math.Round(z.Value.Item2.Value, 0)
                 };
TABLE = eq_lin_zip;

var eq_poly_zip = from z in equity.Zip(polynomialRegLine).Observations
                  select new
                  {
                      Date = z.Key,
                      Equity = z.Value.Item1.Value,
                      Polynomial = Math.Round(z.Value.Item2.Value, 0)
                  };
TABLE = eq_poly_zip;
Regression
Lin Poly Regression Example
Momentum oscilator

Momentum oscilator chart. Equity divided by 10 and placed to the same chart.

Systems and EMAs
Int64 systemId = ENTER_SOME_TRADING_SYSTEM_ID_HERE;

// Daily equity data with commissions and fees
ITimeSheet timeSheet = TimeSheetFactory(systemId, TimeInterval.Day);

// Let TimeSheet run
timeSheet.EquitiesSheet();

// Extract equity data. 
var equity = timeSheet.GetColumn(systemId, EquityType.Equity).AsDoubles();

// Momentum oscilator
var momentum = C2TALib.MOM(equity, 20);

// Create a chart object
ITimeSeriesChart chart = new TimeSeriesChart();
chart.Name = "Momentum example";

// Add equity (divided by 10) to the same chart
chart.Add(equity / 10, "Equity/10", Color.Blue);

// Add momentum to the chart
chart.Add(momentum, "MOM(20)", Color.Green);

CHART = chart;
Momentum oscilator
Momentum Oscilator Example
Two systems and EMAs chart

This example shows how to add two systems and their EMAs to the chart.

Systems and EMAs
// Systems we want to see in the chart
Int64[] systemsIds = { 102387110, 102196708 };

// Create a chart object
ITimeSeriesChart timeSeriesChart = new TimeSeriesChart();
timeSeriesChart.Name = "Systems and EMAs example";

foreach (var id in systemsIds)
{
    var system = GetC2SYSTEM(id);

    // Daily equity data with commissions and fees
    ITimeSheet timeSheet = TimeSheetFactory(id, TimeInterval.Day);

    // Let TimeSheet run
    timeSheet.EquitiesSheet();

    // Extract equity data. 
    var equity = timeSheet.GetColumn(id, EquityType.Equity);

    // Add equity to the chart
    timeSeriesChart.Add(equity, system.Name, Color.Green);

    // Add equity EMA(15) to the chart
    timeSeriesChart.Add(C2TALib.EMA(equity, 15), system.Name + " (EMA)", Color.Red);
}

CHART = timeSeriesChart;
Two systems and their EMAs
Two Systems And Ema Chart