From aba78a607ebd6c62e0cf795c97915a05f13d1aee Mon Sep 17 00:00:00 2001 From: ozdemirozcelik Date: Fri, 11 Nov 2022 16:37:11 -0800 Subject: [PATCH] added: avg slippage --- app.py | 20 +++++++++++++-- models/signals.py | 62 ++++++++++++++++++++++++++++++++++++++++++--- templates/list.html | 51 +++++++++++++++++++++---------------- 3 files changed, 106 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index 7690a48..7292aae 100644 --- a/app.py +++ b/app.py @@ -315,7 +315,7 @@ def dashboard_list(): end_date = end_date + timedelta( days=1 ) # Add 1 day to "%Y-%m-%d" 00:00:00 to reach end of day - + except: start_date = datetime.now(tz=pytz.utc) - timedelta(days=1) date_now = datetime.now(tz=pytz.utc) @@ -335,6 +335,9 @@ def dashboard_list(): simplesession = None if selected_ticker: + + slip_dic = SignalModel.get_avg_slip(selected_ticker, start_date, end_date) + if simplesession: items = SignalModel.get_list_ticker_dates( selected_ticker, "0", start_date, end_date @@ -351,6 +354,17 @@ def dashboard_list(): signals = [item.json() for item in items] + slip_buy ="?" + slip_sell ="?" + slip_avg ="?" + + if slip_dic['buy']: + slip_buy = str(round(slip_dic['buy'], 5)) + if slip_dic['sell']: + slip_sell = str(round(slip_dic['sell'], 5)) + if slip_dic['avg']: + slip_avg = str(round(slip_dic['avg'], 5)) + return render_template( "list.html", signals=signals, @@ -362,9 +376,11 @@ def dashboard_list(): end_date=end_date - timedelta(days=1), selected_ticker=selected_ticker, selected_trade_type=selected_trade_type, + slip_buy=slip_buy, + slip_sell=slip_sell, + slip_avg=slip_avg ) - @app.get("/positions") def positions(): # (flask-session-change) Flask sessions may not be persistent in Heroku, works fine in local diff --git a/models/signals.py b/models/signals.py index c355f0d..6ba3de9 100644 --- a/models/signals.py +++ b/models/signals.py @@ -815,10 +815,66 @@ def splitticker_stocks(self,) -> bool: return success_flag - # TODO: COMPLETE @classmethod - def get_avg_slip(cls, ticker_name) -> List: - pass + def get_avg_slip(cls, ticker_name, start_date, end_date) -> dict: + + slip_dic = {} + + pair = False + + tickers = ticker_name.split("-") # check if pair or single + ticker1 = tickers[0] + ticker2 = "" + if len(tickers) == 2: + ticker2 = tickers[1] + pair = True + + if pair: + slip_dic['buy'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.ticker2 == ticker2) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + & (cls.order_action == "buy") + ).scalar() + + slip_dic['sell'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.ticker2 == ticker2) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + & (cls.order_action == "sell") + ).scalar() + + slip_dic['avg'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.ticker2 == ticker2) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + ).scalar() + + else: + slip_dic['buy'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + & (cls.order_action == "buy") + ).filter(cls.ticker_type == "single").scalar() + + slip_dic['sell'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + & (cls.order_action == "sell") + ).filter(cls.ticker_type == "single").scalar() + + slip_dic['avg'] = db.session.query(db.func.avg(cls.slip)).filter( + (cls.ticker1 == ticker1) + & (cls.timestamp <= end_date) + & (cls.timestamp >= start_date) + ).filter(cls.ticker_type == "single").scalar() + + return slip_dic @classmethod def find_by_orderid(cls, orderid) -> "SignalModel": diff --git a/templates/list.html b/templates/list.html index 1046a6d..408bc04 100644 --- a/templates/list.html +++ b/templates/list.html @@ -1,31 +1,38 @@ {% extends 'base.html' %} {% block submitlist %} -
-
-
- pair - ticker -
-
-
+
+
+ +
+ pair + ticker +
+
+
- -   - -

+ +   + +

+ + + + +
+ +
+
+ slippage:

+ buy: {{slip_buy}}
+ sell: {{slip_sell}}
+ avg: {{slip_avg}}
+
+
- - - -
- -
-
-
{% endblock %} {% block tablelist %}