Class: InvestTinkoff::V2::Client

Inherits:
ClientBase show all
Defined in:
lib/invest_tinkoff/v2/client.rb

Direct Known Subclasses

SandboxClient

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%dT%H:%m:%S.000Z'

Instance Method Summary collapse

Constructor Details

#initialize(token:, logger: nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/invest_tinkoff/v2/client.rb', line 8

def initialize token:, logger: nil
  super(
    token: token,
    logger: logger
  )
end

Instance Method Details

#accountsObject

Метод получения счетов пользователя. tinkoff.github.io/investAPI/users/#getaccounts



486
487
488
# File 'lib/invest_tinkoff/v2/client.rb', line 486

def accounts
  user_service_request '/GetAccounts'
end

#accrued_interests(figi:, from:, to:) ⇒ Object

Метод получения накопленного купонного дохода по облигации. tinkoff.github.io/investAPI/instruments/#getaccruedinterests

@figi: String, пример: ‘BBG00X6ZGSY7’ @from: Time, пример: 1.year.ago @to: Time, пример: 2.months.from_now



123
124
125
126
127
128
129
130
# File 'lib/invest_tinkoff/v2/client.rb', line 123

def accrued_interests figi:, from:, to:
  body = {
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT)
  }
  instrument_request '/GetAccruedInterests', body
end

#asset_by(id:) ⇒ Object

Метод получения актива по его идентификатору. tinkoff.github.io/investAPI/instruments/#getassetby

@id: String, пример: ‘9f083982-cf4c-418a-a0bf-8b82f16db42d’



136
137
138
# File 'lib/invest_tinkoff/v2/client.rb', line 136

def asset_by id:
  instrument_request '/GetAssetBy', { id: id }
end

#assetsObject

Метод получения списка активов. tinkoff.github.io/investAPI/instruments/#getassets



142
143
144
# File 'lib/invest_tinkoff/v2/client.rb', line 142

def assets
  instrument_request '/GetAssets'
end

#bond_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения облигации по её идентификатору. tinkoff.github.io/investAPI/instruments/#bondby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.bond_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘BBG00T22WKV5’



27
28
29
30
# File 'lib/invest_tinkoff/v2/client.rb', line 27

def bond_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/BondBy', body
end

#bond_coupons(figi:, from:, to:) ⇒ Object

Метод получения графика выплат купонов по облигации. tinkoff.github.io/investAPI/instruments/#getbondcoupons

@figi: String, пример: ‘BBG00X6ZGSY7’ @from: Time, пример: 1.year.ago @to: Time, пример: 6.months.from_now



152
153
154
155
156
157
158
159
# File 'lib/invest_tinkoff/v2/client.rb', line 152

def bond_coupons figi:, from:, to:
  body = {
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT)
  }
  instrument_request '/GetBondCoupons', body
end

#bonds(instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE) ⇒ Object

Метод получения списка облигаций. tinkoff.github.io/investAPI/instruments/#bonds

@instrument_status: InvestTinkoff::V2::InstrumentStatus



36
37
38
# File 'lib/invest_tinkoff/v2/client.rb', line 36

def bonds instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE
  instrument_request '/Bonds', { instrumentStatus: instrument_status }
end

#broker_reportObject

Метод получения брокерского отчёта. tinkoff.github.io/investAPI/operations/#getbrokerreport

Raises:

  • (NotImplementedError)


306
307
308
309
# File 'lib/invest_tinkoff/v2/client.rb', line 306

def broker_report
  # operation_request '/GetBrokerReport', body
  raise NotImplementedError
end

#cancel_order(account_id:, order_id:) ⇒ Object

Метод отмены биржевой заявки. tinkoff.github.io/investAPI/orders/#cancelorder

@account_id: String @order_id: String



419
420
421
422
# File 'lib/invest_tinkoff/v2/client.rb', line 419

def cancel_order account_id:, order_id:
  body = { accountId: , orderId: order_id }
  order_request '/CancelOrder', body
end

