Skip to content

Instantly share code, notes, and snippets.

@straumat
Created November 29, 2021 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save straumat/d5802a7efc589e860d1477bb1e457d1a to your computer and use it in GitHub Desktop.
Save straumat/d5802a7efc589e860d1477bb1e457d1a to your computer and use it in GitHub Desktop.
package production.tech.cassandre.trading.bot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.UnderIndicatorRule;
import tech.cassandre.trading.bot.dto.position.PositionCreationResultDTO;
import tech.cassandre.trading.bot.dto.position.PositionDTO;
import tech.cassandre.trading.bot.dto.position.PositionRulesDTO;
import tech.cassandre.trading.bot.dto.user.AccountDTO;
import tech.cassandre.trading.bot.dto.util.CurrencyPairDTO;
import tech.cassandre.trading.bot.strategy.BasicTa4jCassandreStrategy;
import tech.cassandre.trading.bot.strategy.CassandreStrategy;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static tech.cassandre.trading.bot.dto.position.PositionStatusDTO.CLOSED;
import static tech.cassandre.trading.bot.dto.position.PositionStatusDTO.OPENED;
import static tech.cassandre.trading.bot.dto.util.CurrencyDTO.ETH;
import static tech.cassandre.trading.bot.dto.util.CurrencyDTO.USDT;
/**
* Uniswap strategy.
*/
@CassandreStrategy(
strategyId = "001",
strategyName = "ETH")
public final class ETHStrategy extends BasicTa4jCassandreStrategy {
/** Number of bars. */
private static final int NUMBER_OF_BARS = 24;
/** Stop gain percentage. */
private static final float STOP_GAIN_PERCENTAGE = 6;
/** Stop loss percentage. */
private static final float STOP_LOSS_PERCENTAGE = 15;
/** Currency pair. */
private static final CurrencyPairDTO POSITION_CURRENCY_PAIR = new CurrencyPairDTO(ETH, USDT);
/** Position amount. */
private static final BigDecimal POSITION_AMOUNT = new BigDecimal("0.02");
/** Logger. */
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
@Override
public CurrencyPairDTO getRequestedCurrencyPair() {
return POSITION_CURRENCY_PAIR;
}
@Override
public Optional<AccountDTO> getTradeAccount(final Set<AccountDTO> accounts) {
if (accounts.size() == 1) {
return accounts.stream().findFirst();
} else {
return accounts.stream()
.filter(a -> "trade".equals(a.getName()))
.findFirst();
}
}
@Override
public void onPositionsStatusUpdates(final Map<Long, PositionDTO> positions) {
positions.values().forEach(positionDTO -> {
if (positionDTO.getStatus() == OPENED || positionDTO.getStatus() == CLOSED) {
System.out.println("Binance " + positionDTO.getStrategy().getName() + " position " + positionDTO.getPositionId() + " " + positionDTO.getStatus().toString().toLowerCase());
}
});
}
@Override
public int getMaximumBarCount() {
return NUMBER_OF_BARS;
}
@Override
public Duration getDelayBetweenTwoBars() {
return Duration.ofHours(1);
}
@Override
public Strategy getStrategy() {
ClosePriceIndicator closePrice = new ClosePriceIndicator(getSeries());
SMAIndicator sma = new SMAIndicator(closePrice, getMaximumBarCount());
return new BaseStrategy(new OverIndicatorRule(sma, closePrice), new UnderIndicatorRule(sma, closePrice));
}
@Override
public void shouldEnter() {
// Creating the position.
if (canBuy(POSITION_AMOUNT)) {
// Create rules.
PositionRulesDTO rules = PositionRulesDTO.builder()
.stopGainPercentage(STOP_GAIN_PERCENTAGE)
.stopLossPercentage(STOP_LOSS_PERCENTAGE)
.build();
// Create position.
final PositionCreationResultDTO result = createLongPosition(
POSITION_CURRENCY_PAIR,
POSITION_AMOUNT,
rules);
logger.info("Ether long position created: " + result.getPosition().getPositionId());
}
}
@Override
public void shouldExit() {
}
}
@andrelimao
Copy link

is there any classes or just this one?

@andrelimao
Copy link

and why do you importSimpleMailMessage and JavaMailSender if you never use them?

@andrelimao
Copy link

why the number of bars is setted to 24 and where do you set the timeframe?

@straumat
Copy link
Author

@andrelimao cassande does not support ta4j out of the box now, you should look at the new examples.
You can create one easily, look at the documentation: https://trading-bot.cassandre.tech/tutorial/create-your-project.html

@andrelimao
Copy link

out of the box? what do you mean? and how to set a timeframe for ta4j indicators?

@straumat
Copy link
Author

I mean that the sample provide a strategy. You should look at ta4j documentation to set time frame

@andrelimao
Copy link

why number of bars = 24?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment