{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0eb4ab92", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import datetime\n", "import numpy as np\n", "import uuid " ] }, { "cell_type": "code", "execution_count": 2, "id": "4ed42153", "metadata": {}, "outputs": [], "source": [ "class DealManager():\n", " \n", " def __init__(self):\n", " self.commission=0.04\n", " self.columns=['uuid','figi','amount','startPrice','profit']\n", " self.deals = pd.DataFrame(columns=self.columns)\n", " self.deals = self.deals.set_index('uuid')\n", " \n", " def findDealByPriceAndFig(self,price,figi):\n", " ans=None\n", " for i in range(self.deals.shape[0]):\n", " if self.deals.iloc[i].startPrice == price and self.deals.iloc[i].figi == figi:\n", " ans = self.deals.iloc[i].name\n", " break\n", " return ans\n", "\n", " def openDeal(self,figi,startPrice,amount=1):\n", " desiredDeal=self.findDealByPriceAndFig(startPrice,figi)\n", " if desiredDeal == None:\n", " newDealDict={\n", " 'uuid':[str(uuid.uuid4())],\n", " 'figi':[figi],\n", " 'startPrice':[startPrice],\n", " 'amount':[amount]\n", " }\n", "\n", " #newDealDict['profit']=[startPrice*pow(1+self.commission,2)]\n", "\n", "\n", "\n", " newDeal=pd.DataFrame.from_dict(newDealDict).set_index('uuid')\n", " self.deals=pd.concat([self.deals, newDeal])\n", " else:\n", " self.deals.at[desiredDeal,'amount'] += amount\n", "\n", " def closeDeal(self,uuid,amount):\n", " \n", " desiredDeal=self.deals.loc[uuid]\n", " if desiredDeal.amount - amount == 0:\n", " self.deals = self.deals.drop(labels = [uuid],axis = 0)\n", " else:\n", " self.deals.at[uuid,'amount'] -= amount\n", " #self.deals.loc[uuid].amount = desiredDeal.amount - amount\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "622b8115", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'commission': 0.04,\n", " 'columns': ['uuid', 'figi', 'amount', 'startPrice', 'profit'],\n", " 'deals': Empty DataFrame\n", " Columns: [figi, amount, startPrice, profit]\n", " Index: []}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t=DealManager()\n", "t.__dict__" ] }, { "cell_type": "code", "execution_count": null, "id": "e3509306", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 4, "id": "4fae838f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t.deals.shape[0]" ] }, { "cell_type": "code", "execution_count": 5, "id": "68ec95c6", "metadata": {}, "outputs": [], "source": [ "t.openDeal('huigi',100,1)\n", "t.openDeal('huigi',100,3)\n", "t.openDeal('huigi1',100,3)\n", "t.openDeal('huigi1',200,3)" ] }, { "cell_type": "code", "execution_count": 6, "id": "cc5f9cb2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
figiamountstartPriceprofit
uuid
a38e6fa8-c160-481c-b666-78ec9fade50bhuigi4100NaN
482abf6b-7a4f-4e49-aea6-1b5940277c24huigi13100NaN
6e940b6a-30e1-435c-b214-fc952aba02dchuigi13200NaN
\n", "
" ], "text/plain": [ " figi amount startPrice profit\n", "uuid \n", "a38e6fa8-c160-481c-b666-78ec9fade50b huigi 4 100 NaN\n", "482abf6b-7a4f-4e49-aea6-1b5940277c24 huigi1 3 100 NaN\n", "6e940b6a-30e1-435c-b214-fc952aba02dc huigi1 3 200 NaN" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t.deals" ] }, { "cell_type": "code", "execution_count": 7, "id": "eed2748a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
figiamountstartPriceprofit
uuid
482abf6b-7a4f-4e49-aea6-1b5940277c24huigi13100NaN
6e940b6a-30e1-435c-b214-fc952aba02dchuigi13200NaN
\n", "
" ], "text/plain": [ " figi amount startPrice profit\n", "uuid \n", "482abf6b-7a4f-4e49-aea6-1b5940277c24 huigi1 3 100 NaN\n", "6e940b6a-30e1-435c-b214-fc952aba02dc huigi1 3 200 NaN" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t.deals[t.deals.figi == 'huigi1']" ] }, { "cell_type": "code", "execution_count": null, "id": "d7a4a820", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 8, "id": "0cac1f5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "figi huigi\n", "amount 4\n", "startPrice 100\n", "profit NaN\n", "Name: a38e6fa8-c160-481c-b666-78ec9fade50b, dtype: object\n", "figi huigi1\n", "amount 3\n", "startPrice 100\n", "profit NaN\n", "Name: 482abf6b-7a4f-4e49-aea6-1b5940277c24, dtype: object\n", "figi huigi1\n", "amount 3\n", "startPrice 200\n", "profit NaN\n", "Name: 6e940b6a-30e1-435c-b214-fc952aba02dc, dtype: object\n" ] } ], "source": [ "for i in range(t.deals.shape[0]):\n", " print(t.deals.iloc[i])" ] }, { "cell_type": "code", "execution_count": 9, "id": "8906d273", "metadata": {}, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t.findDealByPriceAndFig" ] }, { "cell_type": "code", "execution_count": 10, "id": "896d4dac", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'78228979-3daf-470a-9c2a-8db180c8c3b0'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexes/base.py:3791\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3790\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 3791\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3792\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n", "File \u001b[0;32mindex.pyx:152\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32mindex.pyx:181\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:7080\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:7088\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: '78228979-3daf-470a-9c2a-8db180c8c3b0'", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcloseDeal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m78228979-3daf-470a-9c2a-8db180c8c3b0\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m t\u001b[38;5;241m.\u001b[39mdeals\n", "Cell \u001b[0;32mIn[2], line 38\u001b[0m, in \u001b[0;36mDealManager.closeDeal\u001b[0;34m(self, uuid, amount)\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcloseDeal\u001b[39m(\u001b[38;5;28mself\u001b[39m,uuid,amount):\n\u001b[0;32m---> 38\u001b[0m desiredDeal\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdeals\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43muuid\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m desiredDeal\u001b[38;5;241m.\u001b[39mamount \u001b[38;5;241m-\u001b[39m amount \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdeals \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdeals\u001b[38;5;241m.\u001b[39mdrop(labels \u001b[38;5;241m=\u001b[39m [uuid],axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m)\n", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexing.py:1153\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 1150\u001b[0m axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maxis \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 1152\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[0;32m-> 1153\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmaybe_callable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexing.py:1393\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m 1391\u001b[0m \u001b[38;5;66;03m# fall thru to straight lookup\u001b[39;00m\n\u001b[1;32m 1392\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[0;32m-> 1393\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_label\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexing.py:1343\u001b[0m, in \u001b[0;36m_LocIndexer._get_label\u001b[0;34m(self, label, axis)\u001b[0m\n\u001b[1;32m 1341\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_label\u001b[39m(\u001b[38;5;28mself\u001b[39m, label, axis: AxisInt):\n\u001b[1;32m 1342\u001b[0m \u001b[38;5;66;03m# GH#5567 this will fail if the label is not present in the axis.\u001b[39;00m\n\u001b[0;32m-> 1343\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mobj\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mxs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlabel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/generic.py:4236\u001b[0m, in \u001b[0;36mNDFrame.xs\u001b[0;34m(self, key, axis, level, drop_level)\u001b[0m\n\u001b[1;32m 4234\u001b[0m new_index \u001b[38;5;241m=\u001b[39m index[loc]\n\u001b[1;32m 4235\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 4236\u001b[0m loc \u001b[38;5;241m=\u001b[39m \u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4238\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(loc, np\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[1;32m 4239\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m loc\u001b[38;5;241m.\u001b[39mdtype \u001b[38;5;241m==\u001b[39m np\u001b[38;5;241m.\u001b[39mbool_:\n", "File \u001b[0;32m~/projects/marketTrade/.venv/lib/python3.11/site-packages/pandas/core/indexes/base.py:3798\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 3793\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(casted_key, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[1;32m 3794\u001b[0m \u001b[38;5;28misinstance\u001b[39m(casted_key, abc\u001b[38;5;241m.\u001b[39mIterable)\n\u001b[1;32m 3795\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28many\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mslice\u001b[39m) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m casted_key)\n\u001b[1;32m 3796\u001b[0m ):\n\u001b[1;32m 3797\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InvalidIndexError(key)\n\u001b[0;32m-> 3798\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 3799\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[1;32m 3800\u001b[0m \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[1;32m 3801\u001b[0m \u001b[38;5;66;03m# InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[1;32m 3802\u001b[0m \u001b[38;5;66;03m# the TypeError.\u001b[39;00m\n\u001b[1;32m 3803\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n", "\u001b[0;31mKeyError\u001b[0m: '78228979-3daf-470a-9c2a-8db180c8c3b0'" ] } ], "source": [ "t.closeDeal('78228979-3daf-470a-9c2a-8db180c8c3b0',1)\n", "t.deals" ] }, { "cell_type": "code", "execution_count": 11, "id": "8fbd7460", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a38e6fa8-c160-481c-b666-78ec9fade50b'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t.deals.iloc[0].name" ] }, { "cell_type": "code", "execution_count": 12, "id": "83549b44", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=2\n", "a==None\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2d794cff", "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.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }