{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "a2468e6e-b872-4c22-9d7c-bbd9e63b3960",
   "metadata": {},
   "source": [
    "# Symbologies"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "63dbbd13-4431-4e9f-afaf-a5f0f491cfab",
   "metadata": {},
   "source": [
    "We can query data using different symbologies.\n",
    "\n",
    "In this case parameter `symbol_date` of {func}`otp.run <onetick.py.run>` function should be specified.\n",
    "This parameter specifies the date when queried securities had specified names\n",
    " and it is used to determine the names that the security has between the query start and end times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eed0384-49d5-42f3-b00d-defb751c051c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import onetick.py as otp\n",
    "\n",
    "s = otp.dt(2024, 2, 1, 9, 30)\n",
    "e = otp.dt(2024, 2, 1, 9, 30, 1)\n",
    "\n",
    "trd = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "trd = trd[['PRICE', 'SIZE', 'EXCHANGE', 'COND']]\n",
    "\n",
    "otp.run(trd, start=s, end=e, symbol_date=otp.dt(2024, 2, 1), symbols=['AAPL'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fabae2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "otp.run(trd, start=s, end=e,\n",
    "        symbol_date=otp.dt(2024, 2, 1), symbols=['BSYM::::AAPL US Equity'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "123044a4",
   "metadata": {},
   "source": [
    "Examples of supported symbologies include:\n",
    "\n",
    "- `BSYM::AAPL US Equity` (Bloomberg Symbol)\n",
    "- `FGC::BBG000B9XRY4`    (Composite FIGI)\n",
    "- `CUS::037833100`       (CUSIP)\n",
    "- `ISN::GB00BH4HKS39`    (ISIN)\n",
    "- `SED::BH4HKS3`         (SEDOL)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2b8306e5-bc9c-41ac-824a-ef6da2f6f6a2",
   "metadata": {},
   "source": [
    "We can create a mapping between symbologies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd8a4dc1-0a5b-4fd9-b21c-6c62346e82d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "figi = otp.Symbols('US_COMP_SAMPLE', symbology='FGV', show_original_symbols=True, for_tick_type='TRD')\n",
    "otp.run(figi, start=s, end=e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9510ca4-9f14-4ddf-907d-0e74ec45ee70",
   "metadata": {},
   "outputs": [],
   "source": [
    "btkr = otp.Symbols('US_COMP_SAMPLE', symbology='BTKR', show_original_symbols=True, for_tick_type='TRD')\n",
    "otp.run(btkr, start=s, end=e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58c2d540-438e-4086-87fc-043c443cc1ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "figi = otp.Symbols('US_COMP_SAMPLE', symbology='FGV', show_original_symbols=True, for_tick_type='TRD')\n",
    "btkr = otp.Symbols('US_COMP_SAMPLE', symbology='BTKR', show_original_symbols=True, for_tick_type='TRD')\n",
    "\n",
    "mapping = otp.functions.join(figi, btkr, on=btkr['ORIGINAL_SYMBOL_NAME']==figi['ORIGINAL_SYMBOL_NAME'], how=\"inner\")\n",
    "\n",
    "mapping  = mapping.rename({'RIGHT_SYMBOL_NAME'   : 'BTKR',\n",
    "                           'SYMBOL_NAME'         : 'FIGI',\n",
    "                           'ORIGINAL_SYMBOL_NAME': 'DB_SYMBOL'})\n",
    "\n",
    "mapping = mapping[['FIGI', 'BTKR', 'DB_SYMBOL']]\n",
    "\n",
    "otp.run(mapping, start=s, end=e)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4a5f2236-e7a9-4b20-859b-2fa3e3e0d7da",
   "metadata": {},
   "source": [
    "# Symbol name changes\n",
    "Symbol name changes are easy to handle with the `symbol_date` parameter\n",
    "of {func}`otp.run <onetick.py.run>` which provides the \"as of\" date for the symbols.\n",
    "For example, we can pass `META` while setting `symbol_date` to any date after 2022-06-09 or `FB` and `symbol_date` before 2022-06-09."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dd68a3d-4f69-47d4-ba08-ccd3a3f33377",
   "metadata": {},
   "outputs": [],
   "source": [
    "trd = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "otp.run(trd, start=s, end=e, symbols='META', symbol_date=otp.dt(2023, 2, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ca8dd4e-9212-4d8c-b063-3de40286e034",
   "metadata": {},
   "outputs": [],
   "source": [
    "trd = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "otp.run(trd, start=s, end=e, symbols='FB', symbol_date=otp.dt(2021, 2, 2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
