From ac61467a6aa7dcb5e0678fac5c41b06089f114d1 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Thu, 10 Oct 2024 10:53:20 +0200 Subject: [PATCH] update --- .gitignore | 2 + .vscode/launch.json | 49 +++++++++++++++ .vscode/settings.json | 3 + requirements.txt | 61 ++++++++++++++++++ vbt-helper.ipynb | 0 vbt.py | 139 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 254 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 requirements.txt create mode 100644 vbt-helper.ipynb create mode 100644 vbt.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af74d09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.venv/ +.venv \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..98dbeae --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,49 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Attach", + "type": "python", + "request": "attach", + "port": 5678, // or the port used by your Jupyter server + "justMyCode": false + }, + { + "name": "Python: Aktuální soubor", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "env": { + "PYTHONPATH": "${workspaceFolder}:${workspaceFolder}/bld" + }, + "console": "integratedTerminal", + "justMyCode": false, + "python": "${command:python.interpreterPath}", + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": "Python: Main", + "type": "python", + "request": "launch", + "program": "v2realbot/main.py", + "justMyCode": false + }, + { + "name": "Python: File", + "type": "python", + "request": "launch", + "program": "${file}", + "justMyCode": false + }, + { + "name": "Python: Attach", + "type": "python", + "request": "attach", + "connect": { + "host": "localhost", + "port": 5678 + } + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3b66410 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "git.ignoreLimitWarning": true +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8caca92 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,61 @@ +appnope==0.1.4 +asttokens==2.4.1 +attrs==24.2.0 +certifi==2024.8.30 +charset-normalizer==3.4.0 +comm==0.2.2 +dateparser==1.2.0 +debugpy==1.8.6 +decorator==5.1.1 +exceptiongroup==1.2.2 +executing==2.1.0 +humanize==4.11.0 +idna==3.10 +imageio==2.35.1 +ipykernel==6.29.5 +ipython==8.28.0 +jedi==0.19.1 +joblib==1.4.2 +jupyter_client==8.6.3 +jupyter_core==5.7.2 +llvmlite==0.39.1 +matplotlib-inline==0.1.7 +mypy-extensions==1.0.0 +nest-asyncio==1.6.0 +numba==0.56.4 +numpy==1.23.5 +packaging==24.1 +pandas==2.2.3 +parso==0.8.4 +pexpect==4.9.0 +pillow==10.4.0 +platformdirs==4.3.6 +prompt_toolkit==3.0.48 +psutil==6.0.0 +ptyprocess==0.7.0 +pure_eval==0.2.3 +Pygments==2.18.0 +python-dateutil==2.9.0.post0 +pytz==2024.2 +pyzmq==26.2.0 +regex==2024.9.11 +requests==2.32.3 +schedule==1.2.2 +scikit-learn==1.5.2 +scipy==1.14.1 +six==1.16.0 +stack-data==0.6.3 +TA-Lib==0.4.32 +threadpoolctl==3.5.0 +tomli==2.0.2 +tornado==6.4.1 +tqdm==4.66.5 +traitlets==5.14.3 +ttools @ git+https://github.com/drew2323/ttools.git@6833710e66967948c711614108f8f5b3c7f16294 +typing_extensions==4.12.2 +tzdata==2024.2 +tzlocal==5.2 +urllib3==2.2.3 +vectorbtpro @ file:///Users/davidbrazda/Downloads/vectorbtpro-2024.6.19-py3-none-any.whl#sha256=5e202f62aeb7697bb1411302e1c062d1df4fed5de5e1c2e8867eac07263c2d4a +wcwidth==0.2.13 +websocket-client==1.8.0 diff --git a/vbt-helper.ipynb b/vbt-helper.ipynb new file mode 100644 index 0000000..e69de29 diff --git a/vbt.py b/vbt.py new file mode 100644 index 0000000..20dd4e3 --- /dev/null +++ b/vbt.py @@ -0,0 +1,139 @@ +import vectorbtpro as vbt +import lightweightcharts + +#region DATA + #fetching from remote db +from lib.db import Connection +SYMBOL = "BAC" +SCHEMA = "ohlcv_1s" #time based 1s other options ohlcv_vol_200 (volume based ohlcv with resolution of 200), ohlcv_renko_20 (renko with 20 bricks size) ... +DB = "market_data" + +con = Connection(db_name=DB, default_schema=SCHEMA, create_db=True) +basic_data = con.pull(symbols=[SYMBOL], schema=SCHEMA,start="2024-08-01", end="2024-08-08", tz_convert='America/New_York') +#endregion + +#region DISCOVERY +#get parameters of method +vbt.phelp(vbt.indicator("talib:MOM").run) + +vbt.IF.list_indicators("*sma") + +#endregion + +#region RESAMPLING +from vectorbtpro.utils.config import merge_dicts, Config, HybridConfig +from vectorbtpro import _typing as tp +from vectorbtpro.generic import nb as generic_nb + +_feature_config: tp.ClassVar[Config] = HybridConfig( + { + "buyvolume": dict( + resample_func=lambda self, obj, resampler: obj.vbt.resample_apply( + resampler, + generic_nb.sum_reduce_nb, + ) + ), + "sellvolume": dict( + resample_func=lambda self, obj, resampler: obj.vbt.resample_apply( + resampler, + generic_nb.sum_reduce_nb, + ) + ), + "trades": dict( + resample_func=lambda self, obj, resampler: obj.vbt.resample_apply( + resampler, + generic_nb.sum_reduce_nb, + ) + ) + } +) + +basic_data._feature_config = _feature_config + +t1data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']].resample("1T") +t1data = t1data.transform(lambda df: df.between_time('09:30', '16:00').dropna()) + +#realign closing +resampler_s = vbt.Resampler(t1data.index, s1data.index, source_freq="1T", target_freq="1s") +t1close_realigned = t1data.data["BAC"].close.vbt.realign_closing(resampler_s) + + + +#endregion + +#region ENTRIES/EXITS + +# window open/close + + + +#END OF DAY EXITS +# end_of_day_dates = index.to_series().resample("1d").last().values +# exit_signals.loc[end_of_day_dates] = True +end_of_day_dates = open_hours_index.to_series().resample("1d").last() +df['exit'][df['exit'].index.isin(end_of_day_dates)] = True +# This index should be probably open_hours_index +# But also check that end_of_day_dates doesn't have nans (NaT), and if it has, you need to filter them out (edited) + +#endregion + + +#region INDICATORS +#anchored VWAP +t1vwap_h = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor="H") +t1vwap_d = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor="D") +t1vwap_t = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor="T") + +t1vwap_h_real = t1vwap_h.vwap.vbt.realign_closing(resampler_s) +t1vwap_d_real = t1vwap_d.vwap.vbt.realign_closing(resampler_s) +t1vwap_t_real = t1vwap_t.vwap.vbt.realign_closing(resampler_s) + +#SMA - note for TALIB use skipna=True +mom_multi_beztf = vbt.indicator("talib:MOM").run(t1data.close, timeperiod=5, skipna=True) + +#TALIB INDICATORS can do realing closing : timeframe=["1T"] +mom_multi = vbt.indicator("talib:MOM").run(t1data.close, timeperiod=5, timeframe=["1T","5T"], skipna=True) #returned 5T can be directly compared with 1T + +#ANCHORED indciators vbt.indicator("pandas_ta:BBANDS") is called AnchoredIndicator("pandas_ta:BBANDS") +from ttools import AnchoredIndicator + +#BBANDS = vbt.indicator("pandas_ta:BBANDS") +mom_anch_d = AnchoredIndicator("talib:MOM", anchor='30min').run(t1data.data["BAC"].close, timeperiod=10) +mom = vbt.indicator("talib:MOM").run(t1data.data["BAC"].close, timeperiod=10, skipna=True) +#macd = vbt.indicator("talib:MACD").run(t1data.data["BAC"].close) #, timeframe=["1T"]) #, +t1data.ohlcv.data["BAC"].lw.plot(auto_scale=[mom_anch_d, mom]) + + + +#endregion + + +#region CHARTING + +#LW df/sr accessor +t1data.ohlcv.data["BAC"].lw.plot(left=[(mom_multi, "mom_multi")]) #OHLCV with indicators on top + + +t5data.ohlcv.data["BAC"].lw.plot( + left=[(mom_multi.real, "mom"),(mom_multi_beztf, "mom_beztf"), (mom_5t_orig, "mom_5t_orig"), (mom_5t_orig_realigned, "mom_5t_orig_realigned")], + right=[(t1data.data["BAC"].close, "t1 close"),(t5data.data["BAC"].close, "t5 close")], + size="s") #.loc[:,(20,"1T","BAC")] + +#PANEL +pane1 = Panel( + #auto_scale=[mom_multi, mom_multi_1t], + #ohlcv=(t1data.data["BAC"],), #(series, entries, exits, other_markers) + #histogram=[(order_imbalance_allvolume, "oivol")], # [(series, name, "rgba(53, 94, 59, 0.6)", opacity)] + right=[(t1data.data["BAC"].close,"close 1T"),(t5data.data["BAC"].close,"close 5T"),(mom_multi_1t.close, "mom multi close")], # [(series, name, entries, exits, other_markers)] + left=[(mom_multi, "mom_multi"), (mom_multi_1t, "mom_multi_1t")], + #middle1=[], + #middle2=[], + #xloc="2024-02-12 09:30", + precision=3 +) + +ch = chart([pane1], size="s") + + + +#endregion \ No newline at end of file