{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2e3c64a7",
   "metadata": {},
   "source": [
    "# Filtering"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bfcab03a-8973-4d02-8cce-8461afad511a",
   "metadata": {},
   "source": [
    "Let's start with an unfiltered time series:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a171fb32-9ed7-4869-90ed-ab5a2fc676a6",
   "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",
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cd690ecc-9b96-4a9b-8adf-7e78e9f5dae7",
   "metadata": {},
   "source": [
    "We can filter by the value of a field using method {meth}`where <onetick.py.Source.where>`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0de743e9-a63a-4718-bc26-5b337458558b",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "q = q.where(q['EXCHANGE'] == 'K')\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fb697b63-adb6-4f32-90bc-a68c82c0a9b3",
   "metadata": {},
   "source": [
    "Note that all of the filtering is done in OneTick not in Python, which is much more efficient and lets us work with much bigger data sets."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f9e5df5e-f226-46af-b9de-9dcad5015402",
   "metadata": {},
   "source": [
    "Filtering for a specific trade condition can be done with the string matching methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3865d9ef-fac5-407f-89e9-bd8f5b068aa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "q = q.where(q['COND'].str.contains('I'))\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ef8e7e9b-e95b-44b0-a37c-ab3a2ecc9b15",
   "metadata": {},
   "source": [
    "Trade filter that limits attention to on-exchange continuous trading trades looks like this (it's used when creating bars):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77b9bc26-5b96-483d-89df-409d53c02eca",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "q = q.character_present(q['COND'], 'O6TUHILNRWZ47QMBCGPV', discard_on_match=True)\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ab43354f-56f7-43dc-b040-8b9441231a26",
   "metadata": {},
   "source": [
    "Filters can include 'and' and/or 'or' clauses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c9fc0db-94c5-4de5-b07f-9f71a89417a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "q = q.where((q['EXCHANGE'] == 'K') & (q['PRICE'] > 183.950))\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f106b6e",
   "metadata": {},
   "source": [
    "We can also return two branches after filtering with method {meth}`__getitem__ <onetick.py.Source.__getitem__>` (or {meth}`where_clause <onetick.py.Source.where_clause>`).\n",
    "The first branch contains all ticks that satisfy the condition and the other branch contains all ticks that do not satisfy the condition:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e6a04bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "exchange_k, other = q[q['EXCHANGE'] == 'K']\n",
    "otp.run(exchange_k, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad4a14e",
   "metadata": {},
   "outputs": [],
   "source": [
    "otp.run(other, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d055e73a",
   "metadata": {},
   "source": [
    "We can retrieve ticks by index with the help of\n",
    "{meth}`first <onetick.py.Source.first>` and {meth}`last <onetick.py.Source.last>`.\n",
    "\n",
    "The example below returns the second tick:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7e1f243",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "# get the last of the first two ticks, thus getting the second tick\n",
    "q = q.first(2)\n",
    "q = q.last()\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e36f507f",
   "metadata": {},
   "source": [
    "Python-like slice syntax is also supported:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65a048f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "q = q[['PRICE', 'SIZE', 'COND', 'EXCHANGE']]\n",
    "# get last three ticks\n",
    "q = q[-3:]\n",
    "otp.run(q, start=s, end=e, symbols=['AAPL'])"
   ]
  }
 ],
 "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
}
