{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "aef8b826",
   "metadata": {},
   "source": "# Rendering query graphs"
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "157bf535-0415-431d-a9d7-0375319e6bf1",
   "metadata": {},
   "source": [
    "Rendering ``onetick-py`` query is one of the helpful debug techniques. You can use either {meth}`otp.Source.render_otq <onetick.py.Source.render_otq>` for ``onetick-py`` query or {func}`render_otq <onetick.py.utils.render_otq>` function for rendering ``otq`` files.\n",
    "\n",
    "Here's an example for simple ``onetick-py`` query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91d63514-8e66-4680-be77-252937bdd85d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "import onetick.py as otp\n",
    "\n",
    "data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')\n",
    "data1, data2 = data[(data['PRICE'] > 50)]\n",
    "data = otp.merge([data1, data2])\n",
    "Image(data.render_otq(output_format='png'))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "56dbfa00-45b9-408c-bfc0-3ea78f601498",
   "metadata": {},
   "source": "Rendering function supports graphs with nested queries:"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34a94316-be74-4548-9f2f-0c7ea6781289",
   "metadata": {},
   "outputs": [],
   "source": [
    "def agg_fun(source):\n",
    "    return source.agg({\n",
    "        'volume': otp.agg.sum('SIZE'),\n",
    "        'vwap': otp.agg.vwap('PRICE', 'SIZE')\n",
    "    })\n",
    "\n",
    "data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')\n",
    "data = data[['PRICE', 'SIZE']]\n",
    "data = otp.agg.generic(agg_fun, bucket_interval=otp.Minute(1)).apply(data)\n",
    "Image(data.render_otq(output_format='png'))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "038be2be-08c2-432f-88c0-a021d3e6a737",
   "metadata": {},
   "source": "Query with multiple input sources:"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60d494a6-58ba-4b0a-9f5e-f6f01ecb9987",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_time = otp.dt(2024, 2, 1, 9, 30)\n",
    "end_time = otp.dt(2024, 2, 1, 9, 30, 1)\n",
    "\n",
    "trd = otp.DataSource('US_COMP_SAMPLE', tick_type='TRD')\n",
    "trd = trd[['PRICE', 'SIZE']]\n",
    "\n",
    "qte = otp.DataSource('US_COMP_SAMPLE', tick_type='NBBO')\n",
    "qte = qte[['BID_PRICE', 'ASK_PRICE']]\n",
    "\n",
    "enh_trd = otp.join_by_time([trd, qte])\n",
    "Image(enh_trd.render_otq(output_format='png', symbols=['AAPL'], start=start_time, end=end_time))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d28f734-8ffe-4525-9261-6f000944d27f",
   "metadata": {},
   "source": "Query with {ref}`Per Tick Script <python callable parser>`:"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fad649b3-41c1-4dbb-b130-12f47eac85e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fun(tick):\n",
    "    tick['VOLUME'] = tick['PRICE'] * tick['SIZE']\n",
    "    if tick['SIZE'] >= 1000:\n",
    "        tick['REQ_SIZE'] = 1\n",
    "    else:\n",
    "        tick['REQ_SIZE'] = 0\n",
    "\n",
    "data = otp.DataSource(db='US_COMP_SAMPLE', tick_type='TRD', symbols='AAPL')\n",
    "data = data.script(fun)\n",
    "Image(data.render_otq(output_format='png'))"
   ]
  }
 ],
 "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
}