#cancel_stop_order(account_id:, order_id:) ⇒ Object

Метод отмены стоп-заявки. tinkoff.github.io/investAPI/stoporders/#cancelstoporder

@account_id: String @order_id: String



475
476
477
478
# File 'lib/invest_tinkoff/v2/client.rb', line 475

def cancel_stop_order account_id:, order_id:
  body = { accountId: , stopOrderId: order_id }
  stop_order_request '/CancelStopOrder', body
end

#candles(figi:, from:, to:, interval: InvestTinkoff::V2::CandleInterval::HOUR) ⇒ Object

Метод запроса исторических свечей по инструменту. tinkoff.github.io/investAPI/marketdata/#getcandles

@figi: String, пример: ‘BBG000B9XRY4’ @from: Time, пример: 1.hour.ago @to: Time, пример: Time.zone.now @interval: InvestTinkoff::V2::CandleInterval



250
251
252
253
254
255
256
257
258
# File 'lib/invest_tinkoff/v2/client.rb', line 250

def candles figi:, from:, to:, interval: InvestTinkoff::V2::CandleInterval::HOUR
  body = {
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT),
    interval: interval
  }
  market_request '/GetCandles', body
end

#create_order(account_id:, figi:, quantity:, price:, direction:, order_type:, order_id:) ⇒ Object

Метод выставления заявки. tinkoff.github.io/investAPI/orders/#postorder

@account_id: String @figi: String @quantity: Integer @price: Float @direction: InvestTinkoff::V2::OrderDirection @order_type: InvestTinkoff::V2::OrderType @order_id: String (max length 36)



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/invest_tinkoff/v2/client.rb', line 383

def create_order( # rubocop: disable Metrics/ParameterLists
  account_id:,
  figi:,
  quantity:,
  price:,
  direction:,
  order_type:,
  order_id:
)
  body = {
    accountId: ,
    figi: figi,
    quantity: quantity,
    price: InvestTinkoff::V2::Quotation.create(price),
    direction: direction,
    orderType: order_type,
    orderId: order_id
  }
  order_request '/PostOrder', body
end

#create_stop_order(account_id:, figi:, quantity:, price:, stop_price:, expire_date:, expiration_type: InvestTinkoff::V2::StopOrderExpirationType::UNSPECIFIED, stop_order_type: InvestTinkoff::V2::StopOrderType::UNSPECIFIED) ⇒ Object

Метод выставления стоп-заявки. tinkoff.github.io/investAPI/stoporders/#poststoporder

@account_id: String @figi: String, пример: ‘BBG000B9XRY4’ @quantity: Integer @price: Float @stop_price: Float @expiration_type: InvestTinkoff::V2::StopOrderExpirationType @stop_order_type: InvestTinkoff::V2::StopOrderType



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/invest_tinkoff/v2/client.rb', line 447

def create_stop_order(
  account_id:,
  figi:,
  quantity:,
  price:,
  stop_price:,
  expire_date:,
  expiration_type: InvestTinkoff::V2::StopOrderExpirationType::UNSPECIFIED,
  stop_order_type: InvestTinkoff::V2::StopOrderType::UNSPECIFIED
)
  body = {
    accountId: ,
    figi: figi,
    quantity: quantity,
    price: InvestTinkoff::V2::Quotation.create(price),
    stopPrice: InvestTinkoff::V2::Quotation.create(stop_price),
    expirationType: (expiration_type || StopOrderExpirationType::UNSPECIFIED),
    stop_order_type: (stop_order_type || InvestTinkoff::V2::StopOrderType::UNSPECIFIED),
    expire_date: expire_date.strftime(TIME_FORMAT)
  }
  stop_order_request '/PostStopOrder', body
end

#currencies(instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE) ⇒ Object

Метод получения списка валют. tinkoff.github.io/investAPI/instruments/#currencies

@instrument_status: InvestTinkoff::V2::InstrumentStatus



44
45
46
# File 'lib/invest_tinkoff/v2/client.rb', line 44

def currencies instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE
  instrument_request '/Currencies', { instrumentStatus: instrument_status }
