Created
September 3, 2022 21:45
-
-
Save NotArchon/fc9413e6268ccebdda18c96dd8b269c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 java.net.URL; | |
| import java.util.LinkedHashMap; | |
| import java.util.Map; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.stream.Stream; | |
| @StrategyInfo( | |
| name = "Example Strategy - 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) { | |
| btc_5m_listener.start(this::updateBtc5m); | |
| eth_5m_listener.start(this::updateEth5m); | |
| } | |
| @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.smoothed(closingPrices, 21); | |
| double ema50 = MA.smoothed(closingPrices, 50); | |
| double ema200 = MA.smoothed(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.smoothed(closingPrices, 21); | |
| double ema50 = MA.smoothed(closingPrices, 50); | |
| double ema200 = MA.smoothed(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