stable/docs/sql_cheatsheet.md

135 lines
3.2 KiB
Markdown
Raw Normal View History

2018-01-02 02:17:10 +00:00
# SQL Helper
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
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
```
## Open the DB
2018-01-02 02:17:10 +00:00
```bash
sqlite3
.open <filepath>
```
## Table structure
### List tables
2018-01-02 02:17:10 +00:00
```bash
.tables
```
### Display table structure
2018-01-02 02:17:10 +00:00
```bash
.schema <table_name>
```
### Trade table structure
2018-01-02 02:17:10 +00:00
```sql
CREATE TABLE trades
id INTEGER NOT NULL,
exchange VARCHAR NOT NULL,
pair VARCHAR NOT NULL,
is_open BOOLEAN NOT NULL,
fee_open FLOAT NOT NULL,
fee_open_cost FLOAT,
fee_open_currency VARCHAR,
fee_close FLOAT NOT NULL,
fee_close_cost FLOAT,
fee_close_currency VARCHAR,
open_rate FLOAT,
open_rate_requested FLOAT,
open_trade_price FLOAT,
close_rate FLOAT,
close_rate_requested FLOAT,
close_profit FLOAT,
close_profit_abs FLOAT,
stake_amount FLOAT NOT NULL,
amount FLOAT,
open_date DATETIME NOT NULL,
close_date DATETIME,
open_order_id VARCHAR,
stop_loss FLOAT,
stop_loss_pct FLOAT,
initial_stop_loss FLOAT,
initial_stop_loss_pct FLOAT,
stoploss_order_id VARCHAR,
stoploss_last_update DATETIME,
max_rate FLOAT,
min_rate FLOAT,
sell_reason VARCHAR,
strategy VARCHAR,
2020-06-02 08:02:24 +00:00
timeframe INTEGER,
PRIMARY KEY (id),
CHECK (is_open IN (0, 1))
2018-01-02 02:17:10 +00:00
);
CREATE INDEX ix_trades_stoploss_order_id ON trades (stoploss_order_id);
CREATE INDEX ix_trades_pair ON trades (pair);
CREATE INDEX ix_trades_is_open ON trades (is_open);
2018-01-02 02:17:10 +00:00
```
## Get all trades in the table
```sql
SELECT * FROM trades;
```
2019-03-08 21:15:03 +00:00
## Fix trade still open after a manual sell on the exchange
2018-01-02 02:17:10 +00:00
2019-03-23 18:43:23 +00:00
!!! Warning
2019-04-04 19:05:26 +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, forcesell <tradeid> should be used to accomplish the same thing.
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
2019-04-04 18:53:28 +00:00
This should not be necessary after /forcesell, as forcesell 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
SET is_open=0,
close_date=<close_date>,
close_rate=<close_rate>,
close_profit=close_rate/open_rate-1,
close_profit_abs = (amount * <close_rate> * (1 - fee_close) - (amount * open_rate * 1 - fee_open),
sell_reason=<sell_reason>
2018-01-02 02:17:10 +00:00
WHERE id=<trade_ID_to_update>;
```
### Example
2019-03-06 20:37:52 +00:00
2018-01-02 02:17:10 +00:00
```sql
UPDATE trades
SET is_open=0,
close_date='2017-12-20 03:08:45.103418',
close_rate=0.19638016,
close_profit=0.0496,
close_profit_abs = (amount * 0.19638016 * (1 - fee_close) - (amount * open_rate * 1 - fee_open)
sell_reason='force_sell'
2018-01-02 02:17:10 +00:00
WHERE id=31;
```
## Insert manually a new trade
```sql
2019-03-06 20:37:52 +00:00
INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date)
VALUES ('bittrex', 'ETH/BTC', 1, 0.0025, 0.0025, <open_rate>, <stake_amount>, <amount>, '<datetime>')
```
2019-03-06 20:37:52 +00:00
##### Example:
```sql
2019-03-06 20:37:52 +00:00
INSERT INTO trades (exchange, pair, is_open, fee_open, fee_close, open_rate, stake_amount, amount, open_date)
VALUES ('bittrex', 'ETH/BTC', 1, 0.0025, 0.0025, 0.00258580, 0.002, 0.7715262081, '2017-11-28 12:44:24.000000')
```