end

#currency_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения валюты по её идентификатору. tinkoff.github.io/investAPI/instruments/#currencyby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.currency_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘BBG0013HQ5F0’ client.currency_by id_type: InvestTinkoff::V2::InstrumentIdType::TICKER, id: ‘GBPRUB_TOM’, class_code: ‘CETS’



57
58
59
60
# File 'lib/invest_tinkoff/v2/client.rb', line 57

def currency_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/CurrencyBy', body
end

#dividends(figi:, from:, to:) ⇒ Object

Метод для получения событий выплаты дивидендов по инструменту. tinkoff.github.io/investAPI/instruments/#getdividends

@figi: String, пример: ‘BBG000B9XRY4’ @from: Time, пример: 1.year.ago @to: Time, пример: 1.month.from_now



167
168
169
170
171
172
173
174
# File 'lib/invest_tinkoff/v2/client.rb', line 167

def dividends figi:, from:, to:
  body = {
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT)
  }
  instrument_request '/GetDividends', body
end

#dividends_foreign_issuerObject

Метод получения отчёта “Справка о доходах за пределами РФ”. tinkoff.github.io/investAPI/operations/#getdividendsforeignissuer

Raises:

  • (NotImplementedError)


313
314
315
316
# File 'lib/invest_tinkoff/v2/client.rb', line 313

def dividends_foreign_issuer
  # operation_request '/GetDividendsForeignIssuer', body
  raise NotImplementedError
end

#edit_favorites(figis:, action_type: InvestTinkoff::V2::EditFavoritesActionType::ADD) ⇒ Object

Метод редактирования избранных инструментов. tinkoff.github.io/investAPI/instruments/#editfavorites

@figis: String, пример: [‘BBG000B9XRY4’, ‘BBG000BWXBC2’] @action_type: InvestTinkoff::V2::EditFavoritesActionType



67
68
69
70
71
72
73
# File 'lib/invest_tinkoff/v2/client.rb', line 67

def edit_favorites figis:, action_type: InvestTinkoff::V2::EditFavoritesActionType::ADD
  body = {
    instruments: figis.map { |v| { figi: v } },
    actionType: action_type
  }
  instrument_request '/EditFavorites', body
end

#etf_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения инвестиционного фонда по его идентификатору. tinkoff.github.io/investAPI/instruments/#etfby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.etf_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘BBG005DXDPK9’



83
84
85
86
# File 'lib/invest_tinkoff/v2/client.rb', line 83

def etf_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/EtfBy', body
end

#etfs(instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE) ⇒ Object

Метод получения списка инвестиционных фондов. tinkoff.github.io/investAPI/instruments/#etfs

@instrument_status: InvestTinkoff::V2::InstrumentStatus



92
93
94
# File 'lib/invest_tinkoff/v2/client.rb', line 92

def etfs instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE
  instrument_request '/Etfs', { instrumentStatus: instrument_status }
end

#favoritesObject

Метод получения избранных инструментов. tinkoff.github.io/investAPI/instruments/#getfavorites



178
179
180
# File 'lib/invest_tinkoff/v2/client.rb', line 178

def favorites
  instrument_request '/GetFavorites'
end

#future_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения фьючерса по его идентификатору. tinkoff.github.io/investAPI/instruments/#futureby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.future_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘FUTNL0422000’



104
105
106
107
# File 'lib/invest_tinkoff/v2/client.rb', line 104

def future_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/FutureBy', body
end

#futures(instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE) ⇒ Object

Метод получения списка фьючерсов. tinkoff.github.io/investAPI/instruments/#futures

@instrument_status: InvestTinkoff::V2::InstrumentStatus



113
114
115
# File 'lib/invest_tinkoff/v2/client.rb', line 113

def futures instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE
  instrument_request '/Futures', { instrumentStatus: instrument_status }
end

#futures_margin(figi:) ⇒ Object

Метод получения размера гарантийного обеспечения по фьючерсам. tinkoff.github.io/investAPI/instruments/#getfuturesmargin

