{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from v2realbot.tools.loadbatch import load_batch\n",
    "from v2realbot.utils.utils import zoneNY\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\n",
    "\n",
    "res, df = load_batch(batch_id=\"e44a5075\",\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",
    "\n",
    "#df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# filter dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#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",
    "#basic_data = basic_data.transform(lambda df: df[df.index.date == start_date.date()])\n",
    "basic_data = basic_data.transform(lambda df: df[(df.index >= start_date) & (df.index <= end_date)])\n",
    "#basic_data.data[\"BAC\"].info()\n",
    "\n",
    "# fig = basic_data.plot(plot_volume=False)\n",
    "# pivot_info = basic_data.run(\"pivotinfo\", up_th=0.003, down_th=0.002)\n",
    "# #pivot_info.plot()\n",
    "# pivot_info.plot(fig=fig, conf_value_trace_kwargs=dict(visible=True))\n",
    "# fig.show()\n",
    "\n",
    "\n",
    "# rsi14 = basic_data.data[\"BAC\"][\"Rsi14\"].rename(\"Rsi14\")\n",
    "\n",
    "# rsi14.vbt.plot().show()\n",
    "# basic_data.data[\"BAC\"].vbt.ohlcv.plot().show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@vbt.njit\n",
    "def long_entry_place_func_nb(c, low, close, time_in_ns, rsi14, window_open, window_close):\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",
    "@vbt.njit\n",
    "def long_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",
    "        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 close[i, c.col] >= hit_price or close[i, c.col] <= stop_price :\n",
    "            return out_i\n",
    "    return -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('abcdefghij'))\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.sum()"
   ]
  }
 ],
 "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
}
