Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced NumFactory and other factories #1147

Merged
merged 18 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog for `ta4j`, roughly following [keepachangelog.com](http://keepachangel
### Breaking
- Updated project Java JDK from 11 > 21
- Updated Github workflows to use JDK 21
- Extracted NumFactory as source of numbers with defined precision

### Fixed

Expand Down
32 changes: 32 additions & 0 deletions ta4j-core/src/main/java/org/ta4j/core/BarBuilderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2024 Ta4j Organization & respective
* authors (see AUTHORS)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ta4j.core;

/**
* A factory that provides builders of bars.
*/
public interface BarBuilderFactory {

BaseBarConvertibleBuilder createBarBuilder(BarSeries series);
}
193 changes: 9 additions & 184 deletions ta4j-core/src/main/java/org/ta4j/core/BarSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
package org.ta4j.core;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.function.Function;

import org.ta4j.core.num.Num;
import org.ta4j.core.num.NumFactory;

/**
* A {@code BarSeries} is a sequence of {@link Bar bars} separated by a
Expand All @@ -49,56 +46,19 @@
public interface BarSeries extends Serializable {

/**
* @return the name of the series
*/
String getName();

/**
* @return any instance of Num to determine its Num type and function.
* @return factory that generates numbers usable in this BarSeries
*/
Num num();
NumFactory numFactory();

/**
* Returns the underlying function to transform a Number into the Num
* implementation used by this bar series
*
* @return a function Number -> Num
* @return builder that generates compatible bars
*/
default Function<Number, Num> function() {
return num().function();
}

/**
* @return the Num of 0
*/
default Num zero() {
return num().zero();
}
BaseBarConvertibleBuilder barBuilder();

/**
* @return the Num of 1
*/
default Num one() {
return num().one();
}

/**
* @return the Num of 100
*/
default Num hundred() {
return num().hundred();
}

/**
* Transforms a {@link Number} into the {@link Num implementation} used by this
* bar series
*
* @param number a {@link Number} implementing object.
* @return the corresponding value as a Num implementing object
* @return the name of the series
*/
default Num numOf(Number number) {
return num().function().apply(number);
}
String getName();

/**
* Gets the bar from {@link #getBarData()} with index {@code i}.
Expand Down Expand Up @@ -216,8 +176,6 @@ default String getSeriesPeriodDescription() {
* Exceeding bars are removed.
*
* @param bar the bar to be added
* @apiNote to add bar data directly you can use
* {@link #addBar(Duration, ZonedDateTime, Num, Num, Num, Num, Num)}
* @see BarSeries#setMaximumBarCount(int)
*/
default void addBar(Bar bar) {
Expand All @@ -237,121 +195,10 @@ default void addBar(Bar bar) {
* @param replace true to replace the latest bar. Some exchanges continuously
* provide new bar data in the respective period, e.g. 1 second
* in 1 minute duration.
* @apiNote to add bar data directly you can use
* {@link #addBar(Duration, ZonedDateTime, Num, Num, Num, Num, Num)}
* @see BarSeries#setMaximumBarCount(int)
*/
void addBar(Bar bar, boolean replace);

/**
* Adds the {@code bar} at the end of the series.
*
* <p>
* The {@code beginIndex} is set to {@code 0} if not already initialized.<br>
* The {@code endIndex} is set to {@code 0} if not already initialized, or
* incremented if it matches the end of the series.<br>
* Exceeding bars are removed.
*
* @param timePeriod the {@link Duration} of this bar
* @param endTime the {@link ZonedDateTime end time} of this bar
* @apiNote to add bar data directly you can use
* {@link #addBar(Duration, ZonedDateTime, Num, Num, Num, Num, Num)}
* @see BarSeries#setMaximumBarCount(int)
*/
void addBar(Duration timePeriod, ZonedDateTime endTime);

default void addBar(ZonedDateTime endTime, Number openPrice, Number highPrice, Number lowPrice, Number closePrice) {
this.addBar(endTime, numOf(openPrice), numOf(highPrice), numOf(lowPrice), numOf(closePrice), zero(), zero());
}

default void addBar(ZonedDateTime endTime, Number openPrice, Number highPrice, Number lowPrice, Number closePrice,
Number volume) {
this.addBar(endTime, numOf(openPrice), numOf(highPrice), numOf(lowPrice), numOf(closePrice), numOf(volume));
}

default void addBar(ZonedDateTime endTime, Number openPrice, Number highPrice, Number lowPrice, Number closePrice,
Number volume, Number amount) {
this.addBar(endTime, numOf(openPrice), numOf(highPrice), numOf(lowPrice), numOf(closePrice), numOf(volume),
numOf(amount));
}

default void addBar(Duration timePeriod, ZonedDateTime endTime, Number openPrice, Number highPrice, Number lowPrice,
Number closePrice, Number volume) {
this.addBar(timePeriod, endTime, numOf(openPrice), numOf(highPrice), numOf(lowPrice), numOf(closePrice),
numOf(volume), zero());
}

default void addBar(Duration timePeriod, ZonedDateTime endTime, Number openPrice, Number highPrice, Number lowPrice,
Number closePrice, Number volume, Number amount) {
this.addBar(timePeriod, endTime, numOf(openPrice), numOf(highPrice), numOf(lowPrice), numOf(closePrice),
numOf(volume), numOf(amount));
}

default void addBar(ZonedDateTime endTime, String openPrice, String highPrice, String lowPrice, String closePrice) {
this.addBar(endTime, numOf(new BigDecimal(openPrice)), numOf(new BigDecimal(highPrice)),
numOf(new BigDecimal(lowPrice)), numOf(new BigDecimal(closePrice)), zero(), zero());
}

default void addBar(ZonedDateTime endTime, String openPrice, String highPrice, String lowPrice, String closePrice,
String volume) {
this.addBar(endTime, numOf(new BigDecimal(openPrice)), numOf(new BigDecimal(highPrice)),
numOf(new BigDecimal(lowPrice)), numOf(new BigDecimal(closePrice)), numOf(new BigDecimal(volume)),
zero());
}

default void addBar(ZonedDateTime endTime, String openPrice, String highPrice, String lowPrice, String closePrice,
String volume, String amount) {
this.addBar(endTime, numOf(new BigDecimal(openPrice)), numOf(new BigDecimal(highPrice)),
numOf(new BigDecimal(lowPrice)), numOf(new BigDecimal(closePrice)), numOf(new BigDecimal(volume)),
numOf(new BigDecimal(amount)));
}

default void addBar(ZonedDateTime endTime, Num openPrice, Num highPrice, Num lowPrice, Num closePrice, Num volume) {
this.addBar(endTime, openPrice, highPrice, lowPrice, closePrice, volume, zero());
}

/**
* Adds a new {@code Bar} to the bar series.
*
* @param endTime end time of the bar
* @param openPrice the open price
* @param highPrice the high/max price
* @param lowPrice the low/min price
* @param closePrice the last/close price
* @param volume the volume (default zero)
* @param amount the amount (default zero)
*/
void addBar(ZonedDateTime endTime, Num openPrice, Num highPrice, Num lowPrice, Num closePrice, Num volume,
Num amount);

/**
* Adds a new {@code Bar} to the bar series.
*
* @param endTime end time of the bar
* @param openPrice the open price
* @param highPrice the high/max price
* @param lowPrice the low/min price
* @param closePrice the last/close price
* @param volume the volume (default zero)
*/
void addBar(Duration timePeriod, ZonedDateTime endTime, Num openPrice, Num highPrice, Num lowPrice, Num closePrice,
Num volume);

/**
* Adds a new {@code Bar} to the bar series.
*
* @param timePeriod the time period of the bar
* @param endTime end time of the bar
* @param openPrice the open price
* @param highPrice the high/max price
* @param lowPrice the low/min price
* @param closePrice the last/close price
* @param volume the volume (default zero)
* @param amount the amount (default zero)
*/
void addBar(Duration timePeriod, ZonedDateTime endTime, Num openPrice, Num highPrice, Num lowPrice, Num closePrice,
Num volume, Num amount);

/**
* Adds a trade and updates the close price of the last bar.
*
Expand All @@ -360,18 +207,7 @@ void addBar(Duration timePeriod, ZonedDateTime endTime, Num openPrice, Num highP
* @see Bar#addTrade(Num, Num)
*/
default void addTrade(Number tradeVolume, Number tradePrice) {
addTrade(numOf(tradeVolume), numOf(tradePrice));
}

/**
* Adds a trade and updates the close price of the last bar.
*
* @param tradeVolume the traded volume
* @param tradePrice the price
* @see Bar#addTrade(Num, Num)
*/
default void addTrade(String tradeVolume, String tradePrice) {
addTrade(numOf(new BigDecimal(tradeVolume)), numOf(new BigDecimal(tradePrice)));
addTrade(numFactory().numOf(tradeVolume), numFactory().numOf(tradePrice));
}

/**
Expand All @@ -392,17 +228,6 @@ default void addTrade(String tradeVolume, String tradePrice) {
*/
void addPrice(Num price);

/**
* Updates the close price of the last bar. The open, high and low prices are
* also updated as needed.
*
* @param price the price for the bar
* @see Bar#addPrice(Num)
*/
default void addPrice(String price) {
addPrice(new BigDecimal(price));
}

/**
* Updates the close price of the last bar. The open, high and low prices are
* also updated as needed.
Expand All @@ -411,7 +236,7 @@ default void addPrice(String price) {
* @see Bar#addPrice(Num)
*/
default void addPrice(Number price) {
addPrice(numOf(price));
addPrice(numFactory().numOf(price));
}

/**
Expand Down
Loading
Loading