@figi: String



186
187
188
# File 'lib/invest_tinkoff/v2/client.rb', line 186

def futures_margin figi:
  instrument_request '/GetFuturesMargin', { figi: figi }
end

#infoObject

Метод получения информации о пользователе. tinkoff.github.io/investAPI/users/#getinfo



492
493
494
# File 'lib/invest_tinkoff/v2/client.rb', line 492

def info
  user_service_request '/GetInfo'
end

#instrument_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения основной информации об инструменте. tinkoff.github.io/investAPI/instruments/#getinstrumentby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.instrument_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘BBG000B9XRY4’



198
199
200
201
# File 'lib/invest_tinkoff/v2/client.rb', line 198

def instrument_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/GetInstrumentBy', body
end

#last_prices(figis:) ⇒ Object

Метод запроса последних цен по инструментам. tinkoff.github.io/investAPI/marketdata/#getlastprices

@figis: String, пример: [‘BBG000B9XRY4’, ‘BBG000BWXBC2’]



264
265
266
# File 'lib/invest_tinkoff/v2/client.rb', line 264

def last_prices figis:
  market_request '/GetLastPrices', { figi: figis }
end

#last_trades(figi:, from:, to:) ⇒ Object

Метод запроса последних обезличенных сделок по инструменту. tinkoff.github.io/investAPI/marketdata/#getlasttrades

@figi: String, пример: ‘BBG000B9XRY4’ @from: Time, пример: 1.hour.ago @to: Time, пример: Time.zone.now



274
275
276
277
278
279
280
281
# File 'lib/invest_tinkoff/v2/client.rb', line 274

def last_trades figi:, from:, to:
  body = {
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT)
  }
  market_request '/GetLastTrades', body
end

#margin_attributes(account_id:) ⇒ Object

Расчёт маржинальных показателей по счёту. tinkoff.github.io/investAPI/users/#getmarginattributes

@account_id: String



500
501
502
503
# File 'lib/invest_tinkoff/v2/client.rb', line 500

def margin_attributes account_id:
  body = { accountId:  }
  user_service_request '/GetMarginAttributes', body
end

#operations(account_id:, figi:, from:, to:, state: InvestTinkoff::V2::OperationState::UNSPECIFIED) ⇒ Object

Метод получения списка операций по счёту. tinkoff.github.io/investAPI/operations/#getoperations

@account_id: String @figi: String, пример: ‘BBG000B9XRY4’ @from: Time @to: Time



326
327
328
329
330
331
332
333
334
335
# File 'lib/invest_tinkoff/v2/client.rb', line 326

def operations account_id:, figi:, from:, to:, state: InvestTinkoff::V2::OperationState::UNSPECIFIED
  body = {
    accountId: ,
    figi: figi,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT),
    state: state || InvestTinkoff::V2::OperationState::UNSPECIFIED
  }
  operation_request '/GetOperations', body
end

#order_book(figi:, depth: 10) ⇒ Object

Метод получения стакана по инструменту. tinkoff.github.io/investAPI/marketdata/#getorderbook

@figi: String, пример: ‘BBG000B9XRY4’ @depth: Integer



288
289
290
# File 'lib/invest_tinkoff/v2/client.rb', line 288

def order_book figi:, depth: 10
  market_request '/GetOrderBook', { figi: figi, depth: depth }
end

#order_state(account_id:, order_id:) ⇒ Object

Метод получения статуса торгового поручения. tinkoff.github.io/investAPI/orders/#getorderstate

@account_id: String @order_id: String



409
410
411
412
# File 'lib/invest_tinkoff/v2/client.rb', line 409

def order_state account_id:, order_id:
  body = { accountId: , orderId: order_id }
  order_request '/GetOrderState', body
end

#orders(account_id:) ⇒ Object

Метод получения списка активных заявок по счёту. tinkoff.github.io/investAPI/orders/#getorders

@account_id: String



369
370
371
# File 'lib/invest_tinkoff/v2/client.rb', line 369

