diff --git a/exchange/binance.go b/exchange/binance.go index d1159405..a1e626a3 100644 --- a/exchange/binance.go +++ b/exchange/binance.go @@ -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] } diff --git a/exchange/csvfeed.go b/exchange/csvfeed.go index 97bbd088..92422263 100644 --- a/exchange/csvfeed.go +++ b/exchange/csvfeed.go @@ -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 { diff --git a/exchange/paperwallet.go b/exchange/paperwallet.go index 4864c8fa..8c10939b 100644 --- a/exchange/paperwallet.go +++ b/exchange/paperwallet.go @@ -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] } diff --git a/notification/telegram.go b/notification/telegram.go index 740a505d..e56cd3d2 100644 --- a/notification/telegram.go +++ b/notification/telegram.go @@ -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) diff --git a/order/controller.go b/order/controller.go index 1812fb03..9f509d7c 100644 --- a/order/controller.go +++ b/order/controller.go @@ -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 { diff --git a/service/service.go b/service/service.go index d8b01e43..42253dc1 100644 --- a/service/service.go +++ b/service/service.go @@ -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)