{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "77464d32-356e-47a4-9785-236538354ba4",
   "metadata": {},
   "source": [
    "# Corporate Actions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff008918-52a9-47ff-ad2f-31cd01f73ef5",
   "metadata": {},
   "source": [
    "# Adjusting prices for corporate actions\n",
    "\n",
    "We can use built-in functions to adjust price and volume for corporate actions. \n",
    "\n",
    "We follow an example of a 1:3 split for WMT on Feb 26, 2024.\n",
    "First let's review the unadjusted data: there appears to be a price jump."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce835966-ff9d-49ca-9699-8fa492c3b825",
   "metadata": {},
   "outputs": [],
   "source": [
    "import onetick.py as otp\n",
    "\n",
    "data = otp.DataSource('US_COMP_SAMPLE_DAILY', tick_type='DAY')\n",
    "data = data[['CLOSE', 'VOLUME', 'EXCHANGE']]\n",
    "data = data.where(data['EXCHANGE'] == '')\n",
    "result = otp.run(data,\n",
    "                 start=otp.dt(2024, 2, 10),\n",
    "                 end=otp.dt(2024, 3, 10),\n",
    "                 symbols='WMT')\n",
    "result.plot(x='Time', y='CLOSE')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dce18f90-916c-4cc8-9d0d-5425a63becc4",
   "metadata": {},
   "source": [
    "Adding adjustment for corporate actions fixes this.\n",
    "The prices are adjusted to the level before or after the split depending on the value of the `adjustement_date` parameter.\n",
    "The next example illustrates this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b87b8a61-6895-4494-a02a-86005003d3b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = otp.DataSource('US_COMP_SAMPLE_DAILY', tick_type='DAY')\n",
    "data = data[['CLOSE', 'VOLUME', 'EXCHANGE']]\n",
    "data = data.where(data['EXCHANGE'] == '')\n",
    "data['ORIG_CLOSE'] = data['CLOSE']\n",
    "data = data.corp_actions(fields='CLOSE', adjust_rule='PRICE', apply_split=True)\n",
    "result = otp.run(data,\n",
    "                 start=otp.dt(2024, 2, 10),\n",
    "                 end=otp.dt(2024, 3, 10),\n",
    "                 timezone='America/New_York',\n",
    "                 symbols='WMT',\n",
    "                 symbol_date=otp.dt.now())\n",
    "\n",
    "result.plot(x='Time', y='CLOSE')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d61f599-5f4d-4b1c-a12d-be0b5d16559c",
   "metadata": {},
   "source": [
    "Adjustment can be applied to sizes in a similar way:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f10f2416-d436-49a4-906a-9602a943dfc9",
   "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",
    "result = otp.run(data,\n",
    "                 start=otp.dt(2024, 2, 10),\n",
    "                 end=otp.dt(2024, 3, 10),\n",
    "                 timezone='America/New_York',\n",
    "                 symbols='WMT',\n",
    "                 symbol_date=otp.dt.now())\n",
    "result.plot(x='Time', y='VOLUME')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b0135db-c4a2-4e77-9691-aad9df241777",
   "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",
    "data['ORIG_VOLUME'] = data['VOLUME']\n",
    "data = data.corp_actions(fields='VOLUME', adjust_rule='SIZE', apply_split=True)\n",
    "result = otp.run(data,\n",
    "                 start=otp.dt(2024, 2, 10),\n",
    "                 end=otp.dt(2024, 3, 10),\n",
    "                 timezone='America/New_York',\n",
    "                 symbols='WMT',\n",
    "                 symbol_date=otp.dt.now())\n",
    "result.plot(x='Time', y='VOLUME')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12ad2156-038b-4aa0-ab2e-004b5968e384",
   "metadata": {},
   "source": [
    "# Retrieving Corporate actions for a symbol\n",
    "\n",
    "We can retrieve all corporate actions for the symbols of interest:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c83df246-39cd-49fd-aa3a-f029f051402a",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = otp.databases()['US_COMP_SAMPLE_DAILY']\n",
    "db.ref_data(\n",
    "    ref_data_type='corp_actions',\n",
    "    start=otp.dt(2024, 2, 10),\n",
    "    end=otp.dt(2024, 3, 10),\n",
    "    timezone='America/New_York',\n",
    "    symbol='WMT',\n",
    "    symbol_date=otp.dt.now(),\n",
    ")"
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
