Created
April 9, 2020 15:19
-
-
Save kirugan/a10c3c8fad1affb70afa5d7d2b376b8f to your computer and use it in GitHub Desktop.
Small script to analyze payIn and payOut in tinkoff investment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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