Skip to content

Commit 97a0a83

Browse files
committed
logsvc added
1 parent ef92403 commit 97a0a83

File tree

9 files changed

+179
-19
lines changed

9 files changed

+179
-19
lines changed

.github/workflows/container-websvc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: WebSvc Container
33
on:
44
push:
55
branches:
6-
- beta # update to main later
6+
- main
77
tags:
88
- '**'
99
pull_request:

.github/workflows/container.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@ name: Container
22

33
on:
44
push:
5-
# Publish `main` as Docker `latest` image.
65
branches:
76
- main
8-
# - beta
9-
# - '*docker*'
10-
11-
# Publish `v1.2.3` tags as releases.
127
tags:
13-
- '**' # All tags kick off a new container build Save history ad 5.0.x etc
14-
15-
# Run tests for any PRs.
8+
- '**'
169
pull_request:
17-
18-
# Manual runs
1910
workflow_dispatch:
2011

2112
env:

controllers/PyCryptoBot.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -1061,13 +1061,17 @@ def _notify(notification: str = "", level: str = "normal") -> None:
10611061
# if not live
10621062
else:
10631063
if self.state.last_buy_size == 0 and self.state.last_buy_filled == 0:
1064-
# Sim mode can now use buymaxsize as the amount used for a buy
1065-
if self.buymaxsize is not None:
1064+
# sim mode can now use buymaxsize as the amount used for a buy
1065+
if self.buymaxsize > 0:
10661066
self.state.last_buy_size = self.buymaxsize
10671067
self.state.first_buy_size = self.buymaxsize
1068+
else:
1069+
# TODO: calculate correct buy amount based on quote currency balance
1070+
self.state.last_buy_size = 1
1071+
self.state.first_buy_size = 1
10681072
# add option for buy last sell size
10691073
elif (
1070-
self.buymaxsize is not None
1074+
self.buymaxsize > 0
10711075
and self.buylastsellsize
10721076
and self.state.last_sell_size > self.state.minimum_order_quote(quote=self.state.last_sell_size, balancechk=True)
10731077
):
@@ -1089,7 +1093,7 @@ def _notify(notification: str = "", level: str = "normal") -> None:
10891093
+ "\n - TEST BUY at "
10901094
+ str(self.price)
10911095
+ "\n - Buy Size: "
1092-
+ str(_truncate(self.state.last_buy_size, 4))
1096+
+ str(_truncate(self.state.last_buy_size, 4)) # TODO: why is this 0?
10931097
)
10941098

10951099
if not self.is_verbose:
@@ -1320,6 +1324,8 @@ def _notify(notification: str = "", level: str = "normal") -> None:
13201324

13211325
# if not live
13221326
else:
1327+
# TODO
1328+
13231329
margin, profit, sell_fee = calculate_margin(
13241330
buy_size=self.state.last_buy_size,
13251331
buy_filled=self.state.last_buy_filled,
@@ -2991,6 +2997,10 @@ def get_additional_df(self, short_granularity, websocket) -> pd.DataFrame:
29912997
def get_last_buy(self) -> dict:
29922998
"""Retrieves the last exchange buy order and returns a dictionary"""
29932999

3000+
if not self.is_live:
3001+
# not live, return None
3002+
return None
3003+
29943004
try:
29953005
if self.exchange == Exchange.COINBASEPRO:
29963006
api = CBAuthAPI(
@@ -3074,11 +3084,11 @@ def get_last_buy(self) -> dict:
30743084
return None
30753085

30763086
def get_taker_fee(self):
3077-
if self.is_sim and self.exchange == Exchange.COINBASEPRO:
3087+
if not self.is_live and self.exchange == Exchange.COINBASEPRO:
30783088
return 0.005 # default lowest fee tier
3079-
elif self.is_sim and self.exchange == Exchange.BINANCE:
3089+
elif not self.is_live and self.exchange == Exchange.BINANCE:
30803090
return 0.0 # default lowest fee tier
3081-
elif self.is_sim and self.exchange == Exchange.KUCOIN:
3091+
elif not self.is_live and self.exchange == Exchange.KUCOIN:
30823092
return 0.0015 # default lowest fee tier
30833093
elif self.takerfee > -1.0:
30843094
return self.takerfee
@@ -3114,7 +3124,16 @@ def get_taker_fee(self):
31143124
return 0.005
31153125

31163126
def get_maker_fee(self):
3117-
if self.exchange == Exchange.COINBASEPRO:
3127+
if not self.is_live and self.exchange == Exchange.COINBASEPRO:
3128+
print("WENT IN HERE - GOOD - REMOVE THIS")
3129+
return 0.005 # default lowest fee tier
3130+
elif not self.is_live and self.exchange == Exchange.BINANCE:
3131+
return 0.0 # default lowest fee tier
3132+
elif not self.is_live and self.exchange == Exchange.KUCOIN:
3133+
return 0.0015 # default lowest fee tier
3134+
elif self.makerfee > -1.0:
3135+
return self.makerfee
3136+
elif self.exchange == Exchange.COINBASEPRO:
31183137
api = CBAuthAPI(
31193138
self.api_key,
31203139
self.api_secret,

logsvc.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
4+
import os
5+
import re
6+
import sys
7+
import argparse
8+
import webbrowser
9+
from threading import Timer
10+
from logsvc.app import app
11+
12+
parser = argparse.ArgumentParser(description="PyCryptoBot Log Reader")
13+
parser.add_argument(
14+
"--host",
15+
type=str,
16+
help="web service ip (default: 127.0.0.1)",
17+
)
18+
parser.add_argument(
19+
"--port",
20+
type=int,
21+
help="web service port (default: 5000)",
22+
)
23+
parser.add_argument("--quiet", action="store_true", help="don't open browser")
24+
parser.add_argument("--debug", action="store_true", help="enable debugging")
25+
26+
args = parser.parse_args()
27+
28+
# listen on local host
29+
http_host = "0.0.0.0"
30+
if args.host is not None:
31+
p = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
32+
if p.match(args.host):
33+
http_host = args.host
34+
else:
35+
parser.print_help(sys.stderr)
36+
37+
# flask listening port
38+
http_port = 5000
39+
if args.port is not None:
40+
if args.port >= 0 and args.port <= 65535:
41+
http_port = args.port
42+
else:
43+
parser.print_help(sys.stderr)
44+
45+
46+
def open_browser() -> None:
47+
webbrowser.open_new(f"http://{http_host}:{http_port}/")
48+
49+
50+
if __name__ == "__main__":
51+
if args.quiet is False:
52+
Timer(1, open_browser).start()
53+
54+
port = int(os.environ.get("PORT", http_port))
55+
56+
# pyright: reportUndefinedVariable=false
57+
app.run(host=http_host, port=port, debug=args.debug) # noqa: F821

logsvc/__init__.py

Whitespace-only changes.

logsvc/app/__init__.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import io
2+
import os
3+
import sys
4+
import time
5+
import requests
6+
from threading import Lock
7+
from flask import Flask, render_template, session
8+
from flask_socketio import SocketIO, emit
9+
from pygtail import Pygtail
10+
11+
__all__ = ("follow",)
12+
13+
# threading", "eventlet", "gevent" or None for auto detection
14+
async_mode = None
15+
16+
app = Flask(__name__)
17+
socketio = SocketIO(app, async_mode=async_mode)
18+
thread = None
19+
thread_lock = Lock()
20+
21+
22+
def log_reader_thread():
23+
with io.open("pycryptobot.log") as fp:
24+
line = fp.readline().strip()
25+
while line:
26+
# don't print existing lines
27+
line = fp.readline().strip()
28+
29+
while True:
30+
line = fp.readline().strip()
31+
if len(line):
32+
socketio.emit("log_msg", {"data": line})
33+
socketio.sleep(0.1)
34+
35+
36+
@app.route("/")
37+
def index():
38+
return render_template("index.html", async_mode=socketio.async_mode)
39+
40+
41+
@socketio.event
42+
def my_event(message):
43+
session["receive_count"] = session.get("receive_count", 0) + 1
44+
emit("log_msg", {"data": message["data"]})
45+
46+
47+
@socketio.event
48+
def connect():
49+
global thread
50+
with thread_lock:
51+
if thread is None:
52+
thread = socketio.start_background_task(log_reader_thread)
53+
54+
55+
if __name__ == "__main__":
56+
socketio.run(app)

logsvc/app/templates/index.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
4+
<head>
5+
<title>PyCryptoBot Log</title>
6+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
7+
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
8+
crossorigin="anonymous"></script>
9+
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js"
10+
integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ=="
11+
crossorigin="anonymous"></script>
12+
<script type="text/javascript" charset="utf-8">
13+
$(document).ready(function () {
14+
var socket = io();
15+
16+
socket.on('log_msg', function (msg, cb) {
17+
$('#log').append('<br>' + $('<div/>').text(msg.data).html());
18+
if (cb)
19+
cb();
20+
21+
window.scrollTo(0, document.body.scrollHeight);
22+
});
23+
});
24+
</script>
25+
</head>
26+
27+
<body>
28+
<h1>PyCryptoBot Log</h1>
29+
<div id="log"></div>
30+
</body>
31+
32+
</html>

models/TradingAccount.py

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def __init__(self, app=None):
4444
self.quotebalance = self.get_balance(app.quote_currency)
4545
self.basebalance = self.get_balance(app.base_currency)
4646

47+
self.base_balance_before = 0.0
48+
self.quote_balance_before = 0.0
49+
4750
self.orders = pd.DataFrame()
4851

4952
def _convert_status(self, val):

models/exchange/binance/api.py

+2
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ def get_maker_fee(self, market: str = "") -> float:
308308
def get_taker_fee(self, market: str = "") -> float:
309309
"""Retrieves the taker fee"""
310310

311+
print("SHOULD NOT HAVE GONE IN HERE!")
312+
311313
if len(market) is not None:
312314
fees = self.get_fees(market)
313315
else:

0 commit comments

Comments
 (0)