diff --git a/README.md b/README.md index 15f3162..c791c03 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # Warning 🚨 **This repository is in active development and is not working yet**. The bot currently requires you to put in your seed value via terminal. It is planned to use the Tangem wallet in the future so you will never be required to give away your secrets. At the current stage I would never use a bot like this, even if it should work! @@ -22,11 +23,173 @@ It is planned to use the Tangem wallet in the future so you will never be requir A trading bot that uses the decentralized exchange of the XRP Ledger. It uses arbitrage trading and market making to make profit. ## 📖 Basics: +- The XRP Ledger updates apporx. every 4 seconds. - The XRP Ledger has the world's first [`decentralized exchange`](https://xrpl.org/decentralized-exchange.html#decentralized-exchange) (`DEX`). - Orders on the `DEX` are called [`Offers`](https://xrpl.org/offers.html#offers). - This bot only sends [`OfferCreate` transactions](https://xrpl.org/offercreate.html) with the [Immediate or cancel](https://xrpl.org/offercreate.html#offercreate-flags) flag to the ledger. - `Offers` consume other `Offers` in an [order book](https://en.wikipedia.org/wiki/Order_book). For example if you want to buy a currency you place a `bid Offer` that consumes an `ask Offer` that has the same or a better price than the `bid Offer`. - Arbitrage trading and market making are not known to make huge profits. There are known to make profits with low risk. +- The term 'currency' describes both XRP and tokens in this repo. + +## How does the bot operate? +#### Example account: +Classic address: raiuHwMbTTKbtjWTj8FrfpTtVk8RgYVxWH +Seed: sn8gyEtTt4ZcKkeiTPA8t36CbqhHu +Balances: +- XRP: 1.000 +- USD.Bitstamp = 200 +- USD.Gatehub = 150 +- EUR.Gatehub = 100 +- EUR.Bitstamp = 50 + +
1. Get users seed value +

+ +In the current version the user must provide his secret seed value via input from the terminal. +Before the first official release the trading bot will be able to interact with the Tangem wallet. +Advantages of using Tangem: + - The user's key pair is stored safely on the Tangem wallet + - The user does not have to give away their secret seed value + +

+
+
2. Generate wallet +

