sbpfx

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2025 License: MIT Imports: 11 Imported by: 0

README

State Bank of Pakistan Exchange Rates

Overview

The State Bank of Pakistan (SBP) publishes exchange rates for various currencies against the Pakistani Rupee (PKR) on a daily basis. They publish the exchange rates on the internet in PDF format at a deterministic URL for each day. This lib provides an interface to fetch and/or download the exchange rates for any given date.

Example Rate Sheets:

[!NOTE]

These Exchange Rates are issued by the State Bank of Pakistan for Authorized Dealers to revalue their books daily on Mark-to-Market basis. M2M rate of USD is compiled as weighted average of closing interbank exchange rate collected through Brokerage Houses. M2M rates of other currencies are compiled on the basis of USD/PKR rate compiled from brokerage houses’ data and exchange rate of other currencies against USD quoted on Reuters Eikon Terminal.

[!IMPORTANT]

These rates will almost always differ from google rates (e.g. rates from google search).

Installation

go get github.com/mistermoe/sbpfx

Usage

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	rate, err := client.GetExchangeRate(context.Background(), sbpfx.USD)
	if err != nil {
		log.Fatalf("failed to get exchange rate: %v", err)
	}
	fmt.Println(rate)
}

API

GetExchangeRate

Fetches the exchange rate for a given currency. Optionally, you can pass in a date to fetch the exchange rate for a specific date. If no date is provided, the current date is used.

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	rate, err := client.GetExchangeRate(context.Background(), sbpfx.USD)
	if err != nil {
		log.Fatalf("failed to get exchange rate: %v", err)
	}
	
  rateJSON, err := json.Marshal(rate)
  if err != nil {
    log.Fatalf("failed to marshal exchange rate: %v", err)
  }
  
  fmt.Println(string(rateJSON))
Optional Date

You can pass in a date to fetch the exchange rate for a specific date. If no date is provided, the current date is used.

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	rate, err := client.GetExchangeRate(context.Background(), sbpfx.USD, sbpfx.ForDate("2025-08-27"))
	if err != nil {
		log.Fatalf("failed to get exchange rate: %v", err)
	}
	fmt.Println(rate)
}
GetExchangeRates

Fetches the exchange rates for all currencies. Optionally, you can pass in a date to fetch the exchange rates for a specific date. If no date is provided, the current date is used.

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	rates, err := client.GetExchangeRates(context.Background())
	if err != nil {
		log.Fatalf("failed to get exchange rates: %v", err)
	}
	
	ratesJSON, err := json.Marshal(rates)
	if err != nil {
		log.Fatalf("failed to marshal exchange rates: %v", err)
	}
	
	fmt.Println(string(ratesJSON))
Optional Date

You can pass in a date to fetch the exchange rates for a specific date. If no date is provided, the current date is used.

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	rates, err := client.GetExchangeRates(context.Background(), sbpfx.ForDate("2025-08-27"))
	if err != nil {
		log.Fatalf("failed to get exchange rates: %v", err)
	}
	
	ratesJSON, err := json.Marshal(rates)
	if err != nil {
		log.Fatalf("failed to marshal exchange rates: %v", err)
	}
	
	fmt.Println(string(ratesJSON))
DownloadRateSheet

Downloads the exchange rate sheet for a given date to the specified file path. Optionally, you can pass in a date to download the exchange rate sheet for a specific date. If no date is provided, the current date is used.

import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	err := client.DownloadRateSheet(context.Background(), "exchange_rates.pdf")
	if err != nil {
		log.Fatalf("failed to download exchange rate sheet: %v", err)
	}
}
Optional Date

You can pass in a date to download the exchange rate sheet for a specific date. If no date is provided, the current date is used.


import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	err := client.DownloadRateSheet(context.Background(), "exchange_rates.pdf", sbpfx.ForDate("2025-08-27"))
	if err != nil {
		log.Fatalf("failed to download exchange rate sheet: %v", err)
	}
}

GetUrl

Returns the URL for the exchange rate sheet for a given date. Optionally, you can pass in a date to get the URL for a specific date. If no date is provided, the current date is used.


import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	url := client.GetUrl(sbpfx.ForDate("2025-08-27"))
	fmt.Println(url)
}
Optional Date

You can pass in a date to get the URL for a specific date. If no date is provided, the current date is used.


