tnkbroker

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 19 Imported by: 0

README

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 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/v2"
)

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, actions 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.
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

Documentation

Overview

Package tnkbroker implements trengin.Broker using Tinkoff Invest API. Supports multiple open positions at the same time. Commission in position is approximate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Money

type Money interface {
	GetUnits() int64
	GetNano() int32
}

type MoneyValue

type MoneyValue struct {
	Money
}

func NewMoneyValue

func NewMoneyValue(v Money) *MoneyValue

func NewZeroMoneyValue added in v0.1.3

func NewZeroMoneyValue() *MoneyValue

func (*MoneyValue) ToFloat

func (v *MoneyValue) ToFloat() float64

type Option

type Option func(*Tinkoff)

func WithAppName

func WithAppName(appName string) Option

WithAppName returns Option which sets x-app-name

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger returns Option which sets logger. The default logger is no-op Logger

func WithProtectiveSpread

func WithProtectiveSpread(protectiveSpread float64) Option

WithProtectiveSpread returns Option which sets protective spread in percent for executing orders. The default value is 1%

func WithTradeStreamPingWaitDuration added in v0.1.3

func WithTradeStreamPingWaitDuration(duration time.Duration) Option

WithTradeStreamPingWaitDuration returns Option which defines duration how long we wait for ping before reconnection

func WithTradeStreamRetryTimeout added in v0.1.2

func WithTradeStreamRetryTimeout(timeout time.Duration) Option

WithTradeStreamRetryTimeout returns Option which defines retry timeout on trade stream error

type Tinkoff

type Tinkoff struct {
	// contains filtered or unexported fields
}

func New

func New(token, accountID string, opts ...Option) (*Tinkoff, error)

New creates a new Tinkoff object. It takes full-access token, user account identifier.

func (*Tinkoff) ChangeConditionalOrder

func (t *Tinkoff) ChangeConditionalOrder(
	ctx context.Context,
	action trengin.ChangeConditionalOrderAction,
) (trengin.Position, error)

ChangeConditionalOrder changes stop loss and take profit of current position. It returns updated position.

func (*Tinkoff) ClosePosition

func (t *Tinkoff) ClosePosition(ctx context.Context, action trengin.ClosePositionAction) (trengin.Position, error)

ClosePosition closes current position and returns closed position.

func (*Tinkoff) OpenPosition

func (t *Tinkoff) OpenPosition(
	ctx context.Context,
	action trengin.OpenPositionAction,
) (trengin.Position, trengin.PositionClosed, error)

OpenPosition opens position, returns new position and channel for tracking position closing.

func (*Tinkoff) Run

func (t *Tinkoff) Run(ctx context.Context) error

Run starts to track an open positions

type ZeroMoney added in v0.1.3

type ZeroMoney struct{}

func (*ZeroMoney) GetNano added in v0.1.3

func (*ZeroMoney) GetNano() int32

func (*ZeroMoney) GetUnits added in v0.1.3

func (*ZeroMoney) GetUnits() int64

Directories

Path Synopsis
cmd
tinkoff-checkup
Tinkoff-checkup checks all methods of Tinkoff Broker.
Tinkoff-checkup checks all methods of Tinkoff Broker.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL