Tinkoff / invest-python

Tinkoff Invest Python gRPC client
https://tinkoff.github.io/invest-python/
Apache License 2.0
301 stars 87 forks source link

Метод get_candles() сервиса MarketDataService не принимает CandleInterval.CANDLE_INTERVAL_WEEK #230

Open zig323 opened 1 year ago

zig323 commented 1 year ago

Что случилось?

Метод get_candles() сервиса MarketDataService не принимает (в качестве аргумента interval) такие интервалы:

CandleInterval.CANDLE_INTERVAL_WEEK
CandleInterval.CANDLE_INTERVAL_MONTH

При этом

CandleInterval.CANDLE_INTERVAL_HOUR
CandleInterval.CANDLE_INTERVAL_DAY 

работают нормально. другие интервалы не проверял.

Помогите понять почему не работают недельные/месячные свечи.

Воспроизведение

import numpy as np
import pandas as pd
import tinkoff.invest as ti
from datetime import datetime
from datetime import timedelta
from datetime import timezone

def get_prices(token, figi, period, start_date, end_date=datetime.now(tz=timezone.utc)):
    if period == 'HOUR':
        delta = 7
    if period == 'DAY':
        delta = 365
    if period == 'WEEK':
        delta = 730
    if period == 'MONTH':
        delta = 3650
    prices = pd.DataFrame(columns=['date', 'open', 'close', 'high', 'low', 'volume'])
    start = start_date
    while True:
        end = min((start + timedelta(days=delta)).date(), end_date.date())
        with ti.Client(token) as client:
            candles = client.market_data.get_candles(figi=figi,
                                                     from_=start,
                                                     to=datetime(end.year, end.month, end.day),
                                                     interval=getattr(ti.CandleInterval, 'CANDLE_INTERVAL_' + period)).candles
            for candle in candles:
                date = candle.time
                open_price = candle.open.units + candle.open.nano / 10**9
                close_price = candle.close.units + candle.close.nano / 10**9
                high_price = candle.high.units + candle.high.nano / 10**9
                low_price = candle.low.units + candle.low.nano / 10**9
                volume = candle.volume
                prices.loc[len(prices)] = [date, open_price, close_price, high_price, low_price, volume]
        start += timedelta(days=delta+1)
        if end == end_date.date():
            break
    return prices

token = 'XXX'
figi = 'BBG004730RP0'
period = 'DAY'
start_date = datetime(2022, 1, 1)
prices = get_prices(token, figi, period, start_date)

Tinkoff Invest Version

0.2.0-beta54

Python Version

3.8

OS

Windows

Логи

~\anaconda3\envs\tinkoff_trading_bot\tools.py in get_prices(token, figi, period, start_date, end_date)
    128                                                      from_=start,
    129                                                      to=datetime(end.year, end.month, end.day),
--> 130                                                      interval=getattr(ti.CandleInterval, 'CANDLE_INTERVAL_' + period)).candles
    131             for candle in candles:
    132                 date = candle.time

~\anaconda3\lib\enum.py in __getattr__(cls, name)
    339             return cls._member_map_[name]
    340         except KeyError:
--> 341             raise AttributeError(name) from None
    342 
    343     def __getitem__(cls, name):

AttributeError: CANDLE_INTERVAL_MONTH
AlexanderVolkovTCS commented 12 months ago

@irusland посмотри пожалуйста. В API в GetCandles() такие интервалы поддерживаются.