Skip to content

Instantly share code, notes, and snippets.

@NotArchon
Created September 20, 2022 21:13
Show Gist options
  • Select an option

  • Save NotArchon/beba24e46cc6a5a851ac9c7010d9496e to your computer and use it in GitHub Desktop.

Select an option

Save NotArchon/beba24e46cc6a5a851ac9c7010d9496e to your computer and use it in GitHub Desktop.
package io.archon;
import lombok.Getter;
import org.discrypto.api.model.candle.Candle;
import org.discrypto.api.model.candle.CandleListener;
import org.discrypto.api.model.exchange.SupportedExchange;
import org.discrypto.api.model.indicators.MA;
import org.discrypto.api.model.indicators.RSI;
import org.discrypto.api.model.strategy.Strategy;
import org.discrypto.api.model.strategy.StrategyInfo;
import org.discrypto.api.model.strategy.StrategyProperty;
import org.discrypto.api.model.strategy.events.StartEvent;
import org.discrypto.api.model.strategy.events.StopEvent;
import org.discrypto.api.utility.eventbus.Subscribe;
import org.discrypto.client.utility.utils.ThreadUtils;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@StrategyInfo(
name = "RSI & Moving Averages",
version = "1.0",
author = "Archon",
authorUserId = 1,
threadId = 1337,
description = "Here's an example strategy that prints BTC-USD and ETH-USD moving averages."
)
public class ExampleStrategy extends Strategy {
@Getter private final SupportedExchange defaultExchange = SupportedExchange.COINBASE;
@Getter private final String defaultPair = "ETH-USD";
private final StrategyProperty maxFiatSpendAmount = property("max_fiat_spend_amount", -1,
"The maximum amount of fiat the bot is allowed to spend per trade.",
"This value controls how much of your accumulated balance the bot is allowed to trade.",
"Set this value to -1 to allow the bot to spend entire accumulated value per trade."
);
private final StrategyProperty testProperty1 = property(
"test_1", 123,
"...test"
);
private final StrategyProperty testProperty2 = property(
"test_2", 456,
"...test"
);
private final StrategyProperty testProperty3 = property(
"test_3", 789,
"...test"
);
private final StrategyProperty testProperty4 = property(
"test_4", "aaa",
"...test"
);
private final StrategyProperty testProperty5 = property(
"test_5", "bbb",
"...test"
);
private final StrategyProperty testProperty6 = property(
"test_6", "ccc",
"...test"
);
private final CandleListener btc_5m_listener = new CandleListener()
.chart("BTC-USD", 5, TimeUnit.MINUTES)
.minimum(300);
private final CandleListener eth_5m_listener = new CandleListener()
.chart("ETH-USD", 5, TimeUnit.MINUTES)
.minimum(300);
private final Map<String, Object> ethData = new LinkedHashMap<>();
private final Map<String, Object> btcData = new LinkedHashMap<>();
@Subscribe
public void onStart(StartEvent e) {
System.err.println("START EXAMPLE STRAT!");
System.err.println();
System.err.println("@@@a");
System.err.println("\n"); //will this show two skips regularly?
System.err.println("@@@b");
btc_5m_listener.start(this::updateBtc5m);
eth_5m_listener.start(this::updateEth5m);
int i = 0;
while(i < 100) {
System.err.println(++i);
ThreadUtils.sleep(100L);
}
System.exit(1);
}
@Subscribe
public void onStop(StopEvent e) {
}
private void updateBtc5m(CandleListener l) {
Candle current = l.current();
btcData.put("current", current);
Candle[] candles = l.candles();
double[] closingPrices = Stream.of(candles)
.mapToDouble(p -> p.getClose().getPrice())
.toArray();
double ema21 = MA.exponential(closingPrices, 21);
double ema50 = MA.exponential(closingPrices, 50);
double ema200 = MA.exponential(closingPrices, 200);
double rsi14 = RSI.calculate(candles, 14);
Map<String, Object> indicators = new LinkedHashMap<>();
indicators.put("ema21", ema21);
indicators.put("ema50", ema50);
indicators.put("ema200", ema200);
indicators.put("rsi14", rsi14);
btcData.put("indicators", indicators);
}
private void updateEth5m(CandleListener l) {
Candle current = l.current();
ethData.put("current", current);
Candle[] candles = l.candles();
double[] closingPrices = Stream.of(candles)
.mapToDouble(p -> p.getClose().getPrice())
.toArray();
double ema21 = MA.exponential(closingPrices, 21);
double ema50 = MA.exponential(closingPrices, 50);
double ema200 = MA.exponential(closingPrices, 200);
double rsi14 = RSI.calculate(candles, 14);
Map<String, Object> indicators = new LinkedHashMap<>();
indicators.put("ema21", ema21);
indicators.put("ema50", ema50);
indicators.put("ema200", ema200);
indicators.put("rsi14", rsi14);
ethData.put("indicators", indicators);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment