{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a3f3dda9",
   "metadata": {},
   "source": [
    "# Using pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69a4a9d7",
   "metadata": {},
   "source": [
    "## pandas dataframe and series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c53ed57",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "import pandas as pd\n",
    "import tpqoa\n",
    "api = tpqoa.tpqoa(\"oandaMY.cfg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15bce2a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH IBKR\n",
    "import pandas as pd\n",
    "from ib_insync import *\n",
    "util.startLoop()  # not necessary in scripts\n",
    "ib = IB()\n",
    "ib.connect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2185da2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "df = api.get_history(instrument = \"EUR_USD\", start = \"2022-09-24\", end = \"2024-09-24\", granularity = \"D\", price = \"M\")\n",
    "# we change the column names to match IBKR\n",
    "df.rename(columns={\"o\":\"open\",\"h\":\"high\",\"l\":\"low\",\"c\":\"close\",\"time\":\"date\"},inplace=True)\n",
    "df.index.names = ['date']\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f390aa98",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(ib.reqHistoricalData( Forex(\"EURUSD\"), endDateTime = \"20240924 00:00:00\", \n",
    "                                  barSizeSetting=\"1 day\", durationStr=\"2 Y\", \n",
    "                                  whatToShow= \"MIDPOINT\", useRTH = True, formatDate=2 )).set_index(\"date\")\n",
    "df.index=pd.to_datetime(df.index)\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a175b3b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "651020eb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "863df835",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46817bdf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "935305a7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4917c3f4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e2c3132",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4996ea5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a6824e55",
   "metadata": {},
   "source": [
    "## Convert string to datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "078becda",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "742620f3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a0d8086",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2fdba82",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "388a1126",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ff92d0cb",
   "metadata": {},
   "source": [
    "**to_datetime** can handle many differen formats, such as yyyy-mm-dd, yyyymmdd, yyyy/mm/dd, yyyy mm dd, yyyy-mm-dd. However, always year, month, day, unless you specify the month in letters. Optionally, it accepts also time as hh, hh:mm, hh:mm:ss."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be2e5bae",
   "metadata": {},
   "source": [
    "## indexing and slicing time series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "befc984b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7a220f6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9aa5324",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6d544c3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9e320c7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3f15e19",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f2669dd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fe95a5b7",
   "metadata": {},
   "source": [
    "## Downsampling data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1db1d3d5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99d0f48d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0857b9ca",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "655d66a1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe1baac8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c17e5fea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf79fde6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
