{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "markdown-1",
   "metadata": {},
   "source": [
    "# 4. An indicator\n",
    "\n",
    "A model forecasts. An **indicator** describes: it turns a series into a figure\n",
    "that helps you read it, such as a weekly mean or a day's range. Traders draw\n",
    "indicators beside prices to see what changed.\n",
    "\n",
    "An indicator works like a model with three differences:\n",
    "\n",
    "- it describes the past and present, so it rarely needs a cut-off before its\n",
    "  period;\n",
    "- it must be fast, at most 5 seconds a call, because charts call it often;\n",
    "- it is declared with `@bff.indicator(...)`."
   ]
  },
  {
   "cell_type": "code",
   "id": "code-2",
   "metadata": {},
   "source": [
    "from pathlib import Path\n",
    "\n",
    "from bff import lab\n",
    "\n",
    "FOLDER = Path(\"my_indicator\")\n",
    "if not FOLDER.exists():\n",
    "    lab.new(FOLDER, kind=\"indicator\")\n",
    "print((FOLDER / \"main.py\").read_text())"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "markdown-3",
   "metadata": {},
   "source": [
    "This template computes each market day's **range**: its highest price less its\n",
    "lowest. A big range means a day of sharp swings, when storage and flexible\n",
    "plants earn the most.\n",
    "\n",
    "Read the function's three lines:\n",
    "\n",
    "1. read the series over the call's period;\n",
    "2. group its values by day, on Berlin's clock;\n",
    "3. subtract each day's minimum from its maximum."
   ]
  },
  {
   "cell_type": "code",
   "id": "code-4",
   "metadata": {},
   "source": [
    "answer = lab.run(FOLDER)\n",
    "answer"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "code-5",
   "metadata": {},
   "source": [
    "answer.plot();"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "markdown-6",
   "metadata": {},
   "source": [
    "Each day's range sits at the start of that day. In this week, Tuesday swung the\n",
    "most, by about 45 EUR/MWh, and Friday the least. Ranges change with the wind\n",
    "and the sun as much as with the calendar: a windy night can push prices down\n",
    "while the evening peak stays high.\n",
    "\n",
    "## Try it yourself\n",
    "\n",
    "1. Change the indicator to give each day's mean instead of its range. Which\n",
    "   line changes?\n",
    "2. Add a second output, `peak`, each day's highest price. Declare it under\n",
    "   `outputs`, then return both: `call.answer(range=..., peak=...)`.\n",
    "3. Read `../examples/peak_spread/main.py`. It answers the gap between\n",
    "   expensive working hours and the whole day. How does it pick the hours?"
   ]
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 }
}
