{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from typing import List\n",
    "\n",
    "\n",
    "url_go = \"http://localhost:1323/optimize_portfolio\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "# EIMI{}MI{}IE00BKM4GZ66{}italy{}iShares Core MSCI Emerging Markets IMI UCITS{}2014-07-24{}2023-01-05\n",
    "# EMH5{}MI{}IE00BP46NG52{}italy{}SPDR BofA Merrill Lynch 0-5 Emerging Markets Government Bond UCITS{}2016-02-23{}2023-01-05\n",
    "# GGOV{}PA{}LU1437016204{}france{}Amundi Index Solutions - Amundi Index J.P. Morgan GBI Global Govies{}2017-02-15{}2023-01-05\n",
    "# SWDA{}MI{}IE00B4L5Y983{}italy{}iShares Core MSCI World UCITS ETF USD (Acc){}2010-02-01{}2023-01-05\n",
    "\n",
    "etf1 = {\n",
    "    \"ticker\":\"EIMI\",\n",
    "    \"isin\":\"IE00BKM4GZ66\",\n",
    "    \"exchange\":\"MI\",\n",
    "    \"country\":\"italy\",\n",
    "    \"name\":\"iShares Core MSCI Emerging Markets IMI UCITS\",\n",
    "    \"data_from\":\"2014-07-24\",\n",
    "    \"data_to\":\"2023-01-05\"\n",
    "}\n",
    "etf2 = {\n",
    "    \"ticker\":\"EMH5\",\n",
    "    \"isin\":\"IE00BP46NG52\",\n",
    "    \"exchange\":\"MI\",\n",
    "    \"country\":\"italy\",\n",
    "    \"name\":\"SPDR BofA Merrill Lynch 0-5 Emerging Markets Government Bond UCITS\",\n",
    "    \"data_from\":\"2016-02-23\",\n",
    "    \"data_to\":\"2023-01-05\"\n",
    "}\n",
    "etf3 = {\n",
    "    \"ticker\":\"GGOV\",\n",
    "    \"isin\":\"LU1437016204\",\n",
    "    \"exchange\":\"PA\",\n",
    "    \"country\":\"france\",\n",
    "    \"name\":\"Amundi Index Solutions - Amundi Index J.P. Morgan GBI Global Govies\",\n",
    "    \"data_from\":\"2017-02-15\",\n",
    "    \"data_to\":\"2023-01-05\"\n",
    "}\n",
    "etf4 = {\n",
    "    \"ticker\":\"SWDA\",\n",
    "    \"isin\":\"IE00B4L5Y983\",\n",
    "    \"exchange\":\"MI\",\n",
    "    \"country\":\"italy\",\n",
    "    \"name\":\"iShares Core MSCI World UCITS ETF USD (Acc)\",\n",
    "    \"data_from\":\"2010-02-01\",\n",
    "    \"data_to\":\"2023-01-05\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "opt_request = {\n",
    "    \"etfs\": [etf1, etf2, etf3, etf4],\n",
    "    \"prefer_recent_data\": False,\n",
    "    \"min_weight\": float(0),\n",
    "    \"num_layers\": int(3)\n",
    "}\n",
    "\n",
    "\n",
    "response = requests.post(url_go, json=opt_request)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ERR:( [{\"etfs\":[{\"ticker\":\"EIMI\",\"exchange\":\"MI\",\"isin\":\"IE00BKM4GZ66\",\"percent\":3.3,\"last_price\":27.815},{\"ticker\":\"EMH5\",\"exchange\":\"MI\",\"isin\":\"IE00BP46NG52\",\"percent\":34.84,\"last_price\":25.065},{\"ticker\":\"GGOV\",\"exchange\":\"PA\",\"isin\":\"LU1437016204\",\"percent\":58.39,\"last_price\":47.05},{\"ticker\":\"SWDA\",\"exchange\":\"MI\",\"isin\":\"IE00B4L5Y983\",\"percent\":3.47,\"last_price\":69.38}],\"risk\":0.051729455520849864,\"expected_return\":0.00814539701488171,\"sharpe_ratio\":0.13813014157865466},{\"etfs\":[{\"ticker\":\"EIMI\",\"exchange\":\"MI\",\"isin\":\"IE00BKM4GZ66\",\"percent\":0,\"last_price\":27.815},{\"ticker\":\"EMH5\",\"exchange\":\"MI\",\"isin\":\"IE00BP46NG52\",\"percent\":32.03,\"last_price\":25.065},{\"ticker\":\"GGOV\",\"exchange\":\"PA\",\"isin\":\"LU1437016204\",\"percent\":25.04,\"last_price\":47.05},{\"ticker\":\"SWDA\",\"exchange\":\"MI\",\"isin\":\"IE00B4L5Y983\",\"percent\":42.94,\"last_price\":69.38}],\"risk\":0.08633311621506369,\"expected_return\":0.05844649722173568,\"sharpe_ratio\":0.6654051161391094},{\"etfs\":[{\"ticker\":\"EIMI\",\"exchange\":\"MI\",\"isin\":\"IE00BKM4GZ66\",\"percent\":0,\"last_price\":27.815},{\"ticker\":\"EMH5\",\"exchange\":\"MI\",\"isin\":\"IE00BP46NG52\",\"percent\":29.96,\"last_price\":25.065},{\"ticker\":\"GGOV\",\"exchange\":\"PA\",\"isin\":\"LU1437016204\",\"percent\":5.73,\"last_price\":47.05},{\"ticker\":\"SWDA\",\"exchange\":\"MI\",\"isin\":\"IE00B4L5Y983\",\"percent\":64.31,\"last_price\":69.38}],\"risk\":0.12093677966257205,\"expected_return\":0.08641993052770258,\"sharpe_ratio\":0.7063188780620281}]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "if response.status_code != 200:\n",
    "    print(\"200!! \" + response.text)\n",
    "else:\n",
    "    print(\"ERR:( \" + response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[75], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m response\u001b[39m.\u001b[39;49mjson()[\u001b[39m'\u001b[39;49m\u001b[39mlayers\u001b[39;49m\u001b[39m'\u001b[39;49m]\n",
      "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "response.json()['layers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    {\n",
      "        \"etfs\": [\n",
      "            {\n",
      "                \"ticker\": \"EIMI\",\n",
      "                \"isin\": \"IE00BKM4GZ66\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 3.3,\n",
      "                \"last_price\": 27.815\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"EMH5\",\n",
      "                \"isin\": \"IE00BP46NG52\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 34.84,\n",
      "                \"last_price\": 25.065\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"GGOV\",\n",
      "                \"isin\": \"LU1437016204\",\n",
      "                \"excange\": \"france\",\n",
      "                \"percent\": 58.39,\n",
      "                \"last_price\": 47.05\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"SWDA\",\n",
      "                \"isin\": \"IE00B4L5Y983\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 3.47,\n",
      "                \"last_price\": 69.38\n",
      "            }\n",
      "        ],\n",
      "        \"risk\": 0.051729455520849864,\n",
      "        \"expected_return\": 0.00814539701488171,\n",
      "        \"sharpe_ratio\": 0.13813014157865466\n",
      "    },\n",
      "    {\n",
      "        \"etfs\": [\n",
      "            {\n",
      "                \"ticker\": \"EIMI\",\n",
      "                \"isin\": \"IE00BKM4GZ66\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 0.0,\n",
      "                \"last_price\": 27.815\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"EMH5\",\n",
      "                \"isin\": \"IE00BP46NG52\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 32.03,\n",
      "                \"last_price\": 25.065\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"GGOV\",\n",
      "                \"isin\": \"LU1437016204\",\n",
      "                \"excange\": \"france\",\n",
      "                \"percent\": 25.04,\n",
      "                \"last_price\": 47.05\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"SWDA\",\n",
      "                \"isin\": \"IE00B4L5Y983\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 42.94,\n",
      "                \"last_price\": 69.38\n",
      "            }\n",
      "        ],\n",
      "        \"risk\": 0.08633311621506369,\n",
      "        \"expected_return\": 0.05844649722173568,\n",
      "        \"sharpe_ratio\": 0.6654051161391094\n",
      "    },\n",
      "    {\n",
      "        \"etfs\": [\n",
      "            {\n",
      "                \"ticker\": \"EIMI\",\n",
      "                \"isin\": \"IE00BKM4GZ66\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 0.0,\n",
      "                \"last_price\": 27.815\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"EMH5\",\n",
      "                \"isin\": \"IE00BP46NG52\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 29.96,\n",
      "                \"last_price\": 25.065\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"GGOV\",\n",
      "                \"isin\": \"LU1437016204\",\n",
      "                \"excange\": \"france\",\n",
      "                \"percent\": 5.73,\n",
      "                \"last_price\": 47.05\n",
      "            },\n",
      "            {\n",
      "                \"ticker\": \"SWDA\",\n",
      "                \"isin\": \"IE00B4L5Y983\",\n",
      "                \"excange\": \"italy\",\n",
      "                \"percent\": 64.31,\n",
      "                \"last_price\": 69.38\n",
      "            }\n",
      "        ],\n",
      "        \"risk\": 0.12093677966257205,\n",
      "        \"expected_return\": 0.08641993052770258,\n",
      "        \"sharpe_ratio\": 0.7063188780620281\n",
      "    }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "#pretty print\n",
    "print(json.dumps(resp, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "server.ETFOut() argument after ** must be a mapping, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 4\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[39mfor\u001b[39;00m layer \u001b[39min\u001b[39;00m resp:\n\u001b[1;32m      3\u001b[0m     \u001b[39mfor\u001b[39;00m etf \u001b[39min\u001b[39;00m layer:\n\u001b[0;32m----> 4\u001b[0m         e \u001b[39m=\u001b[39m ETFOut(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39metf)\n\u001b[1;32m      5\u001b[0m         \u001b[39mprint\u001b[39m(e\u001b[39m.\u001b[39mticker, e\u001b[39m.\u001b[39mpercent, e\u001b[39m.\u001b[39mlast_price, \u001b[39m\"\u001b[39m\u001b[39m|\u001b[39m\u001b[39m\"\u001b[39m, end\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m \u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m      6\u001b[0m     \u001b[39mprint\u001b[39m(layer\u001b[39m.\u001b[39mrisk, layer\u001b[39m.\u001b[39mexpected_return, layer\u001b[39m.\u001b[39msharpe_ratio)\n",
      "\u001b[0;31mTypeError\u001b[0m: server.ETFOut() argument after ** must be a mapping, not str"
     ]
    }
   ],
   "source": [
    "# pretty print\n",
    "for layer in resp:\n",
    "    for etf in layer:\n",
    "        e = ETFOut(**etf)\n",
    "        print(e.ticker, e.percent, e.last_price, \"|\", end=\" \")\n",
    "    print(layer.risk, layer.expected_return, layer.sharpe_ratio)\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
