{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "726a1a65",
   "metadata": {},
   "source": [
    "# Strategia Regressione"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20f402a9",
   "metadata": {},
   "source": [
    "<b><font color=RED size=+2>We predict returns and not prices!</font></b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddee183e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use(\"seaborn\")\n",
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd04ed7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "import tpqoa\n",
    "api = tpqoa.tpqoa(\"oandaMY.cfg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70e7e445",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH IBKR\n",
    "# ib.disconnect()\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": "5af2098d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "df = api.get_history(instrument = \"EUR_USD\", start = \"2024-08-24\", end = \"2024-09-24\", granularity = \"M5\", 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 = df.close.to_frame()\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bcba56f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH IBKR\n",
    "df = pd.DataFrame(ib.reqHistoricalData( Forex(\"EURUSD\"), endDateTime = \"20240924 00:00:00\", \n",
    "                                  barSizeSetting=\"5 mins\", durationStr=\"1 M\", \n",
    "                                  whatToShow= \"MIDPOINT\", useRTH = True, formatDate=2 )).set_index(\"date\")\n",
    "df.index=pd.to_datetime(df.index)\n",
    "df = df.close.to_frame()\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc6268e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8d451bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10717ab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[\"2023-10-20\":\"2023-10-27\"].plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c1cac66",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "894b77fe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96956275",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "425301a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71f171da",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0f0d505",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fba75bbc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37eaab00",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e378cd30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77800f7b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3eb11a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59add8ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ad761f8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67a3b2d3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c2021f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5097783",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbafe012",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fc892d4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6db990a0",
   "metadata": {},
   "source": [
    "# Regressione multilineare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46fd34f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = api.get_history(instrument=\"EUR_USD\", start=\"2023-01-01\", \n",
    "                     end=\"2023-10-29\" , granularity = \"M5\", price = \"M\")[\"c\"].to_frame().dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "828f0d0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ee542f9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fff9d50",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d90c6e81",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54c5afc0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e50c3370",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242ba660",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8120ee9b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4868f4a1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d4dafc6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45037d68",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc16eaa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9c87b6f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79db5959",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a00dd2e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ed8b4a3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "868e4fec",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0528ca8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e989566",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = api.get_history(instrument=\"EUR_USD\", start=\"2023-01-01\", \n",
    "                     end=\"2023-10-29\" , granularity = \"H3\", price = \"M\")[\"c\"].to_frame().dropna()\n",
    "df2[\"logRet\"]=np.log(df2.c/df2.c.shift(1))\n",
    "lags=5\n",
    "colonne=[]\n",
    "for lag in range(1,lags+1):\n",
    "    col = \"logRet\"+str(lag)\n",
    "    df2[col]=df2.logRet.shift(lag)\n",
    "    colonne.append(col)\n",
    "df2.dropna(inplace=True)\n",
    "lm = LinearRegression(fit_intercept=True)\n",
    "lm.fit(X=df2[colonne],y=df2.logRet)\n",
    "df2[\"predicted\"]=np.sign(lm.predict(df2[colonne]))\n",
    "df2[\"transazioni\"]= abs(df2.predicted-df2.predicted.shift(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25a25199",
   "metadata": {},
   "outputs": [],
   "source": [
    "(df2.predicted*df2.logRet-df2.transazioni*(comm+spread/2)).cumsum().plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8745b736",
   "metadata": {},
   "source": [
    "# Forward testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20e10a0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ATTENZIONE che lo addestriamo sui dati dell'anno precedente\n",
    "train = api.get_history(instrument=\"EUR_USD\", start=\"2022-01-01\", \n",
    "                     end=\"2022-10-29\" , granularity = \"H3\", price = \"M\")[\"c\"].to_frame().dropna()\n",
    "train[\"logRet\"]=np.log(train.c/train.c.shift(1))\n",
    "lags=5\n",
    "colonne=[]\n",
    "for lag in range(1,lags+1):\n",
    "    col = \"logRet\"+str(lag)\n",
    "    train[col]=train.logRet.shift(lag)\n",
    "    colonne.append(col)\n",
    "train.dropna(inplace=True)\n",
    "lm2022 = LinearRegression(fit_intercept=True)\n",
    "lm2022.fit(X=train[colonne],y=train.logRet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "995cacf3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0a857d2",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf96625e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4758d6e1",
   "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
}
