{"id":13494269,"url":"https://github.com/Fatal1ty/tinkoff-api","last_synced_at":"2025-03-28T13:32:50.465Z","repository":{"id":57475705,"uuid":"237682098","full_name":"Fatal1ty/tinkoff-api","owner":"Fatal1ty","description":"Python Tinkoff API client for asyncio and humans","archived":true,"fork":false,"pushed_at":"2023-04-16T13:15:19.000Z","size":102,"stargazers_count":67,"open_issues_count":1,"forks_count":12,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-09-17T14:56:59.173Z","etag":null,"topics":["asyncio","python","tinkoff","tinkoff-api","tinkoff-invest","tinkoff-sdk"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/tinkoff-api/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Fatal1ty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"custom":["https://coindrop.to/tikhonov_a"]}},"created_at":"2020-02-01T21:43:54.000Z","updated_at":"2024-03-20T20:30:03.000Z","dependencies_parsed_at":"2024-01-16T09:52:05.198Z","dependency_job_id":"24fbbd2d-5c02-4f3b-be99-4b6f8e60be38","html_url":"https://github.com/Fatal1ty/tinkoff-api","commit_stats":{"total_commits":149,"total_committers":2,"mean_commits":74.5,"dds":"0.020134228187919434","last_synced_commit":"51d495af6c0c2116956153900d7922c3160cf453"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fatal1ty%2Ftinkoff-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fatal1ty%2Ftinkoff-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fatal1ty%2Ftinkoff-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fatal1ty%2Ftinkoff-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fatal1ty","download_url":"https://codeload.github.com/Fatal1ty/tinkoff-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222382544,"owners_count":16975383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","python","tinkoff","tinkoff-api","tinkoff-invest","tinkoff-sdk"],"created_at":"2024-07-31T19:01:23.344Z","updated_at":"2024-10-31T08:32:10.389Z","avatar_url":"https://github.com/Fatal1ty.png","language":"Python","readme":"# tinkoff-api\n\n\u003e Python Tinkoff API client for asyncio and humans.\n\n[![build](https://github.com/Fatal1ty/tinkoff-api/workflows/build/badge.svg)](https://github.com/Fatal1ty/tinkoff-api/actions?query=workflow%3Abuild)\n[![Latest Version](https://img.shields.io/pypi/v/tinkoff-api.svg)](https://pypi.python.org/pypi/tinkoff-api)\n[![Python Version](https://img.shields.io/pypi/pyversions/tinkoff-api.svg)](https://pypi.python.org/pypi/tinkoff-api)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n\nTable of contens\n--------------------------------------------------------------------------------\n* [Covered APIs](#covered-apis)\n* [Features](#features)\n* [Installation](#installation)\n* [Usage examples](#usage-examples)\n    * [REST API client](#rest-api-client)\n    * [Streaming client](#streaming-client)\n    * [Dynamic subscriptions in runtime](#dynamic-subscriptions-in-runtime)\n    * [Complete simple bot](#complete-simple-bot)\n    * [Historical data](#historical-data)\n* [TODO](#todo)\n\n\nCovered APIs\n--------------------------------------------------------------------------------\n* Tinkoff Investments ([official docs](https://tinkoffcreditsystems.github.io/invest-openapi/))\n\n\nFeatures\n--------------------------------------------------------------------------------\n* Clients for both [REST](https://tinkoffcreditsystems.github.io/invest-openapi/rest/) and [Streaming](https://tinkoffcreditsystems.github.io/invest-openapi/marketdata/) protocols in Tinkoff Investments\n* Presence of data classes for all interaction with API\n* Automatic reconnection and keep-alive connections\n* Internal exclusive rate limiter for every resource in REST protocol\n* Friendly exceptions for API errors\n\n\nInstallation\n--------------------------------------------------------------------------------\n\nUse pip to install:\n```shell\n$ pip install tinkoff-api\n```\n\nUsage examples\n--------------------------------------------------------------------------------\n\n#### REST API client:\n```python\nimport asyncio\nfrom datetime import datetime\nfrom tinkoff.investments import (\n    TinkoffInvestmentsRESTClient,\n    Environment,\n    CandleResolution,\n)\nfrom tinkoff.investments.client.exceptions import TinkoffInvestmentsError\n\n\nasync def show_apple_year_candles():\n    try:\n        async with TinkoffInvestmentsRESTClient(\n            token=\"TOKEN\", environment=Environment.SANDBOX\n        ) as client:\n\n            candles = await client.market.candles.get(\n                figi=\"BBG000B9XRY4\",\n                dt_from=datetime(2019, 1, 1),\n                dt_to=datetime(2019, 12, 31),\n                interval=CandleResolution.DAY,\n            )\n            for candle in candles:\n                print(f\"{candle.time}: {candle.h}\")\n    except TinkoffInvestmentsError as e:\n        print(e)\n\n\nasync def jackpot():\n    try:\n        async with TinkoffInvestmentsRESTClient(\n            token=\"TOKEN\", environment=Environment.SANDBOX\n        ) as client:\n\n            instruments = await client.market.instruments.search(\"AAPL\")\n            apple = instruments[0]\n\n            account = await client.sandbox.accounts.register()\n            await client.sandbox.accounts.positions.set_balance(\n                figi=apple.figi,\n                balance=100,\n                broker_account_id=account.brokerAccountId,\n            )\n\n            print(\"We created the following portfolio:\")\n            positions = await client.portfolio.get_positions()\n            for position in positions:\n                print(f\"{position.name}: {position.lots} lots\")\n    except TinkoffInvestmentsError as e:\n        print(e)\n\n\nasyncio.run(jackpot())\n```\n\n#### Streaming Client:\n```python\nimport asyncio\nfrom datetime import datetime\nfrom tinkoff.investments import (\n    CandleEvent,\n    CandleResolution,\n    TinkoffInvestmentsStreamingClient,\n)\n\nclient = TinkoffInvestmentsStreamingClient(token=\"TOKEN\")\n\n\n@client.events.candles(\"BBG009S39JX6\", CandleResolution.MIN_1)\n@client.events.candles(\"BBG000B9XRY4\", CandleResolution.MIN_1)\nasync def on_candle(candle: CandleEvent, server_time: datetime):\n    print(candle, server_time)\n\n\nasyncio.run(client.run())\n```\n\n#### Dynamic subscriptions in runtime:\n```python\nimport asyncio\nfrom datetime import datetime\nfrom tinkoff.investments import (\n    CandleEvent,\n    CandleResolution,\n    TinkoffInvestmentsStreamingClient,\n)\n\nclient = TinkoffInvestmentsStreamingClient(token=\"TOKEN\")\n\n\n@client.events.candles(\"BBG000B9XRY4\", CandleResolution.HOUR)\nasync def on_candle(candle: CandleEvent, server_time: datetime):\n    if candle.h \u003e 1000:\n        await client.events.candles.subscribe(\n            callback=on_candle,\n            figi=candle.figi,\n            interval=CandleResolution.MIN_1,\n        )\n    elif candle.h \u003c 1000:\n        await client.events.candles.unsubscribe(\n            candle.figi, CandleResolution.MIN_1\n        )\n\n\nasyncio.run(client.run())\n```\n\n#### Complete simple bot:\n```python\nimport asyncio\nfrom datetime import datetime\n\nfrom tinkoff.investments import (\n    CandleEvent,\n    CandleResolution,\n    OperationType,\n    TinkoffInvestmentsRESTClient,\n    TinkoffInvestmentsStreamingClient,\n)\n\nstreaming = TinkoffInvestmentsStreamingClient(\"TOKEN\")\nrest = TinkoffInvestmentsRESTClient(\"TOKEN\")\n\n\n@streaming.events.candles(\"BBG000B9XRY4\", CandleResolution.MIN_1)\nasync def buy_apple(candle: CandleEvent, server_time: datetime):\n    if candle.c \u003e 350:\n        await rest.orders.create_market_order(\n            figi=\"BBG000B9XRY4\",\n            lots=1,\n            operation=OperationType.BUY,\n            broker_account_id=123,\n        )\n\n\nasyncio.run(streaming.run())\n```\n\n#### Historical data:\n```python\nimport asyncio\nfrom datetime import datetime\n\nfrom tinkoff.investments import (\n    CandleResolution,\n    Environment,\n    TinkoffInvestmentsRESTClient,\n)\nfrom tinkoff.investments.utils.historical_data import HistoricalData\n\n\nasync def get_minute_candles():\n    # show 1 minute candles for AAPL in 1 year period of time\n    async with TinkoffInvestmentsRESTClient(\n        token=\"TOKEN\", environment=Environment.SANDBOX\n    ) as client:\n        historical_data = HistoricalData(client)\n        async for candle in historical_data.iter_candles(\n            figi=\"BBG000B9XRY4\",\n            dt_from=datetime(2019, 1, 1),\n            dt_to=datetime(2020, 1, 1),\n            interval=CandleResolution.MIN_1,\n        ):\n            print(candle)\n\n\nasyncio.run(get_minute_candles())\n```\n\nTODO\n--------------------------------------------------------------------------------\n\n* allow to provide str constants along with specific enum objects\n* add ability to unsubscribe by pattern\n* rename some fields\n* make some fields in snake case\n* generate documentation\n","funding_links":["https://coindrop.to/tikhonov_a"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFatal1ty%2Ftinkoff-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFatal1ty%2Ftinkoff-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFatal1ty%2Ftinkoff-api/lists"}