Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

nazarovsa/Insight.Tinkoff.InvestSdk

Repository files navigation

Unofficial Tinkoff Invest .Net Sdk [DEPRECATED]

Build Status nuget version Nuget

[DEPRECATED]

Repository and package deprecated because of new api presented. See this issue.

About

This is sdk for interact with Tinkoff Invest OpenApi. Please, write your questions and suggestions to issues.

Get started

Firstly get access token - description of procedure

Structure of SDK

Interaction with the API occurs through the following services:

  • MarketService - Information about instruments (Rest, implements IMarketService)
  • OrderService - Information about limit orders and their placement (Rest, implements IOrderService)
  • PortfolioService - Information about portfolio (Rest, implements IPortfolioService)
  • OperationService - Information about operations (Rest, implements IOperationService)
  • SandboxService - Interact with sandbox (Rest, implements ISandboxService)
  • UserService - Information about user (accounts) (Rest, implements IUserService)
  • StreamMarketService - Interaction with API throught WebSocket protocol (WebSocket, implements IStreamMarketService)

All Rest services initializes by RestConfiguration:

  • string AccessToken - Access token
  • string BaseUrl - Base address, default = "https://api-invest.tinkoff.ru"
  • bool SandboxMode - Is sandbox enabled, default = true

StreamMarketService initializes by StreamConfiguration:

  • string AccessToken - Access token
  • string Address - WebSocket address, default - "wss://api-invest.tinkoff.ru/openapi/md/v1/md-openapi/ws"
  • bool ResubscribeOnReconnect - If true StreamMarketService will remember sent subscriptions and will resubscribe on reconnect, default - true
  • bool ReconnectEnabled - Enable reconnect, default - true
  • TimeSpan ReconnectTimeout - Time to reconnect from last message, default - 30 seconds
  • TimeSpan ErrorReconnectTimeout - Reconnect timeout on error, default - 60 seconds

StreamMarketService

StreamMarketService implements IStreamMarketService:

public interface IStreamMarketService
{
    Task Send(IWsMessage message);

    IObservable<WsMessage> AsObservable();
}

Send method alows you to send subscribe and unsubscribe messages. Messages types implements IWsMessage: SubscribeCandleMessage, UnsubscribeCandleMessage, SubscribeOrderBookMessage, UnsubscribeOrderBookMessage, SubscribeInstrumentInfoMessage, UnsubscribeInstrumentInfoMessage, CandleMessage, OrderBookMessage, InstrumentInfoMessage. AsObservable() implemented via System.Reactive, it gives you Push collection, which will receive messages of type IWsMessage (CandleMessage, OrderBookMessage, InstrumentInfoMessage). Messages deserializes into right type using Event property, you can use Pattern Matching. Example:

public sealed class Program {
  private StreamConfiguration _config;
  
  public Program(StreamConfiguration config) {
    if(config == null)
      throw new ArgumentNullException(nameof(config));
      
    _config = config;
  }
  
  public async Task DoWork() {
    using (var client = new StreamMarketService(_config))
    {
      IDisposable subscription = client
        .AsObservable()
        .Do(x =>
        {
          switch (x)
          {
            case CandleMessage message:
              Console.WriteLine($"type: {message}, figi: {message.Payload.Figi}");
              break;
            default:
              Console.WriteLine("Unknown message type");
              break;
          }
        }, ex => { throw ex; })
        .Subscribe();
    
      await client.Send(new SubscribeCandleMessage
      {
        Figi = "{figi}",
        Interval = CandleInterval.Minute
      });

      await Task.Delay(1000 * 5);

      await client.Send(new UnsubscribeCandleMessage()
      {
        Figi = "{figi}",
        Interval = CandleInterval.Minute
      });
      
      subscription?.Dispose();
    }
  }
}