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 live trading LiveIndicator #1149

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog for `ta4j`, roughly following [keepachangelog.com](http://keepachangelog.com/en/1.0.0/) from version 0.9 onwards.

## 0.16 (unreleased)
- **Added LiveIndicator for live trading side channels

### Breaking
- **Upgraded to Java 11**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* 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.indicators;

import java.util.function.Supplier;

import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.num.Num;

/**
* This indicator is usable in live trading as side channel for data analysis.
*
* For example exchange may send profit calculations and we want base entry exit
* rules on that information.
*/
class LiveIndicator implements Indicator<Num> {

private final Supplier<Num> valueSupplier;
private final BarSeries series;

public LiveIndicator(final BarSeries series, final Supplier<Number> valueSupplier) {
this.series = series;
this.valueSupplier = () -> numOf(valueSupplier.get());
}

@Override
public Num getValue(final int index) {
return this.valueSupplier.get();
}

@Override
public int getUnstableBars() {
return 0;
}

@Override
public BarSeries getBarSeries() {
return this.series;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.ta4j.core.indicators;

import static org.junit.Assert.assertEquals;

import java.util.function.Function;

import org.junit.Test;
import org.ta4j.core.BaseBarSeries;
import org.ta4j.core.num.Num;

public class LiveIndicatorTest extends AbstractIndicatorTest {

public LiveIndicatorTest(final Function<Number, Num> function) {
super(function);
}

@Test
public void getValue() {
final var liveIndicator = new LiveIndicator(new BaseBarSeries() {
@Override
public Num numOf(final Number number) {
return LiveIndicatorTest.super.numOf(number);
}
}, () -> 37.5);

final var indicatorValue = liveIndicator.getValue(4534534);
assertEquals(numOf(37.5), indicatorValue);
}
}