Skip to content

Instantly share code, notes, and snippets.

@kirugan
Created April 9, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirugan/a10c3c8fad1affb70afa5d7d2b376b8f to your computer and use it in GitHub Desktop.
Save kirugan/a10c3c8fad1affb70afa5d7d2b376b8f to your computer and use it in GitHub Desktop.
Small script to analyze payIn and payOut in tinkoff investment
package main
import (
"context"
"fmt"
"math/rand"
"os"
"time"
sdk "github.com/TinkoffCreditSystems/invest-openapi-go-sdk"
)
type stat struct {
firstDate time.Time
lastDate time.Time
payIn float64
payOut float64
}
const usd2rub = 75
func main() {
token := os.Args[1]
rand.Seed(time.Now().UnixNano()) // инициируем Seed рандома для функции requestID
client := sdk.NewRestClient(token)
accs, err := client.Accounts(context.TODO())
if err != nil {
panic(err)
}
var accountId string
for _, acc := range accs {
if acc.Type == sdk.AccountTinkoff {
accountId = acc.ID
break
}
}
ops, err := client.Operations(context.TODO(), accountId, time.Unix(0, 0), time.Now(), ``)
if err != nil {
panic(err)
}
var stats = map[sdk.Currency]stat{}
for _, op := range ops {
stat, found := stats[op.Currency]
if !found {
stat.firstDate = time.Unix(0, 0)
}
// внутри payIn, payOut операций нет комиссий
if op.OperationType == sdk.OperationTypePayIn {
fmt.Printf("%v я внёс %.2f\n", op.DateTime, op.Payment)
stat.payIn += op.Payment
} else if op.OperationType == sdk.OperationTypePayOut {
fmt.Printf("%v я забрал %.2f\n", op.DateTime, op.Payment)
stat.payOut += -op.Payment
} else {
continue
}
if stat.firstDate.After(op.DateTime) || stat.firstDate == time.Unix(0, 0) {
stat.firstDate = op.DateTime
}
if stat.lastDate.Before(op.DateTime) || stat.lastDate == time.Unix(0, 0) {
stat.lastDate = op.DateTime
}
stats[op.Currency] = stat
}
const format = `2006-01-03`
var moneyLeft float64
for currency, stat := range stats {
moneyLeftInCurrency := stat.payIn - stat.payOut
fmt.Printf("Всего с %v по %v в %v:\n", stat.firstDate.Format(format), stat.lastDate.Format(format), currency)
fmt.Printf("\tя внёс %.2f и взял %.2f\n", stat.payIn, stat.payOut)
if currency == `RUB` {
moneyLeft += moneyLeftInCurrency
} else if currency == `USD` {
moneyLeft += moneyLeftInCurrency * usd2rub
}
}
fmt.Printf("На балансе должно остаться %.2f руб.\n", moneyLeft)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment