{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': 1725747180059, 'error': None}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# URL for AnkiConnect\n",
    "ANKI_CONNECT_URL = 'http://localhost:8765'\n",
    "\n",
    "def add_note(deck_name, model_name, fields, tags=[]):\n",
    "    # Define the note structure\n",
    "    note = {\n",
    "        \"deckName\": deck_name,\n",
    "        \"modelName\": model_name,\n",
    "        \"fields\": fields,\n",
    "        \"tags\": tags,\n",
    "        \"options\": {\n",
    "            \"allowDuplicate\": False\n",
    "        }\n",
    "    }\n",
    "\n",
    "    # Define the request payload\n",
    "    payload = {\n",
    "        \"action\": \"addNote\",\n",
    "        \"version\": 6,\n",
    "        \"params\": {\n",
    "            \"note\": note\n",
    "        }\n",
    "    }\n",
    "\n",
    "    # Send the request to AnkiConnect\n",
    "    response = requests.post(ANKI_CONNECT_URL, json=payload)\n",
    "    return response.json()\n",
    "\n",
    "# Example usage\n",
    "deck_name = \"Default\"\n",
    "model_name = \"Basic\"\n",
    "fields = {\n",
    "    \"Front\": \"What is the capital of France?\",\n",
    "    \"Back\": \"Paris\"\n",
    "}\n",
    "tags = [\"geography\"]\n",
    "\n",
    "result = add_note(deck_name, model_name, fields, tags)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.9.20\n",
      "asttokens==2.4.1\n",
      "comm==0.2.2\n",
      "debugpy==1.8.5\n",
      "decorator==5.1.1\n",
      "exceptiongroup==1.2.2\n",
      "executing==2.1.0\n",
      "importlib_metadata==8.5.0\n",
      "ipykernel==6.29.5\n",
      "ipython==8.18.1\n",
      "jedi==0.19.1\n",
      "jupyter_client==8.6.2\n",
      "jupyter_core==5.7.2\n",
      "matplotlib-inline==0.1.7\n",
      "nest-asyncio==1.6.0\n",
      "packaging==24.1\n",
      "parso==0.8.4\n",
      "pexpect==4.9.0\n",
      "platformdirs==4.3.2\n",
      "prompt_toolkit==3.0.47\n",
      "psutil==6.0.0\n",
      "ptyprocess==0.7.0\n",
      "pure_eval==0.2.3\n",
      "Pygments==2.18.0\n",
      "python-dateutil==2.9.0.post0\n",
      "pyzmq==26.2.0\n",
      "six==1.16.0\n",
      "stack-data==0.6.3\n",
      "tornado==6.4.1\n",
      "traitlets==5.14.3\n",
      "typing_extensions==4.12.2\n",
      "wcwidth==0.2.13\n",
      "zipp==3.20.2\n"
     ]
    }
   ],
   "source": [
    "!python --version\n",
    "!pip freeze\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fracdiff.sklearn import Fracdiff\n",
    "import pandas_datareader as pdr\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "# Set the start and end dates for the data\n",
    "start_date = '1990-01-01'\n",
    "end_date= '2023-06-01'\n",
    "# Fetch S&P 500 price data\n",
    "data = np.array((pdr.get_data_fred('SP500', start = start_date,end = end_date)).dropna())\n",
    "# Calculate the fractional differentiation\n",
    "window = 100\n",
    "f = Fracdiff(0.48, mode = 'valid', window = window)\n",
    "frac_data = f.fit_transform(data)\n",
    "# Calculate a simple differencing function for comparison\n",
    "diff_data = np.reshape(np.diff(data[:, 0]), (-1, 1))\n",
    "# Harmonizing time indices\n",
    "data = data[window-1:, ]\n",
    "diff_data = diff_data[window-2:, ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.lines.Line2D at 0x7f28bf5a7a60>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 75144 characters]",
      "text/plain": [
       "<Figure size 640x480 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = plt.subplots(nrows = 3, ncols = 1)\n",
    "axes[0].plot(data[5:,], label = 'S&P 500', color = 'blue', linewidth = 1)\n",
    "axes[1].plot(frac_data[5:,], label =\n",
    "'Fractionally Differentiated S&P 500 (0.48)',\n",
    "color = 'orange', linewidth = 1)\n",
    "axes[2].plot(diff_data[5:,], label =\n",
    "'Differenced S&P 500', color = 'green', linewidth = 1)\n",
    "axes[0].legend()\n",
    "axes[1].legend()\n",
    "axes[2].legend()\n",
    "axes[0].grid()\n",
    "axes[1].grid()\n",
    "axes[2].grid()\n",
    "axes[1].axhline(y = 0, color = 'black', linestyle = 'dashed')\n",
    "axes[2].axhline(y = 0, color = 'black', linestyle = 'dashed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "General",
   "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.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
