2018-01-02 02:17:10 +00:00
# SQL Helper
2020-05-13 04:50:52 +00:00
2019-04-04 18:53:28 +00:00
This page contains some help if you want to edit your sqlite db.
2018-01-02 02:17:10 +00:00
## Install sqlite3
2020-05-13 04:50:52 +00:00
Sqlite3 is a terminal based sqlite application.
Feel free to use a visual Database editor like SqliteBrowser if you feel more comfortable with that.
### Ubuntu/Debian installation
2018-01-02 02:17:10 +00:00
```bash
sudo apt-get install sqlite3
```
2020-07-11 05:29:34 +00:00
### Using sqlite3 via docker-compose
The freqtrade docker image does contain sqlite3, so you can edit the database without having to install anything on the host system.
``` bash
docker-compose exec freqtrade /bin/bash
2021-05-22 08:12:03 +00:00
sqlite3 < database-file > .sqlite
2020-07-11 05:29:34 +00:00
```
2018-01-02 02:17:10 +00:00
## Open the DB
2020-05-13 04:50:52 +00:00
2018-01-02 02:17:10 +00:00
```bash
sqlite3
.open < filepath >
```
## Table structure
### List tables
2020-05-13 04:50:52 +00:00
2018-01-02 02:17:10 +00:00
```bash
.tables
```
### Display table structure
2020-05-13 04:50:52 +00:00
2018-01-02 02:17:10 +00:00
```bash
.schema < table_name >
```
## Get all trades in the table
```sql
SELECT * FROM trades;
```
2022-03-09 06:39:32 +00:00
## Fix trade still open after a manual exit on the exchange
2018-01-02 02:17:10 +00:00
2019-03-23 18:43:23 +00:00
!!! Warning
2022-04-08 05:15:05 +00:00
Manually selling a pair on the exchange will not be detected by the bot and it will try to sell anyway. Whenever possible, /forceexit < tradeid > should be used to accomplish the same thing.
2020-10-22 05:42:47 +00:00
It is strongly advised to backup your database file before making any manual changes.
2019-03-08 21:15:03 +00:00
2019-03-23 18:43:23 +00:00
!!! Note
2022-04-08 05:15:05 +00:00
This should not be necessary after /forceexit, as force_exit orders are closed automatically by the bot on the next iteration.
2019-03-06 20:37:52 +00:00
2018-01-02 02:17:10 +00:00
```sql
UPDATE trades
2020-04-22 04:39:03 +00:00
SET is_open=0,
close_date=< close_date > ,
close_rate=< close_rate > ,
2020-07-11 05:29:11 +00:00
close_profit = close_rate / open_rate - 1,
2020-08-22 07:33:35 +00:00
close_profit_abs = (amount * <close_rate> * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))),
2022-03-24 19:53:22 +00:00
exit_reason=< exit_reason >
2018-01-02 02:17:10 +00:00
WHERE id=< trade_ID_to_update > ;
```
2020-04-22 04:39:03 +00:00
### Example
2019-03-06 20:37:52 +00:00
2018-01-02 02:17:10 +00:00
```sql
UPDATE trades
2020-04-22 04:39:03 +00:00
SET is_open=0,
2020-07-11 05:29:11 +00:00
close_date='2020-06-20 03:08:45.103418',
2020-04-22 04:39:03 +00:00
close_rate=0.19638016,
close_profit=0.0496,
2020-08-22 07:33:35 +00:00
close_profit_abs = (amount * 0.19638016 * (1 - fee_close) - (amount * (open_rate * (1 - fee_open)))),
2022-04-04 14:59:27 +00:00
exit_reason='force_exit'
2018-01-02 02:17:10 +00:00
WHERE id=31;
```
2020-07-11 05:29:11 +00:00
## Remove trade from the database
2020-10-22 05:42:47 +00:00
!!! Tip "Use RPC Methods to delete trades"
Consider using `/delete <tradeid>` via telegram or rest API. That's the recommended way to deleting trades.
If you'd still like to remove a trade from the database directly, you can use the below query.
2020-07-11 05:29:11 +00:00
```sql
DELETE FROM trades WHERE id = < tradeid > ;
```
```sql
DELETE FROM trades WHERE id = 31;
```
!!! Warning
2020-07-12 10:36:16 +00:00
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.
2021-05-22 08:12:03 +00:00
2022-06-09 17:51:21 +00:00
!!! Danger
Some systems (Ubuntu) disable foreign keys in their sqlite3 implementation. When using sqlite3 - please ensure that foreign keys are on by running `PRAGMA foreign_keys = ON` before the above query.
2021-05-22 08:12:03 +00:00
## Use a different database system
!!! Warning
By using one of the below database systems, you acknowledge that you know how to manage such a system. Freqtrade will not provide any support with setup or maintenance (or backups) of the below database systems.
### PostgreSQL
Freqtrade supports PostgreSQL by using SQLAlchemy, which supports multiple different database systems.
Installation:
2021-07-31 14:36:28 +00:00
`pip install psycopg2-binary`
2021-05-22 08:12:03 +00:00
Usage:
`... --db-url postgresql+psycopg2://<username>:<password>@localhost:5432/<database>`
Freqtrade will automatically create the tables necessary upon startup.
If you're running different instances of Freqtrade, you must either setup one database per Instance or use different users / schemas for your connections.
### MariaDB / MySQL
Freqtrade supports MariaDB by using SQLAlchemy, which supports multiple different database systems.
Installation:
`pip install pymysql`
Usage:
`... --db-url mysql+pymysql://<username>:<password>@localhost:3306/<database>`