import "github.com/mistermoe/sbpfx"

func main() {
	client := sbpfx.New()
	url := client.GetUrl(sbpfx.ForDate("2025-08-27"))
	fmt.Println(url)
}

Documentation

Index

Constants

View Source
const (
	BaseURL      = "https://www.sbp.org.pk/ecodata/rates/m2m"
	HTTPStatusOK = 200
	YearModulo   = 100 // For getting last 2 digits of year
)
View Source
const (
	HoursInDay = 24 // Hours in a day for time truncation
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(options ...httpr.ClientOption) *Client

func (*Client) DownloadRateSheet

func (c *Client) DownloadRateSheet(ctx context.Context, path string, opts ...Option) error

DownloadRateSheet downloads the exchange rate PDF to the specified file path.

func (*Client) GetExchangeRate

func (c *Client) GetExchangeRate(ctx context.Context, currency Currency, opts ...Option) (*ExchangeRate, error)

func (*Client) GetExchangeRates

func (c *Client) GetExchangeRates(ctx context.Context, opts ...Option) (map[Currency]*ExchangeRate, error)

func (*Client) GetUrl

func (c *Client) GetUrl(opts ...Option) string

type Currency

type Currency string
const (
	USD Currency = "USD"
	EUR Currency = "EUR"
	GBP Currency = "GBP"
	JPY Currency = "JPY"
	CHF Currency = "CHF"
	AUD Currency = "AUD"
	CAD Currency = "CAD"
	SEK Currency = "SEK"
	NOK Currency = "NOK"
	DKK Currency = "DKK"
	SAR Currency = "SAR"
	AED Currency = "AED"
	KWD Currency = "KWD"
	BHD Currency = "BHD"
	QAR Currency = "QAR"
	OMR Currency = "OMR"
	CNY Currency = "CNY"
	HKD Currency = "HKD"
	SGD Currency = "SGD"
	THB Currency = "THB"
	MYR Currency = "MYR"
	INR Currency = "INR"
	KRW Currency = "KRW"
	NZD Currency = "NZD"
	ZAR Currency = "ZAR"
	BDT Currency = "BDT"
	BRL Currency = "BRL"
	ARS Currency = "ARS"
	LKR Currency = "LKR"
	TRY Currency = "TRY"
	IDR Currency = "IDR"
	MXN Currency = "MXN"
	RUB Currency = "RUB"
	GNH Currency = "GNH"
)

func (Currency) IsValid

func (c Currency) IsValid() bool

func (Currency) String

func (c Currency) String() string

type ExchangeRate

type ExchangeRate struct {
	Currency   Currency  `json:"currency"`
	Date       time.Time `json:"date"`
	URL        string    `json:"url"`                   // Source PDF URL
	Ready      string    `json:"ready,omitempty"`       // Spot rate (immediate delivery)
	OneWeek    string    `json:"one_week,omitempty"`    // 1-week forward rate
	TwoWeek    string    `json:"two_week,omitempty"`    // 2-week forward rate
	OneMonth   string    `json:"one_month,omitempty"`   // 1-month forward rate
	TwoMonth   string    `json:"two_month,omitempty"`   // 2-month forward rate
	ThreeMonth string    `json:"three_month,omitempty"` // 3-month forward rate
	FourMonth  string    `json:"four_month,omitempty"`  // 4-month forward rate
	FiveMonth  string    `json:"five_month,omitempty"`  // 5-month forward rate
	SixMonth   string    `json:"six_month,omitempty"`   // 6-month forward rate
	NineMonth  string    `json:"nine_month,omitempty"`  // 9-month forward rate
	OneYear    string    `json:"one_year,omitempty"`    // 1-year forward rate
}

ExchangeRate represents exchange rates for different delivery periods These are forward rates used for currency hedging and speculation.

func (*ExchangeRate) GetSpotRate

func (e *ExchangeRate) GetSpotRate() string

GetSpotRate returns the spot rate (Ready rate) as a string.

type Option

type Option func(*option) error

func ForDate

func ForDate(dateStr string) Option

ForDate sets a specific date for the exchange rate request using a string in YYYY-MM-DD format.

func ForTime

func ForTime(date time.Time) Option

ForTime sets a specific date for the exchange rate request using a time.Time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL