Skip to content

Commit

Permalink
added: avg slippage
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirozcelik committed Nov 12, 2022
1 parent ab0300d commit aba78a6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 27 deletions.
20 changes: 18 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
62 changes: 59 additions & 3 deletions models/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
51 changes: 29 additions & 22 deletions templates/list.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
{% extends 'base.html' %}
{% block submitlist %}

<div class="left-element">
<form action="list" method="get">
<div id="tradetype">
<input type="radio" id="checkpair" name="tradetype" value="pair" {% if selected_trade_type == "pair" %} checked {% endif %}><span>pair</span>
<input type="radio" id="checkticker" name="tradetype" value="ticker" {% if selected_trade_type == "ticker" %} checked {% endif %}><span>ticker</span>
</div>
<br>
<div>
<div style="width: 100%;font-size: medium;">
<div style="width: 50%; height: 100px; float: left;">
<form action="list" method="get">
<div id="tradetype">
<input type="radio" id="checkpair" name="tradetype" value="pair" {% if selected_trade_type == "pair" %} checked {% endif %}><span>pair</span>
<input type="radio" id="checkticker" name="tradetype" value="ticker" {% if selected_trade_type == "ticker" %} checked {% endif %}><span>ticker</span>
</div>
<br>
<div>

<label for="ticker_webhook" id="ticker_webhook_label"></label>
<select onchange="changeStatusColor(this); "name="ticker_webhook" id="ticker_webhook">
<option disabled selected value></option>
</select>&nbsp
<span title= "ticker status" id="show_status" class="round" style="vertical-align:middle";></span>
<br><br>
<label for="ticker_webhook" id="ticker_webhook_label"></label>
<select onchange="changeStatusColor(this); "name="ticker_webhook" id="ticker_webhook">
<option disabled selected value></option>
</select>&nbsp
<span title= "ticker status" id="show_status" class="round" style="vertical-align:middle";></span>
<br><br>

<input type="date" id="start_date" name="start_date" value={{start_date}}>
<input type="date" id="end_date" name="end_date" value={{end_date}}>
<button type="submit">List Signals</button>
</div>
</form>
</div>
<div style="margin-left: 90%; height: 100px; border-style: groove; text-align: left; font-size: medium;">
<b>slippage:</b><br><br>
buy: {{slip_buy}} <br>
sell: {{slip_sell}}<br>
avg: {{slip_avg}}<br>
</div>
</div>

<input type="date" id="start_date" name="start_date" value={{start_date}}>
<input type="date" id="end_date" name="end_date" value={{end_date}}>
<button type="submit">List Signals</button>
</div>
</form>
</div>
<div class="right-element">

</div>
{% endblock %}

{% block tablelist %}
Expand Down

0 comments on commit aba78a6

Please sign in to comment.