| 1 | # SQL Helper |
| 2 | |
| 3 | This page contains some help if you want to edit your sqlite db. |
| 4 | |
| 5 | ## Install sqlite3 |
| 6 | |
| 7 | Sqlite3 is a terminal based sqlite application. |
| 8 | Feel free to use a visual Database editor like SqliteBrowser if you feel more comfortable with that. |
| 9 | |
| 10 | ### Ubuntu/Debian installation |
| 11 | |
| 12 | ```bash |
| 13 | sudo apt-get install sqlite3 |
| 14 | ``` |
| 15 | |
| 16 | ### Using sqlite3 via docker-compose |
| 17 | |
| 18 | The freqtrade docker image does contain sqlite3, so you can edit the database without having to install anything on the host system. |
| 19 | |
| 20 | ``` bash |
| 21 | docker-compose exec freqtrade /bin/bash |
| 22 | sqlite3 <databasefile>.sqlite |
| 23 | ``` |
| 24 | |
| 25 | ## Open the DB |
| 26 | |
| 27 | ```bash |
| 28 | sqlite3 |
| 29 | .open <filepath> |
| 30 | ``` |
| 31 | |
| 32 | ## Table structure |
| 33 | |
| 34 | ### List tables |
| 35 | |
| 36 | ```bash |
| 37 | .tables |
| 38 | ``` |
| 39 | |
| 40 | ### Display table structure |
| 41 | |
| 42 | ```bash |
| 43 | .schema <table_name> |
| 44 | ``` |
| 45 | |
| 46 | ## Get all trades in the table |
| 47 | |
| 48 | ```sql |
| 49 | SELECT * FROM trades; |
| 50 | ``` |
| 51 | |
| 52 | ## Fix trade still open after a manual sell on the exchange |
| 53 | |
| 54 | !!! Warning |
| 55 | Manually selling a pair on the exchange will not be detected by the bot and it will try to sell anyway. Whenever possible, forcesell <tradeid> should be used to accomplish the same thing. |
| 56 | It is strongly advised to backup your database file before making any manual changes. |
| 57 | |
| 58 | !!! Note |
| 59 | This should not be necessary after /forcesell, as forcesell orders are closed automatically by the bot on the next iteration. |
| 60 | |
| 61 | ```sql |
| 62 | UPDATE trades |
| 63 | SET is_open=0, |
| 64 | close_date=<close_date>, |
| 65 | close_rate=<close_rate>, |
| 66 | close_profit = close_rate / open_rate - 1, |
| 67 | close_profit_abs = (amount * <close_rate> * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))), |
| 68 | sell_reason=<sell_reason> |
| 69 | WHERE id=<trade_ID_to_update>; |
| 70 | ``` |
| 71 | |
| 72 | ### Example |
| 73 | |
| 74 | ```sql |
| 75 | UPDATE trades |
| 76 | SET is_open=0, |
| 77 | close_date='2020-06-20 03:08:45.103418', |
| 78 | close_rate=0.19638016, |
| 79 | close_profit=0.0496, |
| 80 | close_profit_abs = (amount * 0.19638016 * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))), |
| 81 | sell_reason='force_sell' |
| 82 | WHERE id=31; |
| 83 | ``` |
| 84 | |
| 85 | ## Remove trade from the database |
| 86 | |
| 87 | !!! Tip "Use RPC Methods to delete trades" |
| 88 | Consider using `/delete <tradeid>` via telegram or rest API. That's the recommended way to deleting trades. |
| 89 | |
| 90 | If you'd still like to remove a trade from the database directly, you can use the below query. |
| 91 | |
| 92 | ```sql |
| 93 | DELETE FROM trades WHERE id = <tradeid>; |
| 94 | ``` |
| 95 | |
| 96 | ```sql |
| 97 | DELETE FROM trades WHERE id = 31; |
| 98 | ``` |
| 99 | |
| 100 | !!! Warning |
| 101 | This will remove this trade from the database. Please make sure you got the correct id and **NEVER** run this query without the `where` clause. |