def orders account_id:
  order_request '/GetOrders', { accountId:  }
end

#portfolio(account_id:) ⇒ Object

Метод получения портфеля по счёту. tinkoff.github.io/investAPI/operations/#getportfolio

@account_id: String



341
342
343
# File 'lib/invest_tinkoff/v2/client.rb', line 341

def portfolio account_id:
  operation_request '/GetPortfolio', { accountId:  }
end

#positions(account_id:) ⇒ Object

Метод получения списка позиций по счёту. tinkoff.github.io/investAPI/operations/#getpositions

@account_id: String



349
350
351
# File 'lib/invest_tinkoff/v2/client.rb', line 349

def positions account_id:
  operation_request '/GetPositions', { accountId:  }
end

#share_by(id_type:, id:, class_code: nil) ⇒ Object

Метод получения акции по её идентификатору. tinkoff.github.io/investAPI/instruments/#shareby

@id_type: InvestTinkoff::V2::InstrumentIdType @id: String @class_code: String (Обязателен при id_type = TICKER)

client.share_by id_type: InvestTinkoff::V2::InstrumentIdType::FIGI, id: ‘BBG000B9XRY4’



211
212
213
214
# File 'lib/invest_tinkoff/v2/client.rb', line 211

def share_by id_type:, id:, class_code: nil
  body = { idType: id_type, classCode: class_code, id: id }
  instrument_request '/ShareBy', body
end

#shares(instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE) ⇒ Object

Метод получения списка акций. tinkoff.github.io/investAPI/instruments/#shares

@instrument_status: InvestTinkoff::V2::InstrumentStatus



220
221
222
# File 'lib/invest_tinkoff/v2/client.rb', line 220

def shares instrument_status: InvestTinkoff::V2::InstrumentStatus::BASE
  instrument_request '/Shares', { instrumentStatus: instrument_status }
end

#stop_orders(account_id:) ⇒ Object

Метод получения списка активных стоп заявок по счёту. tinkoff.github.io/investAPI/stoporders/#getstoporders

@account_id: String



432
433
434
435
# File 'lib/invest_tinkoff/v2/client.rb', line 432

def stop_orders account_id:
  body = { accountId:  }
  stop_order_request '/GetStopOrders', body
end

#trading_schedules(exchange:, from:, to:) ⇒ Object

Метод получения расписания торгов торговых площадок. tinkoff.github.io/investAPI/instruments/#tradingschedulesrequest

@exchange: String, пример: ‘MOEX’ @from: Time, пример: 1.day.from_now @to: Time, пример: 5.days.from_now



230
231
232
233
234
235
236
237
# File 'lib/invest_tinkoff/v2/client.rb', line 230

def trading_schedules exchange:, from:, to:
  body = {
    exchange: exchange,
    from: from.strftime(TIME_FORMAT),
    to: to.strftime(TIME_FORMAT)
  }
  instrument_request '/TradingSchedules', body
end

#trading_status(figi:) ⇒ Object

Метод запроса статуса торгов по инструментам. tinkoff.github.io/investAPI/marketdata/#gettradingstatus

@figi: String, пример: ‘BBG000B9XRY4’



296
297
298
# File 'lib/invest_tinkoff/v2/client.rb', line 296

def trading_status figi:
  market_request '/GetTradingStatus', { figi: figi }
end

#user_tariffObject

Текущий тариф пользователя (лимиты запросов к API). tinkoff.github.io/investAPI/users/#getusertariff



507
508
509
# File 'lib/invest_tinkoff/v2/client.rb', line 507

def user_tariff
  user_service_request '/GetUserTariff'
end

#withdraw_limits(account_id:) ⇒ Object

Метод получения доступного остатка для вывода средств. tinkoff.github.io/investAPI/operations/#getwithdrawlimits

@account_id: String



357
358
359
# File 'lib/invest_tinkoff/v2/client.rb', line 357

def withdraw_limits account_id:
  operation_request '/GetWithdrawLimits', { accountId:  }
end