+ +Generate the wallet using the [`Wallet`](https://github.com/XRPLF/xrpl-py/blob/master/xrpl/wallet/main.py#L12) object from the [xrpl-py](https://github.com/XRPLF/xrpl-py) library. +By doing so, we are able to access the account's classic address (r-address) and sign transactions. + +

+
+
3. Receive the accounts balances once +

+ +Receive all balances the account holds using the [`account_info`](https://xrpl.org/account_info.html) and the [`account_lines`](https://xrpl.org/account_lines.html#account_lines) methods once. The `account_info` method returns the `Balance` field which contains the accounts XRP values expressed in [drops](https://xrpl.org/xrp.html#xrp-properties). The `account_lines` method returns a list of all [tokens](https://xrpl.org/tokens.html) the account holds. Each object in that list contains the `acccount` field (describes the issuer of the token), the `currency` field (describes the currency code, e.g. 'USD') and the `balance` field (describes the amount the account holds of that token). These balances will be stored in the [`XRPWallet`](https://github.com/LimpidCrypto/XRPL-trading-bot/blob/main/xrpl_trading_bot/wallet/main.py#L10) object which is a child class of the [`Wallet`](https://github.com/XRPLF/xrpl-py/blob/master/xrpl/wallet/main.py#L12) object of the xrpl-py library. + +

+
+
4. Subscribe to the accounts transaction stream +

+ +Receive a message every time a transaction affects the user's account. This message include precise information how the transaction affected the ledger and the account. + +

+
+
5. Derive new balances on every new transaction +

+ +Every time the bot receives a message (described in step 4) that a transaction affected the user's account, the bot makes use of a [transaction parser](https://github.com/XRPLF/xrpl-py/pull/342) (currently added in directly to the bot. The parser will be integrated into the xrpl-py library as soon as possible. When integrated the parser will be deleted from the bot.). The transaction parser parses the accounts final balances after the transaction happend and corrects them in the `XRPWallet` object. + +

+
+
6. Parse all possible currency pairs +

+ +Because we now always know what currencies the account holds, we can parse all possible [currency pairs](https://www.investopedia.com/terms/c/currencypair.asp) the account could trade from them. +If we take the above example account the possible currency pairs would be the following: +- XRP/USD.Bitstamp +- XRP/USD.Gatehub +- XRP/EUR.Gatehub +- XRP/EUR.Bitstamp +- USD.Bitstamp/USD.Gatehub +- USD.Bitstamp/EUR.Gatehub +- USD.Bitstamp/EUR.Bitstamp +- USD.Gatehub/EUR.Gatehub +- USD.Gatehub/EUR.Bitstamp +- EUR.Gatehub/EUR.Bitstamp + +

+
+
7. Receive the entire order book for each currency pair once +

+ +Receive the entire [order book](https://www.investopedia.com/terms/o/order-book.asp) for every currency pair once, using the [`subscribe`](https://xrpl.org/subscribe.html) method. The subscription will immediately be canceled after the order book is received because the bot is retreiving it from a full history node. We do not want to load them unnecessarily. The order books will be stored in the [`OrderBooks`](https://github.com/LimpidCrypto/XRPL-trading-bot/blob/main/xrpl_trading_bot/order_books/main.py#L52) object. + +

+
+
8. Subscribe to the transaction stream of each order book +

+ +Receive a message every time a transaction affects the given order book. This message include precise information how the transaction affected the ledger and the order book. + +

+
+
9. Derive new order books on every new transaction +

+ +Everytime the bot receives a new message (described in step 4) that a transaction affected an order book. The bot takes the transaction and the affected order book to see how the transaction changed the order book. It then parses the final state of the order book. To do this it uses the [`parse_final_order_book`](https://github.com/LimpidCrypto/XRPL-trading-bot/blob/main/xrpl_trading_bot/txn_parser/order_book_changes.py#L52) parser. + +

+
+
10. Build trade paths +

+ +You could say that the decentralized exchange of the XRP Ledger is nothing more than a collection of limit orders. Orders are called [`Offers`](https://xrpl.org/offers.html#offers) on the XRP Ledger. Every time the user wants to trade a currency against another he needs to find another participant, who wants to trade the exact same currencies in the other direction, at the same or better exchange rate. For Examlple: +
Person A is willing to pay 10.70 USD in order to receive 10 EUR. To let everybody know he is willing to so he submits an [`OfferCreate`](https://xrpl.org/offercreate.html) transaction to the network. This transaction creates an [`Offer`](https://xrpl.org/offer.html#offer) object on the XRP Ledger which everybody in the network is able to see. Now Person B comes into play. Person B sees that offer of Person A and wants to trade it. So Person B is willing to pay 10 EUR in order to receive 10.70 USD. Person B now submits a `OfferCreate` transaction just as Person A did before. Because both `Offers` have the same exchange rate they are consuming each other. Person A gets 10 EUR from Person B and Person B gets 10.70 USD from Person A. +

You can imagine that the trading bot is Person B. The bot is constantly searching for `Offers` which combined will result in profit due to price differences. If the bot combine to compare two or more `Offers` with each other this is called a *trade path*. +

+## Examples +### Spatial arbitrage trading +#### Soon +### Triangular arbitrage trading +#### Soon +### Market maker +#### Soon + +

+
+
10.5. Adjust values for each trade path +

+ +#### Soon + +

+
+
11. Calulate the numeric and percentage profit/loss of each trade path +

+ +last trade TakerPays amount - first trade TakerGets amount = numeric profit/loss +(1 - (last trade TakerPays amount - first trade TakerGets amount) / first trade TakerGets amount) * 100 = percentage profit/loss + +

+
+
12. Filter for the profitable trade paths +

+ +Filter for positive numeric profit and sort by percentage profit, so the most profitable paths get executed first. + +

+
+ +
13. Build the OfferCreate transactions +

+ +

+
+ +
14. Sign the transactions +

+ +

+
+ +
15. Submit the transactions +

+ +

+
+ +
16. Wait for all transaction results +

+ +

+
+ +
17. Repeat from step 6 +

+ +

+
## 📈 Trading types: