Skip to content

Commit

Permalink
Merge pull request #80 from matoos32/javadoc-updates
Browse files Browse the repository at this point in the history
Tech-debt: add missing Javadoc and update code formatting
  • Loading branch information
matoos32 authored Dec 5, 2022
2 parents 90f0ccb + 987bd1c commit 6fc6fb1
Show file tree
Hide file tree
Showing 24 changed files with 939 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class JFreeChartBuilderDemo {
private static final LocalDateTime startDate = endDate.minus(18, ChronoUnit.MONTHS);

// Java 8 min requirement can't use Set.of()
public static DayOfWeek[] OHLCV_SKIP_DAYS = {DayOfWeek.SATURDAY, DayOfWeek.SUNDAY};
private static DayOfWeek[] OHLCV_SKIP_DAYS = {DayOfWeek.SATURDAY, DayOfWeek.SUNDAY};
private static final Set<DayOfWeek> ohlcvSkipDays = new HashSet<>(Arrays.asList(OHLCV_SKIP_DAYS));

private static IDateTimeSeriesProvider timeProvider = AscendingDateTimeGenerator.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public static class SinusoidParams {
* Constructor
*
* @param amplitude The amplitude of the sinusoid
* @param freqScaleFactor Constant used to multiple the frequency component prior to calculating
* the sinusoid value
* @param freqScaleFactor Constant used to multiply the frequency component prior to calculating
* the sinusoid value. Lower and higher values are for lower and higher frequencies
* respectively
* @param amplitudeOffset Amplitude offset of the sinusoid
*/
public SinusoidParams(double amplitude, double freqScaleFactor, double amplitudeOffset) {
Expand All @@ -54,36 +55,65 @@ public SinusoidParams(double amplitude, double freqScaleFactor, double amplitude
this.amplitudeOffset = amplitudeOffset;
}

/**
* Gets the configured sinusoid amplitude.
*
* @return The amplitude value
*/
public double getAmplitude() {
return amplitude;
}

/**
* Gets the configured sinusoid frequency scaling factor.
*
* @return The scale factor value
*/
public double getFreqScaleFactor() {
return freqScaleFactor;
}

/**
* Gets the configured sinusoid amplitude offset to use.
*
* @return The offset value
*/
public double getAmplitudeOffset() {
return amplitudeOffset;
}
}

/**
* Helper method to generate random sinusoid configuration parameters.
*
* @param freqScaleFactor A desired frequency scale factor
* @return New {@link SinusoidParams} instance containing randomly generated parameters
*/
public static SinusoidParams getRandParams(double freqScaleFactor) {
final double amplitude = ThreadLocalRandom.current().nextDouble() * MAX_AMPLITUDE;
final double yOffset = 2.0 * amplitude;
final double freqFactor = ThreadLocalRandom.current().nextDouble() / freqScaleFactor;
return new SinusoidParams(amplitude, freqFactor, yOffset);
}

/**
* Generates a series of values representing a discrete random sinusoidal waveform. The waveform
* will randomly be a sine or cosine series with random frequency, amplitude, and constant offset.
*
* @param freqScaleFactor The frequency scaling factor to use
* @param numElems The number of elements to produce in the series
* @return New array containing the generated series
*/
public static double[] getRandSeries(double freqScaleFactor, int numElems) {

SinusoidParams params = Sinusoid.getRandParams(freqScaleFactor);

double[] values = new double[numElems];

if(ThreadLocalRandom.current().nextDouble() > 0.5) {
if (ThreadLocalRandom.current().nextDouble() > 0.5) {
for (int n = 0; n < numElems; n++) {
values[n] = getCosineValue(params, n);
}
}
} else {
for (int n = 0; n < numElems; n++) {
values[n] = getSineValue(params, n);
Expand All @@ -93,11 +123,25 @@ public static double[] getRandSeries(double freqScaleFactor, int numElems) {
return values;
}

/**
* Calculates a cosine value at a discrete instant given a set of sinusoid parameters.
*
* @param params The parameters to use in the calculation
* @param n The discrete integer time instant
* @return The calculated cosine value
*/
public static double getCosineValue(SinusoidParams params, int n) {
return params.getAmplitude() * Math.cos(TWO_PI * params.getFreqScaleFactor() * (double) n)
+ params.getAmplitudeOffset();
}

/**
* Calculates a sine value at a discrete instant given a set of sinusoid parameters.
*
* @param params The parameters to use in the calculation
* @param n The discrete integer time instant
* @return The calculated sine value
*/
public static double getSineValue(SinusoidParams params, int n) {
return params.getAmplitude() * Math.sin(TWO_PI * params.getFreqScaleFactor() * (double) n)
+ params.getAmplitudeOffset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
public class StochasticOscillator {

/**
* Data-structure for representing a stochastic oscillator indicator series.
*/
public static class StochData {

private static final double[] EMPTY_DATA = {};
Expand All @@ -48,14 +51,20 @@ private StochData(double[] pctK, double[] pctD) {
this.pctD = pctD;
}

public static double[] getEmptyData() {
return EMPTY_DATA;
}

/**
* Gets the %K data.
*
* @return Reference to the data series array
*/
public double[] getPctK() {
return pctK;
}

/**
* Gets the %D data.
*
* @return Reference to the data series array
*/
public double[] getPctD() {
return pctD;
}
Expand Down Expand Up @@ -124,10 +133,10 @@ public static StochData calculate(int K, int D, double[] highs, double[] lows, d
}
}
}

// Now calculate %D from the %K series we just created.
final double[] pctD = Sma.calculate(D, pctK);

return new StochData(pctK, pctD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.jfcbuilder.types.XYTimeSeriesPlotBuilderElements;

/**
* Utility methods that can be used throughout the application.
* Utility methods for common logic within the framework.
*/
public abstract class BuilderUtils {

Expand Down Expand Up @@ -71,17 +71,17 @@ public static NumberAxis createYAxis(XYTimeSeriesPlotBuilderElements elements) {
}

/**
* Helper method to create and initialize an XYPlot
* Helper method to create and initialize an XYPlot.
*
* @param xAxis The x-axis to be used with the plot
* @param yAxis The y-axis to be used with the plot
* @param dataset An XYDataset to be plotted
* @param xAxis The x-axis to be used with the plot
* @param yAxis The y-axis to be used with the plot
* @param dataset An XYDataset to be plotted
* @param renderer The renderer used to plot the dataset
* @param elements Various settings used to initialize the plot object
* @return The new XYPlot instance
*/
public static XYPlot createPlot(final ValueAxis xAxis, NumberAxis yAxis, XYDataset dataset, XYItemRenderer renderer,
XYTimeSeriesPlotBuilderElements elements) {
public static XYPlot createPlot(final ValueAxis xAxis, NumberAxis yAxis, XYDataset dataset,
XYItemRenderer renderer, XYTimeSeriesPlotBuilderElements elements) {

final XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);

Expand All @@ -100,7 +100,7 @@ public static XYPlot createPlot(final ValueAxis xAxis, NumberAxis yAxis, XYDatas
plot.setDomainMinorGridlinesVisible(elements.minorGrid());
plot.setDomainMinorGridlinePaint(elements.minorGridColor());
plot.setDomainMinorGridlineStroke(elements.minorGridStyle());

plot.setRangeMinorGridlinesVisible(elements.minorGrid());
plot.setRangeMinorGridlinePaint(elements.minorGridColor());
plot.setRangeMinorGridlineStroke(elements.minorGridStyle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,62 @@ private CandlestickRendererBuilder() {
downColor = BuilderConstants.DEFAULT_DOWN_COLOR;
}

/**
* Factory method for obtaining new instances of this class.
*
* @return New instance of this class
*/
public static CandlestickRendererBuilder get() {
return new CandlestickRendererBuilder();
}

/**
* Sets the close-up candle color to be used when rendering candles.
*
* @param c The {@link Color} to use
* @return Reference to this builder for chaining method calls
*/
public CandlestickRendererBuilder upColor(Color c) {
Objects.requireNonNull(c, "Color cannot be set to null");
upColor = c;
return this;
}

/**
* Gets the configured close-up candle color.
*
* @return The color
*/
public Color upColor() {
return upColor;
}

/**
* Sets the close-down candle color to be used when rendering candles.
*
* @param c The {@link Color} to use
* @return Reference to this builder for chaining method calls
*/
public CandlestickRendererBuilder downColor(Color c) {
Objects.requireNonNull(c, "Color cannot be set to null");
downColor = c;
return this;
}

/**
* Gets the configured close-down candle color.
*
* @return The color
*/
public Color downColor() {
return downColor;
}

/**
* Builds the renderer using all configured settings.
*
* @return New instance of a CandlestickRenderer corresponding to all configured settings
*/
public CandlestickRenderer build() {

CandlestickRenderer renderer = new CandlestickRenderer();
Expand Down
Loading

0 comments on commit 6fc6fb1

Please sign in to comment.