Skip to content

Instantly share code, notes, and snippets.

@ShabbirHasan1
Last active January 27, 2023 16:41
Show Gist options
  • Save ShabbirHasan1/f34137cc75cd7d8334b95193e409e234 to your computer and use it in GitHub Desktop.
Save ShabbirHasan1/f34137cc75cd7d8334b95193e409e234 to your computer and use it in GitHub Desktop.
serialize_&_deserialize_ticker_full.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ShabbirHasan1/f34137cc75cd7d8334b95193e409e234/serialize_-_deserialize_ticker_full.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import json\n",
"import requests\n",
"from pprint import pprint"
],
"metadata": {
"id": "hJHs2MP6e5ie"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"dataclass_url = \"https://raw.githubusercontent.com/ShabbirHasan1/kiteconnect-data-structures/main/JsonSchemaAndStructs/KiteConnect-Python-DataClass/ticker_full.py\"\n",
"ticker_full_dataclass = requests.get(dataclass_url).text\n",
"# For making it compatible to run on python version < 3.10 do this else ignore this.\n",
"ticker_full_dataclass = ticker_full_dataclass.replace(\"(slots=True)\",\"\")"
],
"metadata": {
"id": "EHeSDC5smsfc"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"exec(ticker_full_dataclass)"
],
"metadata": {
"id": "g8ZQ2MVFmsi1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"ticker_full_json_url = \"https://raw.githubusercontent.com/zerodha/kiteconnect-mocks/main/ticker_full.json\"\n",
"ticker_full_json = requests.get(ticker_full_json_url).json()"
],
"metadata": {
"id": "fZymCaOgnxg5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"deserialized_ticker_full = ticker_full_from_dict(ticker_full_json)"
],
"metadata": {
"id": "J_5OmJaWYnjn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"deserialized_ticker_full"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kHYyEL6aeXSe",
"outputId": "1e04116d-3e39-497c-a0a0-aa67219ca251"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"TickerFull(average_price=1570.37, buy_quantity=256443, change=5.900000000000091, depth=Depth(buy=[Buy(orders=1, price=1573.4, quantity=5), Buy(orders=2, price=1573.0, quantity=140), Buy(orders=1, price=1572.95, quantity=2), Buy(orders=7, price=1572.9, quantity=219), Buy(orders=1, price=1572.85, quantity=50)], sell=[Buy(orders=3, price=1573.7, quantity=172), Buy(orders=3, price=1573.75, quantity=44), Buy(orders=3, price=1573.85, quantity=302), Buy(orders=2, price=1573.9, quantity=141), Buy(orders=5, price=1573.95, quantity=724)]), instrument_token=408065, last_price=1573.7, last_quantity=7, last_trade_time=datetime.datetime(2021, 7, 5, 10, 41, 27, tzinfo=tzoffset(None, 19800)), mode='full', ohlc=Ohlc(close=1567.8, high=1575, low=1561.05, open=1569.15), oi=0, oi_day_high=0, oi_day_low=0, sell_quantity=363009, timestamp=datetime.datetime(2021, 7, 5, 10, 41, 27, tzinfo=tzoffset(None, 19800)), tradable=True, volume=1192471)"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"source": [
"serialized_ticker_full = ticker_full_to_dict(deserialized_ticker_full)"
],
"metadata": {
"id": "eVhMBj69eq-n"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pprint(serialized_ticker_full)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vDcDQCYZebTr",
"outputId": "d7727d2e-7bb6-4637-c723-08b042a8a326"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'average_price': 1570.37,\n",
" 'buy_quantity': 256443,\n",
" 'change': 5.900000000000091,\n",
" 'depth': {'buy': [{'orders': 1, 'price': 1573.4, 'quantity': 5},\n",
" {'orders': 2, 'price': 1573.0, 'quantity': 140},\n",
" {'orders': 1, 'price': 1572.95, 'quantity': 2},\n",
" {'orders': 7, 'price': 1572.9, 'quantity': 219},\n",
" {'orders': 1, 'price': 1572.85, 'quantity': 50}],\n",
" 'sell': [{'orders': 3, 'price': 1573.7, 'quantity': 172},\n",
" {'orders': 3, 'price': 1573.75, 'quantity': 44},\n",
" {'orders': 3, 'price': 1573.85, 'quantity': 302},\n",
" {'orders': 2, 'price': 1573.9, 'quantity': 141},\n",
" {'orders': 5, 'price': 1573.95, 'quantity': 724}]},\n",
" 'instrument_token': 408065,\n",
" 'last_price': 1573.7,\n",
" 'last_quantity': 7,\n",
" 'last_trade_time': '2021-07-05T10:41:27+05:30',\n",
" 'mode': 'full',\n",
" 'ohlc': {'close': 1567.8, 'high': 1575, 'low': 1561.05, 'open': 1569.15},\n",
" 'oi': 0,\n",
" 'oi_day_high': 0,\n",
" 'oi_day_low': 0,\n",
" 'sell_quantity': 363009,\n",
" 'timestamp': '2021-07-05T10:41:27+05:30',\n",
" 'tradable': True,\n",
" 'volume': 1192471}\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment