Skip to main content

Python Tinkoff API client for asyncio and humans

Project description

tinkoff-api

Python Tinkoff API client for asyncio and humans.

Latest Version Python Version License

In active development.

Table of contens

Installation

Use pip to install:

$ pip install tinkoff-api

Usage examples

REST API client:

import asyncio
from datetime import datetime
from tinkoff.investments import (
    TinkoffInvestmentsRESTClient, Environment,CandleResolution
)
from tinkoff.investments.client.exceptions import TinkoffInvestmentsError

async def show_apple_year_candles():
    try:
        async with TinkoffInvestmentsRESTClient(
                token='TOKEN',
                environment=Environment.SANDBOX) as client:

            candles = await client.market.candles.get(
                figi='BBG000B9XRY4',
                dt_from=datetime(2019, 1, 1),
                dt_to=datetime(2019, 12, 31),
                interval=CandleResolution.DAY
            )
            for candle in candles:
                print(f'{candle.time}: {candle.h}')
    except TinkoffInvestmentsError as e:
        print(e)

async def jackpot():
    try:
        async with TinkoffInvestmentsRESTClient(
                token='TOKEN',
                environment=Environment.SANDBOX) as client:

            instruments = await client.market.instruments.search(ticker='AAPL')
            apple = instruments[0]

            account = await client.sandbox.accounts.register()
            await client.sandbox.accounts.positions.set_balance(
                broker_account_id=account.brokerAccountId,
                figi=apple.figi,
                balance=100,
            )

            print('We created the following portfolio:')
            positions = await client.portfolio.get_positions()
            for position in positions:
                print(f'{position.name}: {position.lots} lots')
    except TinkoffInvestmentsError as e:
        print(e)

asyncio.run(jackpot())

Streaming Client:

import asyncio
from tinkoff.investments import (
    TinkoffInvestmentsStreamingClient, CandleEvent, CandleResolution
)

client = TinkoffInvestmentsStreamingClient(token='TOKEN')

@client.events.candles('BBG009S39JX6', CandleResolution.MIN_1)
@client.events.candles('BBG000B9XRY4', CandleResolution.MIN_1)
async def on_candle(candle: CandleEvent):
    print(candle)

asyncio.run(client.run())

Dynamic subscriptions in runtime:

import asyncio
from tinkoff.investments import (
    TinkoffInvestmentsStreamingClient, CandleEvent, CandleResolution
)

client = TinkoffInvestmentsStreamingClient(token='TOKEN')

@client.events.candles('BBG000B9XRY4', CandleResolution.HOUR)
async def on_candle(candle: CandleEvent):
    if candle.h > 1000:
        await client.events.candles.subscribe(
            callback=on_candle,
            figi=candle.figi,
            interval=CandleResolution.MIN_1
        )
    elif candle.h < 1000:
        await client.events.candles.unsubscribe(
            candle.figi, CandleResolution.MIN_1
        )

asyncio.run(client.run())

Complete simple bot:

import asyncio
from tinkoff.investments import (
    TinkoffInvestmentsStreamingClient, TinkoffInvestmentsRESTClient,
    CandleEvent, CandleResolution, OperationType
)

streaming = TinkoffInvestmentsStreamingClient('TOKEN')
rest = TinkoffInvestmentsRESTClient('TOKEN')

@streaming.events.candles('BBG000B9XRY4', CandleResolution.MIN_1)
async def buy_apple(candle: CandleEvent):
    if candle.c > 350:
        await rest.orders.create_market_order(
            figi='BBG000B9XRY4',
            lots=1,
            operation=OperationType.BUY,
            broker_account_id=123,
        )

asyncio.run(streaming.run())

TODO

  • allow to provide str constants along with specific enum objects
  • add request limiter
  • add ability to unsubscribe by pattern
  • rename some fields
  • make some fields in snake case
  • generate documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinkoff-api-0.4.tar.gz (13.3 kB view details)

Uploaded Source

File details

Details for the file tinkoff-api-0.4.tar.gz.

File metadata

  • Download URL: tinkoff-api-0.4.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.4

File hashes

Hashes for tinkoff-api-0.4.tar.gz
Algorithm Hash digest
SHA256 696e309410f87f3d34a484dcbc75090f79fddf24b16837c6e7dd4308d8044eeb
MD5 62a2342fbcb5186770c3d4388d574a22
BLAKE2b-256 6c465bd228bbafdf12cdd36013c7830484ec18686669c62ca51f8cb398d361b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page