Files
Polymarket/order_entry.ipynb

689 lines
26 KiB
Plaintext
Raw Normal View History

2026-03-27 12:40:49 -04:00
{
"cells": [
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 2,
2026-03-27 12:40:49 -04:00
"id": "c0bfb3b5",
"metadata": {},
"outputs": [],
"source": [
"import modules.api as api\n",
"import pandas as pd\n",
"from datetime import datetime, timezone, timedelta\n",
"import math\n",
"import requests\n",
"import json\n",
"from dataclasses import dataclass\n",
"\n",
"from py_clob_client.clob_types import OrderArgs, OrderType, PostOrdersArgs, PartialCreateOrderOptions\n",
"from py_clob_client.order_builder.constants import BUY, SELL\n"
]
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 3,
2026-03-27 12:40:49 -04:00
"id": "7d7dc787",
"metadata": {},
"outputs": [],
"source": [
"def time_round_down(dt, interval_mins=5) -> int: # returns timestamp in seconds\n",
" interval_secs = interval_mins * 60\n",
" seconds = dt.timestamp()\n",
" rounded_seconds = math.floor(seconds / interval_secs) * interval_secs\n",
" \n",
" return rounded_seconds\n",
"\n",
"def get_mkt_details_by_slug(slug: str) -> dict[str, str, str]: # {'Up' : 123, 'Down': 456, 'isActive': True, 'MinTickSize': 0.01, 'isNegRisk': False}\n",
" r = requests.get(f\"https://gamma-api.polymarket.com/events/slug/{slug}\")\n",
" market = r.json()['markets'][0]\n",
" token_ids = json.loads(market.get(\"clobTokenIds\", \"[]\"))\n",
" outcomes = json.loads(market.get(\"outcomes\", \"[]\"))\n",
" d = dict(zip(outcomes, token_ids))\n",
" d['isActive'] = market['negRisk']\n",
" d['MinTickSize'] = market['orderPriceMinTickSize']\n",
" d['isNegRisk'] = market['negRisk']\n",
" d['ConditionId'] = market['conditionId']\n",
"\n",
" return d, market"
]
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 4,
2026-03-27 12:40:49 -04:00
"id": "c3e07e21",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-03-29 16:27:58 +00:00
"Timestamp('2026-03-29 05:05:00')"
2026-03-27 12:40:49 -04:00
]
},
2026-03-29 16:27:58 +00:00
"execution_count": 4,
2026-03-27 12:40:49 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"slug_prefix = 'btc-updown-5m-'\n",
"slug_time_id = time_round_down(dt=datetime.now(timezone.utc))\n",
"slug_full = slug_prefix + str(slug_time_id)\n",
"market_details, market = get_mkt_details_by_slug(slug_full)\n",
"pd.to_datetime(slug_time_id, unit='s')"
]
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 5,
2026-03-27 12:40:49 -04:00
"id": "5ba43ffc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2026-03-29 16:27:58 +00:00
"{'Up': '100363012413042717231742069495969784533240629092182259086983062365921258452690',\n",
" 'Down': '33447700958945117801119737006959954665425600820162218508565813029801829841900',\n",
2026-03-27 12:40:49 -04:00
" 'isActive': False,\n",
" 'MinTickSize': 0.01,\n",
" 'isNegRisk': False,\n",
2026-03-29 16:27:58 +00:00
" 'ConditionId': '0x7e64102ae25311ab43ed27d0fe2c614dd9deefdd1324012b91f0814b530cb7db'}"
2026-03-27 12:40:49 -04:00
]
},
2026-03-29 16:27:58 +00:00
"execution_count": 5,
2026-03-27 12:40:49 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"market_details"
]
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 7,
2026-03-27 12:40:49 -04:00
"id": "5d356d3b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"creating client...\n",
2026-03-29 16:27:58 +00:00
"You've made 44 trades\n",
2026-03-27 12:40:49 -04:00
"client created successfully!\n"
]
}
],
"source": [
"client = api.create_client()"
]
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 8,
2026-03-27 12:40:49 -04:00
"id": "bebb53eb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2026-03-29 16:27:58 +00:00
"{'price': '0.01', 'side': 'SELL'}\n"
2026-03-27 12:40:49 -04:00
]
}
],
"source": [
"last = client.get_last_trade_price(market_details['Up'])\n",
"print(last)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bae5e6a9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
2026-03-29 16:27:58 +00:00
"execution_count": 9,
2026-03-27 12:40:49 -04:00
"id": "52c0c38a",
"metadata": {},
"outputs": [
{
2026-03-29 16:27:58 +00:00
"ename": "PolyApiException",
"evalue": "PolyApiException[status_code=403, error_message={'error': 'Trading restricted in your region, please refer to available regions - https://docs.polymarket.com/developers/CLOB/geoblock'}]",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mPolyApiException\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m response = \u001b[43mclient\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpost_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\n\u001b[32m 2\u001b[39m \u001b[43m \u001b[49m\u001b[43mPostOrdersArgs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 3\u001b[39m \u001b[43m \u001b[49m\u001b[43morder\u001b[49m\u001b[43m=\u001b[49m\u001b[43mclient\u001b[49m\u001b[43m.\u001b[49m\u001b[43mcreate_order\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[43m \u001b[49m\u001b[43morder_args\u001b[49m\u001b[43m=\u001b[49m\u001b[43mOrderArgs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5\u001b[39m \u001b[43m \u001b[49m\u001b[43mtoken_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmarket_details\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mUp\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 6\u001b[39m \u001b[43m \u001b[49m\u001b[43mprice\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m0.20\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 7\u001b[39m \u001b[43m \u001b[49m\u001b[43msize\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m10\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[43m \u001b[49m\u001b[43mside\u001b[49m\u001b[43m=\u001b[49m\u001b[43mBUY\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 9\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 10\u001b[39m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[43m=\u001b[49m\u001b[43mPartialCreateOrderOptions\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 11\u001b[39m \u001b[43m \u001b[49m\u001b[43mtick_size\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmarket_details\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mMinTickSize\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 12\u001b[39m \u001b[43m \u001b[49m\u001b[43mneg_risk\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmarket_details\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43misNegRisk\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[32m 13\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 14\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 15\u001b[39m \u001b[43m \u001b[49m\u001b[43morderType\u001b[49m\u001b[43m=\u001b[49m\u001b[43mOrderType\u001b[49m\u001b[43m.\u001b[49m\u001b[43mGTC\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 16\u001b[39m \u001b[43m \u001b[49m\u001b[43mpostOnly\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 17\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 18\u001b[39m \u001b[43m \u001b[49m\u001b[43mPostOrdersArgs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 19\u001b[39m \u001b[43m \u001b[49m\u001b[43morder\u001b[49m\u001b[43m=\u001b[49m\u001b[43mclient\u001b[49m\u001b[43m.\u001b[49m\u001b[43mcreate_order\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 20\u001b[39m \u001b[43m \u001b[49m\u001b[43morder_args\u001b[49m\u001b[43m=\u001b[49m\u001b[43mOrderArgs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 21\u001b[39m \u001b[43m \u001b[49m\u001b[43mtoken_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmarket_details\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mDown\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 22\u001b[39m \u001b[43m \u001b[49m\u001b[43mprice\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m0.10\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/py_clob_client/client.py:617\u001b[39m, in \u001b[36mClobClient.post_orders\u001b[39m\u001b[34m(self, args)\u001b[39m\n\u001b[32m 611\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m post(\n\u001b[32m 612\u001b[39m \u001b[33m\"\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[33m\"\u001b[39m.format(\u001b[38;5;28mself\u001b[39m.host, POST_ORDERS),\n\u001b[32m 613\u001b[39m headers=builder_headers,\n\u001b[32m 614\u001b[39m data=request_args.serialized_body,\n\u001b[32m 615\u001b[39m )\n\u001b[32m 616\u001b[39m \u001b[38;5;66;03m# send exact serialized bytes\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m617\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mpost\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 618\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[38;5;132;43;01m{}\u001b[39;49;00m\u001b[38;5;132;43;01m{}\u001b[39;49;00m\u001b[33;43m\"\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mformat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mhost\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mPOST_ORDERS\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 619\u001b[39m \u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m=\u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 620\u001b[39m \u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrequest_args\u001b[49m\u001b[43m.\u001b[49m\u001b[43mserialized_body\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 621\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/py_clob_client/http_helpers/helpers.py:69\u001b[39m, in \u001b[36mpost\u001b[39m\u001b[34m(endpoint, headers, data)\u001b[39m\n\u001b[32m 68\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mpost\u001b[39m(endpoint, headers=\u001b[38;5;28;01mNone\u001b[39;00m, data=\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[32m---> \u001b[39m\u001b[32m69\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mendpoint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mPOST\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/miniconda3/envs/py_313/lib/python3.13/site-packages/py_clob_client/http_helpers/helpers.py:57\u001b[39m, in \u001b[36mrequest\u001b[39m\u001b[34m(endpoint, method, headers, data)\u001b[39m\n\u001b[32m 49\u001b[39m resp = _http_client.request(\n\u001b[32m 50\u001b[39m method=method,\n\u001b[32m 51\u001b[39m url=endpoint,\n\u001b[32m 52\u001b[39m headers=headers,\n\u001b[32m 53\u001b[39m json=data,\n\u001b[32m 54\u001b[39m )\n\u001b[32m 56\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m resp.status_code != \u001b[32m200\u001b[39m:\n\u001b[32m---> \u001b[39m\u001b[32m57\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m PolyApiException(resp)\n\u001b[32m 59\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m 60\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m resp.json()\n",
"\u001b[31mPolyApiException\u001b[39m: PolyApiException[status_code=403, error_message={'error': 'Trading restricted in your region, please refer to available regions - https://docs.polymarket.com/developers/CLOB/geoblock'}]"
2026-03-27 12:40:49 -04:00
]
}
],
"source": [
"response = client.post_orders([\n",
" PostOrdersArgs(\n",
" order=client.create_order(\n",
" order_args=OrderArgs(\n",
" token_id=market_details['Up'],\n",
2026-03-29 16:27:58 +00:00
" price=0.20,\n",
2026-03-27 12:40:49 -04:00
" size=10,\n",
" side=BUY,\n",
" ),\n",
" options=PartialCreateOrderOptions(\n",
" tick_size=str(market_details['MinTickSize']),\n",
" neg_risk=market_details['isNegRisk']\n",
" ),\n",
" ),\n",
" orderType=OrderType.GTC,\n",
" postOnly=False,\n",
" ),\n",
" PostOrdersArgs(\n",
" order=client.create_order(\n",
" order_args=OrderArgs(\n",
" token_id=market_details['Down'],\n",
" price=0.10,\n",
" size=10,\n",
" side=BUY,\n",
" ),\n",
" options=PartialCreateOrderOptions(\n",
" tick_size=str(market_details['MinTickSize']),\n",
" neg_risk=market_details['isNegRisk']\n",
" ),\n",
" ),\n",
" orderType=OrderType.GTC,\n",
" postOnly=True,\n",
" ),\n",
"])\n",
"\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52a7229b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb5f066a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b3ccf83",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 68,
"id": "75d45cd8",
"metadata": {},
"outputs": [],
"source": [
"d = [{'asset_id': '97987758532314346863331680319607711694838937984814950023901315671390566048932', 'price': '0.37', 'size': '429.41', 'side': 'BUY', 'hash': '999d5b73d83a840c0df7dc2d817ae55a242845d5', 'best_bid': '0.37', 'best_ask': '0.38'}, {'asset_id': '92959981857766705879127008770062050214089835506649207585188324269480756219695', 'price': '0.63', 'size': '429.41', 'side': 'SELL', 'hash': 'ead5af5a55f8b125b1e50f1c80a783c3c2d33187', 'best_bid': '0.62', 'best_ask': '0.63'}]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "f608378f",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "asset_id",
"rawType": "object",
"type": "string"
},
{
"name": "price",
"rawType": "object",
"type": "string"
},
{
"name": "size",
"rawType": "object",
"type": "string"
},
{
"name": "side",
"rawType": "object",
"type": "string"
},
{
"name": "hash",
"rawType": "object",
"type": "string"
},
{
"name": "best_bid",
"rawType": "object",
"type": "string"
},
{
"name": "best_ask",
"rawType": "object",
"type": "string"
}
],
"ref": "310c4763-20db-4051-931a-ef52e1b6513b",
"rows": [
[
"0",
"97987758532314346863331680319607711694838937984814950023901315671390566048932",
"0.37",
"429.41",
"BUY",
"999d5b73d83a840c0df7dc2d817ae55a242845d5",
"0.37",
"0.38"
],
[
"1",
"92959981857766705879127008770062050214089835506649207585188324269480756219695",
"0.63",
"429.41",
"SELL",
"ead5af5a55f8b125b1e50f1c80a783c3c2d33187",
"0.62",
"0.63"
]
],
"shape": {
"columns": 7,
"rows": 2
}
},
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>asset_id</th>\n",
" <th>price</th>\n",
" <th>size</th>\n",
" <th>side</th>\n",
" <th>hash</th>\n",
" <th>best_bid</th>\n",
" <th>best_ask</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>9798775853231434686333168031960771169483893798...</td>\n",
" <td>0.37</td>\n",
" <td>429.41</td>\n",
" <td>BUY</td>\n",
" <td>999d5b73d83a840c0df7dc2d817ae55a242845d5</td>\n",
" <td>0.37</td>\n",
" <td>0.38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>9295998185776670587912700877006205021408983550...</td>\n",
" <td>0.63</td>\n",
" <td>429.41</td>\n",
" <td>SELL</td>\n",
" <td>ead5af5a55f8b125b1e50f1c80a783c3c2d33187</td>\n",
" <td>0.62</td>\n",
" <td>0.63</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" asset_id price size side \\\n",
"0 9798775853231434686333168031960771169483893798... 0.37 429.41 BUY \n",
"1 9295998185776670587912700877006205021408983550... 0.63 429.41 SELL \n",
"\n",
" hash best_bid best_ask \n",
"0 999d5b73d83a840c0df7dc2d817ae55a242845d5 0.37 0.38 \n",
"1 ead5af5a55f8b125b1e50f1c80a783c3c2d33187 0.62 0.63 "
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(d)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fe32a82",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7603be6c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 75,
"id": "c099da9a",
"metadata": {},
"outputs": [],
"source": [
"start = 1774585800\n",
"end = 1774585800 + 60*5"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b631306d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-03-27 04:35:00')"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(start, unit='s')"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "685d7dd9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"300"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"end - start"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "bdd681cc",
"metadata": {},
"outputs": [],
"source": [
"def format_timestamp(total_seconds):\n",
" minutes, seconds = divmod(total_seconds, 60)\n",
" print(f\"{minutes} minutes and {seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "7ba83cea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 minutes and 0 seconds\n"
]
}
],
"source": [
"format_timestamp(end - start)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "4e211e0d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1774586619"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(datetime.now().timestamp())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dabe016",
"metadata": {},
"outputs": [],
"source": [
"1774585800"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "8a65ba6f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-03-27 03:20:00')"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(1774581600, unit='s')"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "83f63c10",
"metadata": {},
"outputs": [],
"source": [
"sdt = '2026-03-27T03:20:00Z'"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "1f907a71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1774581600"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(datetime.strptime(sdt, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=timezone.utc).timestamp())"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "fe7c0c12",
"metadata": {},
"outputs": [],
"source": [
"d = {\"connection_id\":\"a4-i0ecArPECFLQ=\",\"payload\":{\"full_accuracy_value\":\"65779051825748830000000\",\"symbol\":\"btc/usd\",\"timestamp\":1774627572000,\"value\":65779.05182574883},\"timestamp\":1774627573524,\"topic\":\"crypto_prices_chainlink\",\"type\":\"update\"}"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "a98ace05",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'connection_id': 'a4-i0ecArPECFLQ=',\n",
" 'payload': {'full_accuracy_value': '65779051825748830000000',\n",
" 'symbol': 'btc/usd',\n",
" 'timestamp': 1774627572000,\n",
" 'value': 65779.05182574883},\n",
" 'timestamp': 1774627573524,\n",
" 'topic': 'crypto_prices_chainlink',\n",
" 'type': 'update'}"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "d2278853",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timestamp('2026-03-27 16:06:13.524000')"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.to_datetime(d['timestamp'], unit='ms')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "008cb5c9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6c647b7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1b8d116",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f1e3a83",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
2026-03-29 16:27:58 +00:00
"display_name": "py_313",
2026-03-27 12:40:49 -04:00
"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",
2026-03-29 16:27:58 +00:00
"version": "3.13.12"
2026-03-27 12:40:49 -04:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}