From f4fbda7daf2e4844955c0dae15b2ba4fe5e9dbd8 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Mon, 17 Apr 2023 15:26:30 +0200 Subject: [PATCH] backend: slope support, gui:add json --- testy/testSlope.py | 15 ++++ testy/testTelegram.py | 3 + v2realbot/ENTRY_backtest_strategyVykladaci.py | 50 +++++++++++-- .../__pycache__/backtester.cpython-310.pyc | Bin 18058 -> 18073 bytes v2realbot/backtesting/backtester.py | 2 +- v2realbot/controller/services.py | 1 + .../live_interface.cpython-310.pyc | Bin 5289 -> 5289 bytes v2realbot/interfaces/live_interface.py | 2 +- v2realbot/main.py | 2 +- v2realbot/static/index.html | 30 +++++++- v2realbot/static/js/mychart.js | 2 +- v2realbot/static/js/mytables.js | 67 +++++++++++++++++- v2realbot/static/js/mywebsocket.js | 20 ++++-- v2realbot/static/main.css | 2 +- .../strategy/StrategyOrderLimitVykladaci.py | 4 +- ...trategyOrderLimitVykladaci.cpython-310.pyc | Bin 5054 -> 5108 bytes .../strategy/__pycache__/base.cpython-310.pyc | Bin 11726 -> 11768 bytes v2realbot/strategy/base.py | 10 +-- .../utils/__pycache__/utils.cpython-310.pyc | Bin 6933 -> 7387 bytes v2realbot/utils/utils.py | 12 ++++ 20 files changed, 196 insertions(+), 26 deletions(-) create mode 100644 testy/testSlope.py create mode 100644 testy/testTelegram.py diff --git a/testy/testSlope.py b/testy/testSlope.py new file mode 100644 index 0000000..423a9ef --- /dev/null +++ b/testy/testSlope.py @@ -0,0 +1,15 @@ + +sma = list + +sma = [28.90, 28.91, 28.91, 28.92, 28.97, 28.99] +slope_lookback = 4 +roc_lookback = 4 + +slope = (sma[-1] - sma[-slope_lookback])/slope_lookback +roc = ((sma[-1] - sma[-roc_lookback])/sma[-roc_lookback])*100 + +print(slope) + + +# -1 až 0 klesání +# 0 až 1 stoupání \ No newline at end of file diff --git a/testy/testTelegram.py b/testy/testTelegram.py new file mode 100644 index 0000000..b46e6f6 --- /dev/null +++ b/testy/testTelegram.py @@ -0,0 +1,3 @@ +from v2realbot.utils.utils import send_to_telegram + +send_to_telegram("nazdarek") \ No newline at end of file diff --git a/v2realbot/ENTRY_backtest_strategyVykladaci.py b/v2realbot/ENTRY_backtest_strategyVykladaci.py index 65cfcf5..6444c01 100644 --- a/v2realbot/ENTRY_backtest_strategyVykladaci.py +++ b/v2realbot/ENTRY_backtest_strategyVykladaci.py @@ -45,7 +45,10 @@ stratvars = AttributeDict(maxpozic = 250, curve = [0.01, 0.01, 0.01, 0, 0.02, 0.02, 0.01,0.01, 0.01,0.03, 0.01, 0.01, 0.01,0.04, 0.01,0.01, 0.01,0.05, 0.01,0.01, 0.01,0.01, 0.06,0.01, 0.01,0.01, 0.01], blockbuy = 0, ticks2reset = 0.04, - consolidation_bar_count = 10) + consolidation_bar_count = 10, + slope_lookback = 20, + minimum_slope = -0.23 + ) ##toto rozparsovat a strategii spustit stejne jako v main toml_string = """ [[strategies]] @@ -106,7 +109,7 @@ def next(data, state: StrategyState): price = last_price ##prvni se vyklada na aktualni cenu, další jdou podle krivky, nula v krivce zvyšuje množství pro následující iteraci state.buy_l(price=price, size=qty) - print("prvni limitka na aktuální cenu. Další podle křicvky", price, qty) + print("prvni limitka na aktuální cenu. Další podle křivky", price, qty) for i in range(0,vykladka-1): price = price2dec(float(price - curve[i])) if price == last_price: @@ -133,24 +136,55 @@ def next(data, state: StrategyState): return 0 try: + + ## slope vyresi rychlé sesupy - jeste je treba podchytit pomalejsi sesupy + + + slope = 99 + #minimum slope disabled if -1 + + + #roc_lookback = 20 #print(state.vars.MA, "MACKO") #print(state.bars.hlcc4) state.indicators.ema = ema(state.bars.close, state.vars.MA) #state.bars.vwap #trochu prasarna, EMAcko trunc na 3 mista - kdyz se osvedci, tak udelat efektivne state.indicators.ema = [trunc(i,3) for i in state.indicators.ema] ic(state.vars.MA, state.vars.Trend, state.indicators.ema[-5:]) + + slope_lookback = int(state.vars.slope_lookback) + minimum_slope = float(state.vars.minimum_slope) + + if len(state.bars.close) > slope_lookback: + #slope = ((state.indicators.ema[-1] - state.indicators.ema[-slope_lookback])/slope_lookback)*100 + #PUVODNI slope = ((state.bars.close[-1] - state.bars.close[-slope_lookback])/slope_lookback)*100 + slope = ((state.bars.close[-1] - state.bars.close[-slope_lookback])/state.bars.close[-slope_lookback])*100 + #roc = ((state.indicators.ema[-1] - state.indicators.ema[-roc_lookback])/state.indicators.ema[-roc_lookback])*100 + state.indicators.slope.append(slope) + #state.indicators.roc.append(roc) + ic(state.indicators.slope[-5:]) + #ic(state.indicators.roc[-5:]) except Exception as e: - print("No data for MA yet", str(e)) + print("Exception in NEXT Indicator section", str(e)) print("is falling",isfalling(state.indicators.ema,state.vars.Trend)) print("is rising",isrising(state.indicators.ema,state.vars.Trend)) - #and data['index'] > state.vars.lastbuyindex+state.vars.Trend: - #neni vylozeno muzeme nakupovat + #SLOPE ANGLE PROTECTION + if slope < minimum_slope: + print("OCHRANA SLOPE TOO HIGH") + if len(state.vars.pendingbuys)>0: + print("CANCEL PENDINGBUYS") + ic(state.vars.pendingbuys) + res = asyncio.run(state.cancel_pending_buys()) + ic(state.vars.pendingbuys) + print("slope", slope) + print("min slope", minimum_slope) + if ic(state.vars.jevylozeno) == 0: print("Neni vylozeno, muzeme testovat nakup") - if isfalling(state.indicators.ema,state.vars.Trend): + if isfalling(state.indicators.ema,state.vars.Trend) and slope > minimum_slope: print("BUY MARKET") ic(data['updated']) ic(state.time) @@ -167,7 +201,7 @@ def next(data, state: StrategyState): #TODO predelat mechanismus ticků (zrelativizovat), aby byl pouzitelny na tituly s ruznou cenou #TODO spoustet 1x za X iteraci nebo cas if state.vars.jevylozeno == 1: - #pokud mame vylozeno a cena je vetsi nez 0.04 + #pokud mame vylozeno a cena je vetsi nez tick2reset if len(state.vars.pendingbuys)>0: maxprice = max(state.vars.pendingbuys.values()) print("max cena v orderbuys", maxprice) @@ -264,6 +298,8 @@ def init(state: StrategyState): #place to declare new vars print("INIT v main",state.name) state.indicators['ema'] = [] + state.indicators['slope'] = [] + #state.indicators['roc'] = [] def main(): diff --git a/v2realbot/backtesting/__pycache__/backtester.cpython-310.pyc b/v2realbot/backtesting/__pycache__/backtester.cpython-310.pyc index 3a4718716941afb8eedb0bb1e1d229444260582b..01a0da45627b6d233ce175bce2036eba5f2128b1 100644 GIT binary patch delta 168 zcmeC`Wt`c|$d}K{%f$c$|5}F6Umhw;*Z#$qOxoCmXm-V|+IGxJxACt;ve6;fz-%SGpE4KA!x>bshk) C$})cd delta 144 zcmbQ)%h=V+$d}K{%f$c$Q-4^cylLIYSFOPqGr3E{Kv}ewrG_PoxrQm5sYttqF@>d+ zp;!$l!cxNs7GYk%I{CRqybxQFAW(ae1&GiE5ek!IHDejMHt*JaFTxnLd9~eJ7RFnX nXFB&XzL~7Yf9IdcBp(9ehk*z;5a9tLJVAsXhzOdzUT7xct;wFk K?}4PHNC5zwz#pjq delta 130 zcmZ3fxl)ripO=@50SJzLv`XpN$ji^izLKek5h$=(hA)_rsVHD_1;3|U6iZ@RdO;L( zL4H+{2vD-f7f5I_7b$_*L6aYFNo@YkKa)v56vPh)5pE#D14MX&2tN=JJbAs)Ovb3m Mp2F{eq@_p!02(qM%m4rY diff --git a/v2realbot/interfaces/live_interface.py b/v2realbot/interfaces/live_interface.py index b877635..62f809b 100644 --- a/v2realbot/interfaces/live_interface.py +++ b/v2realbot/interfaces/live_interface.py @@ -147,7 +147,7 @@ class LiveInterface(GeneralInterface): return a except APIError as e: #order doesnt exist - if e.code == 40410000: return 0,0 + if e.code == 40410000: return 0 else: print("nepovedlo se zrusit objednavku", str(e)) #raise Exception(e) diff --git a/v2realbot/main.py b/v2realbot/main.py index 5ecc708..a22ffa3 100644 --- a/v2realbot/main.py +++ b/v2realbot/main.py @@ -11,7 +11,7 @@ from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import APIKeyHeader import uvicorn from uuid import UUID -import controller.services as cs +import v2realbot.controller.services as cs from v2realbot.common.model import StrategyInstance, RunnerView, RunRequest from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends, HTTPException, status, WebSocketException, Cookie, Query from fastapi.responses import HTMLResponse, FileResponse diff --git a/v2realbot/static/index.html b/v2realbot/static/index.html index 5f13f39..f16ddc8 100644 --- a/v2realbot/static/index.html +++ b/v2realbot/static/index.html @@ -79,7 +79,13 @@
- + + + + + + + @@ -192,6 +198,28 @@ +