Skip to content

Commit

Permalink
fix(telegram): display realtime balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito committed Jan 23, 2022
1 parent 3cec963 commit 4952fe6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions exchange/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ func NewBinance(ctx context.Context, options ...BinanceOption) (*Binance, error)
return exchange, nil
}

func (b *Binance) LastQuote(ctx context.Context, pair string) (float64, error) {
candles, err := b.CandlesByLimit(ctx, pair, "1m", 1)
if err != nil || len(candles) < 1 {
return 0, err
}
return candles[0].Close, nil
}

func (b *Binance) AssetsInfo(pair string) model.AssetInfo {
return b.assetsInfo[pair]
}
Expand Down
4 changes: 4 additions & 0 deletions exchange/csvfeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (c CSVFeed) feedTimeframeKey(pair, timeframe string) string {
return fmt.Sprintf("%s--%s", pair, timeframe)
}

func (c CSVFeed) LastQuote(_ context.Context, _ string) (float64, error) {
return 0, errors.New("invalid operation")
}

func isFistCandlePeriod(t time.Time, fromTimeframe, targetTimeframe string) (bool, error) {
fromDuration, err := str2duration.ParseDuration(fromTimeframe)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions exchange/paperwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (p *PaperWallet) Pairs() []string {
return pairs
}

func (p *PaperWallet) LastQuote(ctx context.Context, pair string) (float64, error) {
return p.feeder.LastQuote(ctx, pair)
}

func (p *PaperWallet) AssetValues(pair string) []AssetValue {
return p.assetValues[pair]
}
Expand Down
3 changes: 2 additions & 1 deletion notification/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ func (t telegram) BalanceHandle(m *tb.Message) {
return
}

assetValue, err := t.orderController.PositionValue(pair)
quote, err := t.orderController.LastQuote(pair)
if err != nil {
log.Error(err)
t.OnError(err)
return
}

assetValue := assetSize * quote
quotesValue[quotePair] = quoteSize
total += assetValue
message += fmt.Sprintf("%s: `%.4f` ≅ `%.2f` %s \n", assetPair, assetSize, assetValue, quotePair)
Expand Down
4 changes: 4 additions & 0 deletions order/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ func (c *Controller) Position(pair string) (asset, quote float64, err error) {
return c.exchange.Position(pair)
}

func (c *Controller) LastQuote(pair string) (float64, error) {
return c.exchange.LastQuote(c.ctx, pair)
}

func (c *Controller) PositionValue(pair string) (float64, error) {
asset, _, err := c.exchange.Position(pair)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Exchange interface {

type Feeder interface {
AssetsInfo(pair string) model.AssetInfo
LastQuote(ctx context.Context, pair string) (float64, error)
CandlesByPeriod(ctx context.Context, pair, period string, start, end time.Time) ([]model.Candle, error)
CandlesByLimit(ctx context.Context, pair, period string, limit int) ([]model.Candle, error)
CandlesSubscription(ctx context.Context, pair, timeframe string) (chan model.Candle, chan error)
Expand Down

0 comments on commit 4952fe6

Please sign in to comment.