{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dab9f125",
   "metadata": {},
   "source": [
    "# Strategia regressione logistica"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df4ec140",
   "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 LogisticRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c72071a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "import tpqoa\n",
    "api = tpqoa.tpqoa(\"oandaMY.cfg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "048ebab9",
   "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": "762ffb38",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH OANDA\n",
    "train = api.get_history(instrument = \"EUR_USD\", start = \"2023-07-24\", end = \"2023-09-24\", granularity = \"M5\", price = \"M\")\n",
    "# we change the column names to match IBKR\n",
    "train.rename(columns={\"o\":\"open\",\"h\":\"high\",\"l\":\"low\",\"c\":\"close\",\"time\":\"date\"},inplace=True)\n",
    "train.index.names = ['date']\n",
    "train = train.close.to_frame()\n",
    "train.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2727895",
   "metadata": {},
   "outputs": [],
   "source": [
    "# WITH IBKR\n",
    "train = pd.DataFrame(ib.reqHistoricalData( Forex(\"EURUSD\"), endDateTime = \"20230924 00:00:00\", \n",
    "                                  barSizeSetting=\"5 mins\", durationStr=\"2 M\", \n",
    "                                  whatToShow= \"MIDPOINT\", useRTH = True, formatDate=2 )).set_index(\"date\")\n",
    "train.index=pd.to_datetime(train.index)\n",
    "train = train.close.to_frame()\n",
    "train.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa045b02",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e959e8f4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c271308",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf9c2e33",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f6f33a8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56a48db3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8feb27a1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc757e87",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b529dcf9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95bd2794",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc720e5b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b760e452",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137a5838",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f85fb7ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "test = api.get_history(instrument=\"EUR_USD\", start=\"2023-01-01\", \n",
    "                     end=\"2023-10-29\" , granularity = \"M5\", price = \"B\")[\"c\"].to_frame().dropna()\n",
    "test[\"logRet\"]=np.log(test.c/test.c.shift(1))\n",
    "lags=5\n",
    "colonne=[]\n",
    "for lag in range(1,lags+1):\n",
    "    col = \"logRet\"+str(lag)\n",
    "    test[col]=test.logRet.shift(lag)\n",
    "    colonne.append(col)\n",
    "test.dropna(inplace=True)\n",
    "\n",
    "# attenzione che NON RIFACCIO il modello\n",
    "\n",
    "test[\"predicted\"]=np.sign(lm2022.predict(test[colonne])) \n",
    "test[\"transazioni\"]= abs(test.predicted-test.predicted.shift(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a49b852a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d69cfce1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e1566c1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54a0d566",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1979b947",
   "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
}
