Skip to content

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

Open
zig323 opened this issue May 6, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@zig323
Copy link

zig323 commented May 6, 2023

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

Метод 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
@zig323 zig323 added the bug Something isn't working label May 6, 2023
@AlexanderVolkovTCS
Copy link
Contributor

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants