The goal of this library is to contain a bunch of common time-series analysis tools and indicators mostly used in technical analysis in finance.
- Linear Trend Line
- Relative Strength Index
- Moving Average Convergence Divergence
- Bollinger Bands
- Ichimoku Cloud
- Fibonacci Retracement
- Fibonacci Fan
- Fibonacci Arc
- Fibonacci Time Zones
You can use Maven or Gradle to add the library to your project.
<dependency>
<groupId>com.github.ahmadmo</groupId>
<artifactId>math-time-series</artifactId>
<version>0.0.4</version>
</dependency>
dependencies {
compile 'com.github.ahmadmo:math-time-series:0.0.4'
}
import ir.ahmadmo.math.timeseries.trend.*
import org.knowm.xchart.SwingWrapper
import org.knowm.xchart.XYChartBuilder
import org.knowm.xchart.style.Styler
import org.knowm.xchart.style.markers.SeriesMarkers
import kotlin.math.abs
import kotlin.math.min
You need to add the dependency for xchart to run the sample code:
<dependency>
<groupId>org.knowm.xchart</groupId>
<artifactId>xchart</artifactId>
<version>3.6.5</version>
</dependency>
val trend = generateRandomTrend(size = 120)
val smoothingFactors = doubleArrayOf(0.01, 0.02, 0.05, 0.1, 0.15, 0.25)
val (resistance, support, average) = trend.linearTrendLines(smoothingFactors)
val resistanceData = DoubleArray(trend.size, resistance::value)
val supportData = DoubleArray(trend.size, support::value)
val averageData = DoubleArray(trend.size, average::value)
Notice that this is the user's responsibility to choose the rigth values for smoothingFactors
from 0.0
to 1.0
to achieve better results. Choosing higher values results in smoother trends and consequently tighter trend lines. An empty smoothingFactors
disables smoothing.
The function for generating a random trend (time-series):
fun generateRandomTrend(size: Int): Trend {
val trend = Trend(size)
var min = Double.MAX_VALUE
for (x in trend.indices.drop(1)) {
trend[x] = trend[x - 1] + Math.random() - 0.5
min = min(min, trend[x])
}
val shift = abs(min(min, 0.0))
return if (shift == 0.0) trend
else Trend(size) { x -> trend[x] + shift }
}
Finally, to visualize the computed trend lines using xchart: (learn more)
val chart = XYChartBuilder()
.width(1280).height(720)
.theme(Styler.ChartTheme.Matlab)
.title("Trend Lines")
.xAxisTitle("Day").yAxisTitle("Price")
.build()
chart.styler.isPlotGridLinesVisible = false
chart.styler.xAxisTickMarkSpacingHint = 100
val xData = DoubleArray(trend.size, Int::toDouble)
chart.addSeries("Trend", xData, trend)
chart.addSeries("Resistance", xData, resistanceData)
chart.addSeries("Support", xData, supportData)
chart.addSeries("Average", xData, averageData)
chart.seriesMap.values.forEach { it.marker = SeriesMarkers.NONE }
SwingWrapper(chart).displayChart()
Here is the complete sample code demonstrating how to draw linear trend lines for a time-series.
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO