Documentation, examples and further information of the ta4j project
This project is maintained by ta4j Organization
This page explains one of the most common first live-trading pitfalls in ta4j: repeated buys (or sells) while the same candle is still open.
BarSeries.Repeated entries usually come from one or more integration issues:
strategy.shouldEnter(index) instead of strategy.shouldEnter(index, tradingRecord)tradingRecordWhen this happens, ta4j can keep seeing the entry rule as true and still think no position is open.
Use this mode if you want less noise and one decision per completed bar:
tradingRecord into shouldEnter/shouldExitint index = series.getEndIndex(); // latest closed bar
if (strategy.shouldEnter(index, tradingRecord)) {
submitBuyOrder();
}
Use this mode if you want intra-candle reactions:
tradingRecordint lastEntryBarIndex = -1;
while (true) {
int index = series.getEndIndex();
if (strategy.shouldEnter(index, tradingRecord) && index != lastEntryBarIndex) {
submitBuyOrder();
lastEntryBarIndex = index;
}
}
This is critical:
After your exchange confirms execution, write it into ta4j (enter/exit or operate(fill)), so strategy state and broker state stay aligned.
if (strategy.shouldEnter(index, tradingRecord)) {
ExchangeOrder order = submitBuyOrder();
if (order.isFilled()) {
tradingRecord.enter(index, fillPrice, fillAmount);
}
}
If your venue gives partial fills, use TradingRecord.operate(fill) as fills arrive.
shouldEnter(index, tradingRecord) and shouldExit(index, tradingRecord)