/tinkoff-broker

An implementation of Broker interface http://github.com/evsamsonov/trengin using Tinkoff Invest API https://tinkoff.github.io/investAPI/ for creating automated trading robots

Primary LanguageGoMIT LicenseMIT

tinkoff-broker

Lint Status Test Status Go Report Card codecov

An implementation of trengin.Broker using Tinkoff Invest API for creating automated trading robots.

Features

  • Opens position, changes stop loss and take profit, closes position.
  • Tracks open position.
  • Doesn't support multiple open positions at the same time.
  • Commission in position is approximate.

How to use

Create a new Tinkoff object using constructor New. Pass full-access token, user account identifier, FIGI of a trading instrument.

package main

import (
	"context"
	"log"

	tnkbroker "github.com/evsamsonov/tinkoff-broker"
	"github.com/evsamsonov/trengin"
)

func main() {
	tinkoffBroker, err := tnkbroker.New(
		"tinkoff-token",
		"123",
		"BBG004730N88",
		// options...
	)
	if err != nil {
		log.Fatal("Failed to create tinkoff broker")
	}
	
	tradingEngine := trengin.New(&Strategy{}, tinkoffBroker)
	if err = tradingEngine.Run(context.Background()); err != nil {
		log.Fatal("Trading engine crashed")
	}
}

type Strategy struct{}
func (s *Strategy) Run(ctx context.Context) error { panic("implement me") }
func (s *Strategy) Actions() trengin.Actions { panic("implement me") }

See more details in trengin documentation.

Option

You can configure Tinkoff to use Option

Methods Returns Option which
WithLogger Sets logger. The default logger is no-op Logger.
WithAppName Sets x-app-name.
WithProtectiveSpread Sets protective spread in percent for executing orders. The default value is 1%.
WithTradeStreamRetryTimeout Defines retry timeout on trade stream error.
WithTradeStreamPingWaitDuration Defines duration how long we wait for ping before reconnection.

Checkup

Use tinkoff-checkup for checking the ability to trade with a specific token, instrument and account.

How to install

go install github.com/evsamsonov/tinkoff-broker/cmd/tinkoff-checkup@latest

How to use

tinkoff-checkup [ACCOUNT_ID] [INSTRUMENT_FIGI] [-v]
Flag Description
-v Print logger output

Development

Makefile

Makefile tasks are required docker and golang.

$ make help    
doc                            Run doc server using docker
lint                           Run golang lint using docker
pre-push                       Run golang lint and test
test                           Run tests