{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from v2realbot.tools.loadbatch import load_batch\n",
    "from v2realbot.utils.utils import zoneNY, DATA_DIR\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import vectorbtpro as vbt\n",
    "\n",
    "vbt.settings.set_theme(\"dark\")\n",
    "vbt.settings['plotting']['layout']['width'] = 1280\n",
    "vbt.settings.plotting.auto_rangebreaks = True\n",
    "# Set the option to display with pagination\n",
    "pd.set_option('display.notebook_repr_html', True)\n",
    "pd.set_option('display.max_rows', 10)  # Number of rows per page"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_id = \"e44a5075\"\n",
    "# res, df = load_batch(batch_id=batch_id,\n",
    "#                      space_resolution_evenly=False,\n",
    "#                      indicators_columns=[\"Rsi14\"],\n",
    "#                      main_session_only=True)\n",
    "# if res < 0:\n",
    "#     print(\"Error\" + str(res) + str(df))\n",
    "# df = df[\"bars\"]\n",
    "# df.info()\n",
    "# df.head()\n",
    "# #df.ptable()\n",
    "# df.to_pickle(DATA_DIR+\"/\"+f'{batch_id}.pickle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FILTERING"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle(DATA_DIR+\"/\"+f'{batch_id}.pickle')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#naloadujeme do vbt symbol as column\n",
    "basic_data = vbt.Data.from_data({\"BAC\": df}, tz_convert=zoneNY)\n",
    "start_date = pd.Timestamp('2024-03-12 09:30', tz=zoneNY)\n",
    "end_date = pd.Timestamp('2024-03-13 16:00', tz=zoneNY)\n",
    "\n",
    "#filter date\n",
    "#basic_data = basic_data.transform(lambda df: df[df.index.date == start_date.date()])\n",
    "#filter range\n",
    "basic_data = basic_data.transform(lambda df: df[(df.index >= start_date) & (df.index <= end_date)])\n",
    "#filtered_data = basic_data.transform(lambda df: df[(df.index >= start_date) & (df.index <= end_date)])\n",
    "# #range filtered_data = data[(data.index >= start_date) & (data.index <= end_date)\n",
    "#df.between_time('09:30', '16:00')\n",
    "#(df.index.time >= pd.Timestamp('09:30').time()) & (df.index.time <= pd.Timestamp('16:00').time())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# basic_data.data[\"BAC\"]\n",
    "rsi14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#b = filtered_data.get().iloc[100:200] #b[[\"Open\",\"High\"]]\n",
    "rsi14 = basic_data.data[\"BAC\"][\"Rsi14\"].rename(\"Rsi14\")\n",
    "#create subploit\n",
    "fig = vbt.make_subplots(rows=2, cols=1, shared_xaxes=True, specs=[[{\"secondary_y\": True}], [{\"secondary_y\": False}]])\n",
    "rsi14.vbt.plot(add_trace_kwargs=dict(row=1, col=1, secondary_y=True),fig=fig)\n",
    "basic_data.data[\"BAC\"].vbt.ohlcv.plot(add_trace_kwargs=dict(row=1, col=1, secondary_y=False), fig=fig)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run_rsi = vbt.talib_func(\"rsi\")\n",
    "rsi_new = run_rsi(basic_data.vwap, timeperiod=15)\n",
    "rsi_new = rsi_new.fillna(0)\n",
    "# print(rsi_new)\n",
    "# print(dir(rsi_new))\n",
    "rsi14 = basic_data.data[\"BAC\"][\"Rsi14\"]\n",
    "# print(rsi14)\n",
    "\n",
    "#zkombinujeme do stejneho dataframe skrz sloupce (axis1)\n",
    "# combined_df = pd.concat([rsi_new, rsi14], axis=1)\n",
    "# combined_df\n",
    "\n",
    "#create subplot\n",
    "fig = vbt.make_subplots(rows=1, cols=1, shared_xaxes=True)\n",
    "\n",
    "plot_rsi = vbt.talib_plot_func(\"rsi\")\n",
    "plot_rsi(rsi_new, fig=fig)\n",
    "plot_rsi(rsi14, fig=fig)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "entries = rsi_new.vbt.crossed_below(30)\n",
    "entries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exits = rsi_new.vbt.crossed_above(70)\n",
    "exits  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_rsi(close, rsi, entries, exits):\n",
    "    fig = vbt.make_subplots(rows=2, cols=1, shared_xaxes=True, specs=[[{\"secondary_y\": True}], [{\"secondary_y\": False}]])\n",
    "    basic_data.data[\"BAC\"].vbt.ohlcv.plot(add_trace_kwargs=dict(row=1, col=1, secondary_y=True),fig=fig)\n",
    "    rsi.vbt.plot(fig=fig, add_trace_kwargs=dict(row=1, col=1, secondary_y=False), trace_kwargs=dict(line=dict(color='grey', width=1.5)))\n",
    "    #close.vbt.plot(fig=fig, add_trace_kwargs=dict(row=1, col=1, secondary_y=True))\n",
    "    entries.vbt.signals.plot_as_entries(y=close, fig=fig, add_trace_kwargs=dict(row=1, col=1, secondary_y=True))\n",
    "    exits.vbt.signals.plot_as_exits(y=close, fig=fig, add_trace_kwargs=dict(row=1, col=1, secondary_y=True))\n",
    "    return fig\n",
    "\n",
    "close = basic_data.get(\"Close\")\n",
    "\n",
    "print(entries)\n",
    "\n",
    "plot_rsi(close, rsi_new, entries, exits).show()\n",
    "\n",
    "clean_entries, clean_exits = entries.vbt.signals.clean(exits) \n",
    "\n",
    "plot_rsi(close, rsi_new, clean_entries, clean_exits).show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_entries.vbt.signals.total() \n",
    "clean_exits.vbt.signals.total() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = basic_data.wrapper.index\n",
    "\n",
    "#minutes from market open\n",
    "market_open_time = pd.to_timedelta('09:30:00')\n",
    "\n",
    "# Calculate the market open datetime for each day\n",
    "market_opens = index.normalize() + market_open_time\n",
    "\n",
    "minutes_from_open = (index - market_opens).total_seconds() / 60\n",
    "# Ensuring the result is a Series\n",
    "minutes_from_open = pd.Series(minutes_from_open, index=index)\n",
    "\n",
    "#minutes_from_open.values\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "symbol_wrapper = basic_data.get_symbol_wrapper()\n",
    "\n",
    "@vbt.njit\n",
    "def elapsed_minutes_from_open_nb(time_in_ns):\n",
    "    market_opens_in_minute = 570  # 9 hours * 60 minutes + 30 minutes\n",
    "    current_minute = vbt.dt_nb.hour_nb(time_in_ns) * 60 + vbt.dt_nb.minute_nb(time_in_ns)\n",
    "    #print(\"current_minutes\", current_minutes)\n",
    "    # Calculate elapsed minutes since market open at 9:30 AM\n",
    "    minutes_from_open = current_minute - market_opens_in_minute\n",
    "    print( \"elapsed_from_open\", minutes_from_open)\n",
    "    return minutes_from_open if minutes_from_open >= 0 else 0\n",
    "\n",
    "@vbt.njit\n",
    "def entry_place_func_nb(c, low, close, time_in_ns, rsi14, window_open, window_close):\n",
    "    # if c.from_i == 0:  # (1)!\n",
    "    #     c.out[0] = True\n",
    "    #     return -1\n",
    "    # print(\"ted\")\n",
    "    # print(c.from_i)\n",
    "    #exit_i = c.from_i - c.wait  # (2)!\n",
    "    #exit_price = close[exit_i, c.col]  # (3)!\n",
    "    #hit_price = exit_price * (1 - th)\n",
    "    market_open_minutes = 570  # 9 hours * 60 minutes + 30 minutes\n",
    "\n",
    "    for out_i in range(len(c.out)):\n",
    "        i = c.from_i + out_i\n",
    "\n",
    "        current_minutes = vbt.dt_nb.hour_nb(time_in_ns[i]) * 60 + vbt.dt_nb.minute_nb(time_in_ns[i])\n",
    "        #print(\"current_minutes\", current_minutes)\n",
    "        # Calculate elapsed minutes since market open at 9:30 AM\n",
    "        elapsed_from_open = current_minutes - market_open_minutes\n",
    "        elapsed_from_open = elapsed_from_open if elapsed_from_open >= 0 else 0\n",
    "        #print( \"elapsed_from_open\", elapsed_from_open)\n",
    "\n",
    "        #elapsed_from_open = elapsed_minutes_from_open_nb(time_in_ns) \n",
    "        in_window = elapsed_from_open > window_open and elapsed_from_open < window_close\n",
    "        #print(\"in_window\", in_window)\n",
    "        # if in_window:\n",
    "        #     print(\"in window\")\n",
    "\n",
    "        if in_window and rsi14[i] > 60: # and low[i, c.col] <= hit_price: # and hour == 9:  # (4)!\n",
    "            return out_i\n",
    "    return -1\n",
    "\n",
    "#whether the date changed\n",
    "# day_changed_nb(\n",
    "#     ts1,\n",
    "#     ts2\n",
    "# )\n",
    "\n",
    "# h_ns(ts1) int\n",
    "\n",
    "\n",
    "@vbt.njit\n",
    "def exit_place_func_nb(c, high, close, time_index, tp, sl):  # (5)!\n",
    "    entry_i = c.from_i - c.wait\n",
    "    entry_price = close[entry_i, c.col]\n",
    "    hit_price = entry_price * (1 + tp)\n",
    "    stop_price = entry_price * (1 - sl)\n",
    "    for out_i in range(len(c.out)):\n",
    "        i = c.from_i + out_i\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "        last_bar_of_day = vbt.dt_nb.day_changed_nb(time_index[i], time_index[i + 1])\n",
    "\n",
    "        #print(next_day)\n",
    "        if last_bar_of_day: #pokud je dalsi next day, tak zavirame posledni\n",
    "            print(\"ted\",out_i)\n",
    "            return out_i\n",
    "        if high[i, c.col] >= hit_price or close[i, c.col] <= stop_price :\n",
    "            return out_i\n",
    "    return -1\n",
    "\n",
    "index = basic_data.wrapper.index\n",
    "\n",
    "#minutes from market open\n",
    "market_open_time = pd.to_timedelta('09:30:00')\n",
    "\n",
    "# Calculate the market open datetime for each day\n",
    "market_opens = index.normalize() + market_open_time\n",
    "\n",
    "minutes_from_open = (index - market_opens).total_seconds() / 60\n",
    "\n",
    "print(minutes_from_open)\n",
    "\n",
    "\n",
    "#index 9:30 az 10:00\n",
    "time_entry_window = ((index.time >= pd.Timestamp(\"09:30:00\").time())&\n",
    "                        (index.time <= pd.Timestamp(\"14:00:00\").time()))\n",
    "\n",
    "\n",
    "print(time_entry_window)\n",
    "print(rsi_new)\n",
    "rsi_entries = rsi_new.vbt.crossed_below(64)\n",
    "rsi_entries = rsi_entries < 40\n",
    "rsi_entries_array = rsi_entries.vbt.to_1d_array()\n",
    "print(rsi_entries_array)\n",
    "\n",
    "\n",
    "entries, exits = vbt.pd_acc.signals.generate_both(  # (6)!\n",
    "    symbol_wrapper.shape,\n",
    "    entry_place_func_nb=entry_place_func_nb,\n",
    "    #timeindex to ns to numba\n",
    "    entry_place_args=(vbt.Rep(\"low\"), vbt.Rep(\"close\"), vbt.dt.to_ns(basic_data.wrapper.index), vbt.Rep(\"rsi14\"), 0, 380),  # (7)!\n",
    "    exit_place_func_nb=exit_place_func_nb,\n",
    "    exit_place_args=(vbt.Rep(\"high\"), vbt.Rep(\"close\"), vbt.dt.to_ns(basic_data.wrapper.index), 0.001, 0.001),\n",
    "    wrapper=symbol_wrapper,\n",
    "    broadcast_named_args=dict(  # (8)!\n",
    "        high=basic_data.get(\"High\"),\n",
    "        low=basic_data.get(\"Low\"),\n",
    "        close=basic_data.get(\"Close\"),\n",
    "        rsi14=basic_data.get(\"Rsi14\"),\n",
    "        window_open=10,\n",
    "        window_close=60\n",
    "    ),\n",
    "    broadcast_kwargs=dict(post_func=np.asarray)  # (9)!\n",
    ")\n",
    "\n",
    "\n",
    "plot_rsi(close, rsi_new, entries, exits).show()\n",
    "\n",
    "# fig = basic_data.plot(\n",
    "#     symbol=\"BAC\", \n",
    "#     ohlc_trace_kwargs=dict(opacity=0.5), \n",
    "#     plot_volume=False\n",
    "# )\n",
    "\n",
    "# #rsi_entries.vbt.plot(fig=fig)\n",
    "# entries.vbt.signals.plot_as_entries(\n",
    "#     y=close, fig=fig)\n",
    "# exits.vbt.signals.plot_as_exits(\n",
    "#     y=close, fig=fig)\n",
    "# fig.show()  # (10)!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
