Merge pull request #429 from gcarq/fix/issue-385

Fix dry_run db issue when open_order_id already exist
This commit is contained in:
Samuel Husso 2018-01-24 07:25:26 +02:00 committed by GitHub
commit c968b904de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 1 deletions

View File

@ -47,6 +47,10 @@ def init(config: dict, engine: Optional[Engine] = None) -> None:
Trade.query = session.query_property() Trade.query = session.query_property()
_DECL_BASE.metadata.create_all(engine) _DECL_BASE.metadata.create_all(engine)
# Clean dry_run DB
if _CONF.get('dry_run', False) and _CONF.get('dry_run_db', False):
clean_dry_run_db()
def cleanup() -> None: def cleanup() -> None:
""" """
@ -56,6 +60,17 @@ def cleanup() -> None:
Trade.session.flush() Trade.session.flush()
def clean_dry_run_db() -> None:
"""
Remove open_order_id from a Dry_run DB
:return: None
"""
for trade in Trade.query.filter(Trade.open_order_id.isnot(None)).all():
# Check we are updating only a dry_run order not a prod one
if 'dry_run' in trade.open_order_id:
trade.open_order_id = None
class Trade(_DECL_BASE): class Trade(_DECL_BASE):
__tablename__ = 'trades' __tablename__ = 'trades'

View File

@ -4,7 +4,7 @@ import os
import pytest import pytest
from freqtrade.exchange import Exchanges from freqtrade.exchange import Exchanges
from freqtrade.persistence import Trade, init from freqtrade.persistence import Trade, init, clean_dry_run_db
def test_init_create_session(default_conf, mocker): def test_init_create_session(default_conf, mocker):
@ -310,3 +310,50 @@ def test_calc_profit_percent(limit_buy_order, limit_sell_order):
# Test with a custom fee rate on the close trade # Test with a custom fee rate on the close trade
assert trade.calc_profit_percent(fee=0.003) == 0.0614782 assert trade.calc_profit_percent(fee=0.003) == 0.0614782
def test_clean_dry_run_db(default_conf, mocker):
init(default_conf)
# Simulate dry_run entries
trade = Trade(
pair='BTC_ETH',
stake_amount=0.001,
amount=123.0,
fee=0.0025,
open_rate=0.123,
exchange='BITTREX',
open_order_id='dry_run_buy_12345'
)
Trade.session.add(trade)
trade = Trade(
pair='BTC_ETC',
stake_amount=0.001,
amount=123.0,
fee=0.0025,
open_rate=0.123,
exchange='BITTREX',
open_order_id='dry_run_sell_12345'
)
Trade.session.add(trade)
# Simulate prod entry
trade = Trade(
pair='BTC_ETC',
stake_amount=0.001,
amount=123.0,
fee=0.0025,
open_rate=0.123,
exchange='BITTREX',
open_order_id='prod_buy_12345'
)
Trade.session.add(trade)
# We have 3 entries: 2 dry_run, 1 prod
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 3
clean_dry_run_db()
# We have now only the prod
assert len(Trade.query.filter(Trade.open_order_id.isnot(None)).all()) == 1