WORK IN PROGRESS
It allows:
- to select a crypto pair to trade from the available pairs
- to create a custom strategy for your specific needs based on the available rules
- to send buy and sell orders on an Exchange
- to log the trades into a file (orderLog.txt) in the project root directory
Planned features:
- Allow to trade with a fraction of your quote balance to be able to share it between multiple bots
- Add UI for cryptocurrency and strategy selection
- Implementation of threads to allow for spinning up multiple bots and remote management through the UI
- Log transactions to Database
- Make the bot more Exchange agnostic
- BTCUSDT
- BTCBUSD
- ETHUSDT
- ETHBUSD
A strategy is always defined by an entrance rule (i.e., when to enter a trade), and an exit rule
(i.e., when to exit a trade).
Each rule takes one or more indicators as parameters, which are used to determine the rule's outcome.
Tailored rules can be created by composing the available rules and indicators using the AndRule and OrRule.
Your first indicator will be calculated on the closing prices of the last bars. First the indicator needs to be instantiated as follows:
// SMAIndicator(baseIndicator, numberOfPeriods)
Indicator SMA = new SMAIndicator(new PriceIndicator(), 15);Available indicators:
ConstantIndicator: represents a constant valuePriceIndicator: the result of this indicator are the closing prices of the pricesSMAIndicator: represents the Simple Moving Average of the prices which were givenEMAIndicator: represents the Exponential Moving Average of the prices which were givenSubtractIndicator: represents the difference between two indicators
Now, lets say you want to buy when the price is above the SMA and sell when the price is below the SMA. The entrance rule would be:
Rule entranceRule = new OverIndicatorRule(new PriceIndicator(), SMA);The exit rule would be:
Rule exitRule = new UnderIndicatorRule(new PriceIndicator(), SMA);Finally, you can create your strategy:
Strategy strategy = new Strategy(entranceRule, exitRule);Available rules:
OverIndicatorRule: returns true when the value the first indicator is above the second indicator.UnderIndicatorRule: returns true when the value the first indicator is under the second indicator.AndRule: returns true when all the provided rules are true.OrRule: returns true when at least one of the provided rules is true.
If you wanted to build a more complex rule, say the Moving Average Convergence Divergence you'd have to combine the rules and indicators as follows:
// MACDIndicator(baseIndicator, shortPeriod, longPeriod)
Indicator MACD = new MACDIndicator(new PriceIndicator(), 12, 26);
// EMAIndicator(baseIndicator, numberOfPeriods)
Indicator signal = new EMAIndicator(MACD, 9);
// SubtractIndicator(baseIndicator, indicatorToSubtract)
Indicator subtraction = new SubtractIndicator(MACD, signal);
// Buy when MACD is above the signal (i.e., subtraction is positive)
Rule entrance = new OverIndicatorRule(subtraction, new ConstantIndicator(0));
// Sell when MACD is below the signal (i.e., subtraction is negative)
Rule exit = new UnderIndicatorRule(subtraction, new ConstantIndicator(0));Caching of indicators: When an indicator is used multiple times in one strategy (e.g., the MACD in the example above is used as reference once, and twice as base indicator for the signal and subtraction indicators), it is computed only once, and the result is cached for subsequent uses until the next prices are fetched.
Simply instantiate an Exchange corresponding to the exchange you want to trade on:
Exchange exchange = new BinanceExchange();- Binance:
BinanceExchange
Choose between the available symbols:
Symbol symbol = Symbol.BTCUSDT;- Bitcoin / USD Tether:
BTCUSDT - Bitcoin / Binance USD:
BTCBUSD - Ethereum / USD Tether:
ETHUSDT - Ethereum / Binance USD:
ETHBUSD
Period period = Period.FIFTEEN_MINUTES;
``Finally, you can create your bot with all the objects you have created:
Trader trader = new Trader(exchange, period, symbol, strategy);trader.run();!!! Caution : use this bot at your own risk and expenses !!!
Be aware that Binance will take commissions on your trades in either the base crypto your trading or in BNBs if you’ve set up your account to do so.
- Make sure you have JDK 17 (or newer) installed
- Clone the git repository to your computer
cdto the directory- Set following environment variables on your system:
BINANCE_TRADER_ENV="DEV" // 'DEV' Testnet - 'PROD' Actual Binance Exchang !!!! USE AT YOUR OWN RISK !!!!
TESTNET_API_KEY="YOUR_TESTNET_API_KEY" // 'PROD' uses BINANCE_API_KEY
TESTNET_SECRET_KEY="YOUR_TESTNET_SECRET_KEY" // 'PROD' uses BINANCE_SECRET_KEY
- Modify the
bot/src/main/java/org/crypto/bot/App.javafile to create your own strategy - Run
./gradlew build - Run
java -jar bot/build/libs/bot-1.0-SNAPSHOT.jar
- You can call the
TraderAPI directly from your own code
Warning : Development on this feature is in its beginning phase, thus running the bot as a library is experimental and will produce unexpected behaviors.
The webapp is in a non-working state for the moment even though it can be run using an application server on which to deploy the packaged war file.
The bot consumes BinanceConnectorException and BinanceServerException thrown by the Binance Connector when getting
or sending data to the exchange: when they occur, the bot tries to reconnect 5 times before throwing a
BinanceTraderException.
BinanceClientException thrown by the Connector are logged and re-thrown by the bot because they are
unsolvable without human interaction.
- Java
- Jakarta EE
- MariaDB - Database
- Binance Connector for Java - Interaction with Binance
- Gson - Deserialization
- JUnit - Test Suite
- Mockito - Mocking / Stubbing Framework
- Logback - Logging utility
- AmbryN - Initial work - AmbryN