{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValidationError",
     "evalue": "1 validation error for StockBarsRequest\ntimeframe\n  instance of TimeFrame expected (type=type_error.arbitrary_type; expected_arbitrary_type=TimeFrame)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValidationError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[97], line 35\u001b[0m\n\u001b[1;32m     30\u001b[0m \u001b[39m#print(time_from)\u001b[39;00m\n\u001b[1;32m     31\u001b[0m \n\u001b[1;32m     32\u001b[0m \u001b[39m# vytvorim request objekt\u001b[39;00m\n\u001b[1;32m     33\u001b[0m \u001b[39m#latestQuoteRequest = StockLatestQuoteRequest(symbol_or_symbols=[\"SPY\", \"GLD\", \"TLT\"])\u001b[39;00m\n\u001b[1;32m     34\u001b[0m stockTradeRequest \u001b[39m=\u001b[39m StockTradesRequest(symbol_or_symbols\u001b[39m=\u001b[39m[\u001b[39m\"\u001b[39m\u001b[39mBAC\u001b[39m\u001b[39m\"\u001b[39m], start\u001b[39m=\u001b[39mtime_from,end\u001b[39m=\u001b[39mtime_to)\n\u001b[0;32m---> 35\u001b[0m stockBarRequest \u001b[39m=\u001b[39m StockBarsRequest(symbol_or_symbols\u001b[39m=\u001b[39;49m[\u001b[39m\"\u001b[39;49m\u001b[39mBAC\u001b[39;49m\u001b[39m\"\u001b[39;49m], start\u001b[39m=\u001b[39;49mtime_from,end\u001b[39m=\u001b[39;49mtime_to, timeframe\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m15s\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n\u001b[1;32m     37\u001b[0m \u001b[39m#zavolam na clientovi metodu s request objektem, vrací se mi Dict[str, Quote] - obj.Quote pro kazdy symbol\u001b[39;00m\n\u001b[1;32m     38\u001b[0m \u001b[39m#latestQuoteObject = stock_client.get_stock_latest_quote(latestQuoteRequest)\u001b[39;00m\n\u001b[1;32m     39\u001b[0m \u001b[39m#tradesResponse = stock_client.get_stock_trades(stockTradeRequest).df\u001b[39;00m\n\u001b[1;32m     40\u001b[0m stocksResponse \u001b[39m=\u001b[39m stock_client\u001b[39m.\u001b[39mget_stock_bars(stockBarRequest)\u001b[39m.\u001b[39mdf\n",
      "File \u001b[0;32m~/Documents/Development/python/trading/.venv/lib/python3.11/site-packages/alpaca/data/requests.py:45\u001b[0m, in \u001b[0;36mBaseTimeseriesDataRequest.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m     37\u001b[0m \u001b[39mif\u001b[39;00m (\n\u001b[1;32m     38\u001b[0m     \u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m \u001b[39min\u001b[39;00m data\n\u001b[1;32m     39\u001b[0m     \u001b[39mand\u001b[39;00m data[\u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m     40\u001b[0m     \u001b[39mand\u001b[39;00m \u001b[39misinstance\u001b[39m(data[\u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m], datetime)\n\u001b[1;32m     41\u001b[0m     \u001b[39mand\u001b[39;00m data[\u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m]\u001b[39m.\u001b[39mtzinfo \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m     42\u001b[0m ):\n\u001b[1;32m     43\u001b[0m     data[\u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m data[\u001b[39m\"\u001b[39m\u001b[39mend\u001b[39m\u001b[39m\"\u001b[39m]\u001b[39m.\u001b[39mastimezone(pytz\u001b[39m.\u001b[39mutc)\u001b[39m.\u001b[39mreplace(tzinfo\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m)\n\u001b[0;32m---> 45\u001b[0m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49m\u001b[39m__init__\u001b[39;49m(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mdata)\n",
      "File \u001b[0;32m~/Documents/Development/python/trading/.venv/lib/python3.11/site-packages/pydantic/main.py:342\u001b[0m, in \u001b[0;36mpydantic.main.BaseModel.__init__\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValidationError\u001b[0m: 1 validation error for StockBarsRequest\ntimeframe\n  instance of TimeFrame expected (type=type_error.arbitrary_type; expected_arbitrary_type=TimeFrame)"
     ]
    }
   ],
   "source": [
    "# 2 clients for historical data StockHistoricalDataClient (needs keys), CryptoHistoricalDataClient\n",
    "# 2 clients for real time data CryptoDataStream, StockDataStream\n",
    "\n",
    "\n",
    "# naimportuju si daneho clienta\n",
    "from alpaca.data.historical import StockHistoricalDataClient, CryptoHistoricalDataClient\n",
    "\n",
    "#pokdu pouzivam historicke data(tzn. REST) tak si naimportuju dany request object\n",
    "from alpaca.data.requests import StockLatestQuoteRequest, StockBarsRequest, StockTradesRequest\n",
    "\n",
    "#objekty se kterymi pak pracuju (jsou soucasi package výše, tady jen informačně)\n",
    "from alpaca.data import Quote, Trade, Snapshot, Bar\n",
    "from alpaca.data.models import BarSet, QuoteSet, TradeSet\n",
    "\n",
    "\n",
    "from config import API_KEY, SECRET_KEY\n",
    "import datetime\n",
    "import pandas as pd\n",
    "\n",
    "# vytvorim si clienta\n",
    "stock_client = StockHistoricalDataClient(API_KEY, SECRET_KEY, raw_data=False)\n",
    "\n",
    "sloupce=[\"symbol\",\"timestamp\",\"exchange\",\"price\",\"size\",\"id\",\"conditions\",\"tape\"]\n",
    "\n",
    "# deklarace globalniho df s timeindexem\n",
    "#gdf = pd.DataFrame(columns=sloupce, index=pd.to_datetime([]))\n",
    "\n",
    "time_from = datetime.datetime(2023, 2, 17, 14, 50, 0, 0)\n",
    "time_to = datetime.datetime(2023, 2, 17, 14, 55, 1, 0)\n",
    "#print(time_from)\n",
    "\n",
    "# vytvorim request objekt\n",
    "#latestQuoteRequest = StockLatestQuoteRequest(symbol_or_symbols=[\"SPY\", \"GLD\", \"TLT\"])\n",
    "stockTradeRequest = StockTradesRequest(symbol_or_symbols=[\"BAC\"], start=time_from,end=time_to)\n",
    "stockBarRequest = StockBarsRequest(symbol_or_symbols=[\"BAC\"], start=time_from,end=time_to, timeframe=\"15s\")\n",
    "\n",
    "#zavolam na clientovi metodu s request objektem, vrací se mi Dict[str, Quote] - obj.Quote pro kazdy symbol\n",
    "#latestQuoteObject = stock_client.get_stock_latest_quote(latestQuoteRequest)\n",
    "#tradesResponse = stock_client.get_stock_trades(stockTradeRequest).df\n",
    "stocksResponse = stock_client.get_stock_bars(stockBarRequest).df\n",
    "\n",
    "#data = [{'t': '2023-02-17T14:50:00.582845696Z', 'x': 'D', 'p': 34.83, 's': 1, 'c': [' ', 'I'], 'i': 71675642337847, 'z': 'A'}, {'t': '2023-02-17T14:50:00.948229632Z', 'x': 'D', 'p': 34.8383, 's': 10, 'c': [' ', 'I'], 'i': 79371872323411, 'z': 'A'}]\n",
    "# data = [{   'conditions': [' ', 'I'],\n",
    "#     'exchange': 'D',\n",
    "#     'id': 71675642337847,\n",
    "#     'price': 34.83,\n",
    "#     'size': 1.0,\n",
    "#     'symbol': 'BAC',\n",
    "#     'tape': 'A',\n",
    "#     'timestamp': datetime.datetime(2023, 2, 17, 14, 50, 0, 582845, tzinfo=datetime.timezone.utc)}, {   'conditions': [' ', 'I'],\n",
    "#     'exchange': 'D',\n",
    "#     'id': 79371872323411,\n",
    "#     'price': 34.8383,\n",
    "#     'size': 10.0,\n",
    "#     'symbol': 'BAC',\n",
    "#     'tape': 'A',\n",
    "#     'timestamp': datetime.datetime(2023, 2, 17, 14, 50, 0, 948229, tzinfo=datetime.timezone.utc)}, {   'conditions': [' ', 'I'],\n",
    "#     'exchange': 'D',\n",
    "#     'id': 71675642400306,\n",
    "#     'price': 34.835,\n",
    "#     'size': 1.0,\n",
    "#     'symbol': 'BAC',\n",
    "#     'tape': 'A',\n",
    "#     'timestamp': datetime.datetime(2023, 2, 17, 14, 50, 1, 870989, tzinfo=datetime.timezone.utc)}, {   'conditions': [' ', 'I'],\n",
    "#     'exchange': 'D',\n",
    "#     'id': 71675642400308,\n",
    "#     'price': 34.84,\n",
    "#     'size': 100.0,\n",
    "#     'symbol': 'BAC',\n",
    "#     'tape': 'A',\n",
    "#     'timestamp': datetime.datetime(2023, 2, 17, 14, 55, 0, 88460, tzinfo=datetime.timezone.utc)}]\n",
    "# datetime.datetime(2023, 2, 17, 14, 50, 0, 948229, tzinfo=datetime.timezone.utc)\n",
    "#data = tradesResponse\n",
    "\n",
    "#gdf = pd.DataFrame.from_dict(data=data, orient='index')\n",
    "#gdf = pd.DataFrame(data)\n",
    "\n",
    "#gdf = pd.DataFrame(tradesResponse.data[\"BAC\"])\n",
    "\n",
    "# works with raw data\n",
    "#gdf = pd.DataFrame([t for t in tradesResponse[\"BAC\"]], columns=sloupce)\n",
    "\n",
    "#gdf = tradesResponse.df\n",
    "print(stocksResponse)\n",
    "# print(tradesResponse)\n",
    "#print(tradesResponse[\"BAC\"])\n",
    "# print(tradesResponse.data[\"BAC\"])\n",
    "\n",
    "# positions_df = pd.concat((pd.DataFrame(position).set_index(0) for position in positions),axis=1)\n",
    "# positions_df = positions_df.T.apply(pd.to_numeric, errors='ignore').T # convert strings to numeric\n",
    "# For orders:\n",
    "# orders_df = pd.concat((pd.DataFrame(order).set_index(0) for order in orders),axis=1).T\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9391835cf7167c62e8e53032533e4da7e63c83f818ef5f19912128bc45706236"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
