use get_trades in rpc modules

This commit is contained in:
Matthias 2019-10-29 15:09:01 +01:00
parent 26a5800a7f
commit b37c5e4878
2 changed files with 16 additions and 18 deletions

View File

@ -395,9 +395,12 @@ class Trade(_DECL_BASE):
@staticmethod @staticmethod
def get_trades(trade_filter=None) -> Query: def get_trades(trade_filter=None) -> Query:
""" """
Helper function to query Trades using filter. Helper function to query Trades using filters.
:param trade_filter: Filter to apply to trades :param trade_filter: Optional filter to apply to trades
:return: Query object Can be either a Filter object, or a List of filters
e.g. `(trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True),])`
e.g. `(trade_filter=Trade.id == trade_id)`
:return: unsorted query object
""" """
if trade_filter is not None: if trade_filter is not None:
if not isinstance(trade_filter, list): if not isinstance(trade_filter, list):

View File

@ -9,7 +9,6 @@ from enum import Enum
from typing import Dict, Any, List, Optional from typing import Dict, Any, List, Optional
import arrow import arrow
import sqlalchemy as sql
from numpy import mean, NAN from numpy import mean, NAN
from pandas import DataFrame from pandas import DataFrame
@ -154,12 +153,11 @@ class RPC:
for day in range(0, timescale): for day in range(0, timescale):
profitday = today - timedelta(days=day) profitday = today - timedelta(days=day)
trades = Trade.query \ trades = Trade.get_trades(trade_filter=[
.filter(Trade.is_open.is_(False)) \ Trade.is_open.is_(False),
.filter(Trade.close_date >= profitday)\ Trade.close_date >= profitday,
.filter(Trade.close_date < (profitday + timedelta(days=1)))\ Trade.close_date < (profitday + timedelta(days=1))
.order_by(Trade.close_date)\ ]).order_by(Trade.close_date).all()
.all()
curdayprofit = sum(trade.calc_profit() for trade in trades) curdayprofit = sum(trade.calc_profit() for trade in trades)
profit_days[profitday] = { profit_days[profitday] = {
'amount': f'{curdayprofit:.8f}', 'amount': f'{curdayprofit:.8f}',
@ -192,7 +190,7 @@ class RPC:
def _rpc_trade_statistics( def _rpc_trade_statistics(
self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]: self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]:
""" Returns cumulative profit statistics """ """ Returns cumulative profit statistics """
trades = Trade.query.order_by(Trade.id).all() trades = Trade.get_trades().order_by(Trade.id).all()
profit_all_coin = [] profit_all_coin = []
profit_all_perc = [] profit_all_perc = []
@ -385,11 +383,8 @@ class RPC:
return {'result': 'Created sell orders for all open trades.'} return {'result': 'Created sell orders for all open trades.'}
# Query for trade # Query for trade
trade = Trade.query.filter( trade = Trade.get_trades(
sql.and_( trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True), ]
Trade.id == trade_id,
Trade.is_open.is_(True)
)
).first() ).first()
if not trade: if not trade:
logger.warning('forcesell: Invalid argument received') logger.warning('forcesell: Invalid argument received')
@ -419,7 +414,7 @@ class RPC:
# check if valid pair # check if valid pair
# check if pair already has an open pair # check if pair already has an open pair
trade = Trade.query.filter(Trade.is_open.is_(True)).filter(Trade.pair.is_(pair)).first() trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair.is_(pair)]).first()
if trade: if trade:
raise RPCException(f'position for {pair} already open - id: {trade.id}') raise RPCException(f'position for {pair} already open - id: {trade.id}')
@ -428,7 +423,7 @@ class RPC:
# execute buy # execute buy
if self._freqtrade.execute_buy(pair, stakeamount, price): if self._freqtrade.execute_buy(pair, stakeamount, price):
trade = Trade.query.filter(Trade.is_open.is_(True)).filter(Trade.pair.is_(pair)).first() trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair.is_(pair)]).first()
return trade return trade
else: else:
return None return None