{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from typing import List\n",
    "\n",
    "url_go = \"http://127.0.0.1:8090/api/portfolio/optimize\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "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": 16,
   "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": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200:) [\n",
      "  {\n",
      "    \"etfs\": [\n",
      "      {\n",
      "        \"ticker\": \"EIMI\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BKM4GZ66\",\n",
      "        \"percent\": 3.3,\n",
      "        \"last_price\": 27.815\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"EMH5\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BP46NG52\",\n",
      "        \"percent\": 34.84,\n",
      "        \"last_price\": 25.065\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"GGOV\",\n",
      "        \"exchange\": \"PA\",\n",
      "        \"isin\": \"LU1437016204\",\n",
      "        \"percent\": 58.39,\n",
      "        \"last_price\": 47.05\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"SWDA\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00B4L5Y983\",\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",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BKM4GZ66\",\n",
      "        \"percent\": 0,\n",
      "        \"last_price\": 27.815\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"EMH5\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BP46NG52\",\n",
      "        \"percent\": 32.03,\n",
      "        \"last_price\": 25.065\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"GGOV\",\n",
      "        \"exchange\": \"PA\",\n",
      "        \"isin\": \"LU1437016204\",\n",
      "        \"percent\": 25.04,\n",
      "        \"last_price\": 47.05\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"SWDA\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00B4L5Y983\",\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",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BKM4GZ66\",\n",
      "        \"percent\": 0,\n",
      "        \"last_price\": 27.815\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"EMH5\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00BP46NG52\",\n",
      "        \"percent\": 29.96,\n",
      "        \"last_price\": 25.065\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"GGOV\",\n",
      "        \"exchange\": \"PA\",\n",
      "        \"isin\": \"LU1437016204\",\n",
      "        \"percent\": 5.73,\n",
      "        \"last_price\": 47.05\n",
      "      },\n",
      "      {\n",
      "        \"ticker\": \"SWDA\",\n",
      "        \"exchange\": \"MI\",\n",
      "        \"isin\": \"IE00B4L5Y983\",\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",
      "\n"
     ]
    }
   ],
   "source": [
    "if response.status_code != 200:\n",
    "    print(\"ERR!! \" + response.text)\n",
    "else:\n",
    "    print(\"200:) \" + response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'etfs': [{'ticker': 'EIMI',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BKM4GZ66',\n",
       "    'percent': 3.3,\n",
       "    'last_price': 27.815},\n",
       "   {'ticker': 'EMH5',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BP46NG52',\n",
       "    'percent': 34.84,\n",
       "    'last_price': 25.065},\n",
       "   {'ticker': 'GGOV',\n",
       "    'exchange': 'PA',\n",
       "    'isin': 'LU1437016204',\n",
       "    'percent': 58.39,\n",
       "    'last_price': 47.05},\n",
       "   {'ticker': 'SWDA',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00B4L5Y983',\n",
       "    'percent': 3.47,\n",
       "    'last_price': 69.38}],\n",
       "  'risk': 0.051729455520849864,\n",
       "  'expected_return': 0.00814539701488171,\n",
       "  'sharpe_ratio': 0.13813014157865466},\n",
       " {'etfs': [{'ticker': 'EIMI',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BKM4GZ66',\n",
       "    'percent': 0,\n",
       "    'last_price': 27.815},\n",
       "   {'ticker': 'EMH5',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BP46NG52',\n",
       "    'percent': 32.03,\n",
       "    'last_price': 25.065},\n",
       "   {'ticker': 'GGOV',\n",
       "    'exchange': 'PA',\n",
       "    'isin': 'LU1437016204',\n",
       "    'percent': 25.04,\n",
       "    'last_price': 47.05},\n",
       "   {'ticker': 'SWDA',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00B4L5Y983',\n",
       "    'percent': 42.94,\n",
       "    'last_price': 69.38}],\n",
       "  'risk': 0.08633311621506369,\n",
       "  'expected_return': 0.05844649722173568,\n",
       "  'sharpe_ratio': 0.6654051161391094},\n",
       " {'etfs': [{'ticker': 'EIMI',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BKM4GZ66',\n",
       "    'percent': 0,\n",
       "    'last_price': 27.815},\n",
       "   {'ticker': 'EMH5',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00BP46NG52',\n",
       "    'percent': 29.96,\n",
       "    'last_price': 25.065},\n",
       "   {'ticker': 'GGOV',\n",
       "    'exchange': 'PA',\n",
       "    'isin': 'LU1437016204',\n",
       "    'percent': 5.73,\n",
       "    'last_price': 47.05},\n",
       "   {'ticker': 'SWDA',\n",
       "    'exchange': 'MI',\n",
       "    'isin': 'IE00B4L5Y983',\n",
       "    'percent': 64.31,\n",
       "    'last_price': 69.38}],\n",
       "  'risk': 0.12093677966257205,\n",
       "  'expected_return': 0.08641993052770258,\n",
       "  'sharpe_ratio': 0.7063188780620281}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#pretty print\n",
    "print(json.dumps(response.json(), 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"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
