{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3966fa65-b8a8-4d09-9e2c-e031727a0d8c",
   "metadata": {},
   "source": [
    "# Daily OHLCV (with closing prices)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b7336ed2-69ef-4d96-860d-2b13184cfa1b",
   "metadata": {},
   "source": [
    "We can retrieve daily OHLCV data for specific tickers using various symbologies, by querying for the `DAY` Table.\n",
    "\n",
    "For consolidated datasets such as for the US, each exchange's data and a composite are represented.\n",
    "\n",
    "The *_DAILY* databases such as the sample *US_COMP_SAMPLE_DAILY* are optimized for querying long time periods,\n",
    "while the Tick and Bar databases are optimized for querying an intraday time range.\n",
    "\n",
    "Without Symbology:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "397be0a1-5817-4341-b0b2-4159c32558a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import onetick.py as otp\n",
    "\n",
    "data = otp.DataSource(db='US_COMP_SAMPLE_DAILY', tick_type='DAY')\n",
    "df = otp.run(data,\n",
    "             start=otp.dt(2024, 1, 1),\n",
    "             end=otp.dt(2024, 4, 1),\n",
    "             timezone='America/New_York',\n",
    "             symbols='CSCO')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccac7c7d",
   "metadata": {},
   "source": [
    "With Symbology:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6167e3a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = otp.DataSource(db='US_COMP_SAMPLE_DAILY', tick_type='DAY')\n",
    "df = otp.run(data,\n",
    "             start=otp.dt(2024, 1, 1),\n",
    "             end=otp.dt(2024, 4, 1),\n",
    "             timezone='America/New_York',\n",
    "             symbols='BSYM::::CSCO US Equity',\n",
    "             symbol_date=otp.dt(2024, 1, 3))\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "414be8fb",
   "metadata": {},
   "source": [
    "For US equities,\n",
    "`USPRIM` stands for the primary exchange,\n",
    "empty string for the composite,\n",
    "and `N` for the New York Stock Exchange.\n",
    "\n",
    "Most of the time you'll be looking for composite, which you can specify through a filter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "525d5aa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = otp.DataSource(db='US_COMP_SAMPLE_DAILY', tick_type='DAY')\n",
    "data = data[['CLOSE', 'VOLUME', 'EXCHANGE']]\n",
    "data = data.where(data['EXCHANGE'] == '')\n",
    "otp.run(data,\n",
    "        start=otp.dt(2024, 1, 1),\n",
    "        end=otp.dt(2024, 4, 1),\n",
    "        timezone='America/New_York',\n",
    "        symbols='BSYM::::CSCO US Equity',\n",
    "        symbol_date=otp.dt(2024, 1, 3))"
   ]
  }
 ],
 "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
}
