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 averages chart.
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;
Exponential moving averages chart.
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;
Bollinger bands chart.
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;
MACD chart.
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;
Linear trend line and a polynomial regression of the 4th order.
// 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;
Momentum oscilator chart. Equity divided by 10 and placed to the same chart.
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;
This example shows how to add two systems and their EMAs to the chart.
// 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;