Merge pull request #244 from jblestang/fix_daily_profit

Fixing daily profit,
This commit is contained in:
Janne Sinivirta 2017-12-29 06:05:25 +02:00 committed by GitHub
commit 145583f0b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,12 @@
import logging import logging
import re import re
from decimal import Decimal from decimal import Decimal
from datetime import timedelta, date, datetime from datetime import timedelta, datetime
from typing import Callable, Any from typing import Callable, Any
import arrow import arrow
from pandas import DataFrame from pandas import DataFrame
from sqlalchemy import and_, func, text, between from sqlalchemy import and_, func, text
from tabulate import tabulate from tabulate import tabulate
from telegram import ParseMode, Bot, Update, ReplyKeyboardMarkup from telegram import ParseMode, Bot, Update, ReplyKeyboardMarkup
from telegram.error import NetworkError, TelegramError from telegram.error import NetworkError, TelegramError
@ -220,29 +220,28 @@ def _daily(bot: Bot, update: Update) -> None:
:param update: message update :param update: message update
:return: None :return: None
""" """
today = datetime.utcnow().toordinal() today = datetime.utcnow().date()
profit_days = {} profit_days = {}
try: try:
timescale = int(update.message.text.replace('/daily', '').strip()) timescale = int(update.message.text.replace('/daily', '').strip())
except (TypeError, ValueError): except (TypeError, ValueError):
timescale = 5 timescale = 7
if not (isinstance(timescale, int) and timescale > 0): if not (isinstance(timescale, int) and timescale > 0):
send_msg('*Daily [n]:* `must be an integer greater than 0`', bot=bot) send_msg('*Daily [n]:* `must be an integer greater than 0`', bot=bot)
return return
for day in range(0, timescale): for day in range(0, timescale):
# need to query between day+1 and day-1 profitday = today - timedelta(days=day)
nextdate = date.fromordinal(today - day + 1)
prevdate = date.fromordinal(today - day - 1)
trades = Trade.query \ trades = Trade.query \
.filter(Trade.is_open.is_(False)) \ .filter(Trade.is_open.is_(False)) \
.filter(between(Trade.close_date, prevdate, nextdate)) \ .filter(Trade.close_date >= profitday)\
.filter(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[date.fromordinal(today - day)] = format(curdayprofit, '.8f') profit_days[profitday] = format(curdayprofit, '.8f')
stats = [ stats = [
[ [