Skip to content

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

License

Notifications You must be signed in to change notification settings

evsamsonov/tinkoff-broker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.
  • Supports multiple open positions at the same time.
  • Commission in position is approximate.

How to use

Create a new Tinkoff object using constructor New. Pass Tinkoff Client and user account identifier.

package main

import (
	"context"
	"log"

	"github.com/evsamsonov/trengin/v2"
	"github.com/russianinvestments/invest-api-go-sdk/investgo"
	"go.uber.org/zap"

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

func main() {
	ctx := context.Background()

	tinkoffConfig := investgo.Config{
		EndPoint:  "invest-public-api.tinkoff.ru:443",
		Token:     "[tinkoff-token]",
		AccountId: "[account-id]",
	}
	tinkoffClient, err := investgo.NewClient(ctx, tinkoffConfig, zap.NewNop().Sugar())
	if err != nil {
		log.Fatal("Failed to create tinkoff client", zap.Error(err))
	}
	
	tinkoffBroker, err := tnkbroker.New(
		tinkoffClient,
		"[account-id]",
		// 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, actions trengin.Actions) error { 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.
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/v2/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
generate                       Run go generate