Skip to content

Commit

Permalink
types: fix asset package and calculate debt, interest in usd
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 16, 2024
1 parent 51b987b commit 46ea27d
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 272 deletions.
186 changes: 0 additions & 186 deletions pkg/asset/asset.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/spf13/viper"
"gopkg.in/tucnak/telebot.v2"

"github.com/c9s/bbgo/pkg/asset"
"github.com/c9s/bbgo/pkg/envvar"
"github.com/c9s/bbgo/pkg/exchange"
"github.com/c9s/bbgo/pkg/fixedpoint"
Expand All @@ -30,6 +29,7 @@ import (
googleservice "github.com/c9s/bbgo/pkg/service/google"
"github.com/c9s/bbgo/pkg/slack/slacklog"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/asset"
"github.com/c9s/bbgo/pkg/util"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/asset"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/asset"
)

const DefaultBindAddress = "localhost:8080"
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jmoiron/sqlx"
"go.uber.org/multierr"

"github.com/c9s/bbgo/pkg/asset"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/asset"
)

type AccountService struct {
Expand Down Expand Up @@ -52,8 +52,8 @@ func (s *AccountService) InsertAsset(
account,
time,
v.Currency,
v.InUSD,
v.InBTC,
v.NetAssetInUSD,
v.NetAssetInBTC,
v.Total,
v.Available,
v.Locked,
Expand Down
22 changes: 11 additions & 11 deletions pkg/service/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/asset"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/asset"
)

func TestAccountService(t *testing.T) {
Expand All @@ -26,16 +26,16 @@ func TestAccountService(t *testing.T) {
t1 := time.Now()
err = service.InsertAsset(t1, "binance", types.ExchangeBinance, "main", false, false, "", asset.Map{
"BTC": asset.Asset{
Currency: "BTC",
Total: fixedpoint.MustNewFromString("1.0"),
InUSD: fixedpoint.MustNewFromString("10.0"),
InBTC: fixedpoint.MustNewFromString("0.0001"),
Time: t1,
Locked: fixedpoint.MustNewFromString("0"),
Available: fixedpoint.MustNewFromString("1.0"),
Borrowed: fixedpoint.MustNewFromString("0"),
NetAsset: fixedpoint.MustNewFromString("1"),
PriceInUSD: fixedpoint.MustNewFromString("44870"),
Currency: "BTC",
Total: fixedpoint.MustNewFromString("1.0"),
NetAssetInUSD: fixedpoint.MustNewFromString("10.0"),
NetAssetInBTC: fixedpoint.MustNewFromString("0.0001"),
Time: t1,
Locked: fixedpoint.MustNewFromString("0"),
Available: fixedpoint.MustNewFromString("1.0"),
Borrowed: fixedpoint.MustNewFromString("0"),
NetAsset: fixedpoint.MustNewFromString("1"),
PriceInUSD: fixedpoint.MustNewFromString("44870"),
},
})
assert.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/strategy/tri/_doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
tri is an example strategy that demonstrates how to implement an arbitrage strategy.
*/
package tri
30 changes: 15 additions & 15 deletions pkg/strategy/tri/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ func (p *MultiCurrencyPosition) handleTrade(trade types.Trade) {

func (p *MultiCurrencyPosition) CollectProfits() []Profit {
var profits []Profit
for currency, base := range p.Currencies {
for cu, base := range p.Currencies {
if base.IsZero() {
continue
}

profit := Profit{
Asset: currency,
Asset: cu,
Profit: base,
ProfitInUSD: fixedpoint.Zero,
}

if price, ok := p.TradePrices[currency]; ok && !price.IsZero() {
if price, ok := p.TradePrices[cu]; ok && !price.IsZero() {
profit.ProfitInUSD = base.Mul(price)
} else if currency.IsUSDFiatCurrency(currency) {
} else if currency.IsUSDFiatCurrency(cu) {
profit.ProfitInUSD = base
}

profits = append(profits, profit)

if total, ok := p.TotalProfits[currency]; ok {
p.TotalProfits[currency] = total.Add(base)
if total, ok := p.TotalProfits[cu]; ok {
p.TotalProfits[cu] = total.Add(base)
} else {
p.TotalProfits[currency] = base
p.TotalProfits[cu] = base
}
}

Expand All @@ -100,40 +100,40 @@ func (p *MultiCurrencyPosition) CollectProfits() []Profit {
}

func (p *MultiCurrencyPosition) Reset() {
for currency := range p.Currencies {
p.Currencies[currency] = fixedpoint.Zero
for cu := range p.Currencies {
p.Currencies[cu] = fixedpoint.Zero
}
}

func (p *MultiCurrencyPosition) String() (o string) {
o += "position: \n"

for currency, base := range p.Currencies {
for cu, base := range p.Currencies {
if base.IsZero() {
continue
}

o += fmt.Sprintf("- %s: %f\n", currency, base.Float64())
o += fmt.Sprintf("- %s: %f\n", cu, base.Float64())
}

o += "totalProfits: \n"

for currency, total := range p.TotalProfits {
for cu, total := range p.TotalProfits {
if total.IsZero() {
continue
}

o += fmt.Sprintf("- %s: %f\n", currency, total.Float64())
o += fmt.Sprintf("- %s: %f\n", cu, total.Float64())
}

o += "fees: \n"

for currency, fee := range p.Fees {
for cu, fee := range p.Fees {
if fee.IsZero() {
continue
}

o += fmt.Sprintf("- %s: %f\n", currency, fee.Float64())
o += fmt.Sprintf("- %s: %f\n", cu, fee.Float64())
}

return o
Expand Down
Loading

0 comments on commit 46ea27d

Please sign in to comment.