Skip to content

Commit

Permalink
fix: daily variation
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Oct 20, 2024
1 parent cbe2040 commit cf33722
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/coinapi/coinapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/araujo88/bitcoin-price-bot-nostr/pkg/config"
Expand Down Expand Up @@ -64,22 +65,30 @@ func FetchRate(currency string) (float64, error) {

// FetchDailyVariation retrieves the daily variation in percentage of the Bitcoin price for a specified currency
func FetchDailyVariation(currency string) (float64, error) {
symbolID := fmt.Sprintf("BITSTAMP_SPOT_BTC_%s", currency) // Adjust the exchange as needed
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02T00:00:00") // Format yesterday's date in ISO 8601 format
currency = strings.ToUpper(currency)
symbolID := fmt.Sprintf("BITSTAMP_SPOT_BTC_%s", currency)
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02T00:00:00")
url := fmt.Sprintf("%sohlcv/%s/history?period_id=1DAY&time_start=%s&limit=1", BASE_URL, symbolID, yesterday)

response, err := makeRequest(url)
if err != nil {
return 0, err
}

if len(response) == 0 {
return 0, fmt.Errorf("no response data")
}

var data []responses.OHLCVData
if err := json.Unmarshal(response, &data); err != nil {
return 0, fmt.Errorf("error unmarshalling OHLCV data: %w", err)
}

if len(data) > 0 {
ohlcv := data[0]
if ohlcv.PriceOpen == 0 {
return 0, fmt.Errorf("price open is zero, cannot calculate variation")
}
variation := ((ohlcv.PriceClose - ohlcv.PriceOpen) / ohlcv.PriceOpen) * 100
return variation, nil
}
Expand Down

0 comments on commit cf33722

Please sign in to comment.