Documentation
¶
Overview ¶
Package oanda is a dependency-free Go client for the Oanda REST v20 API: instrument candles (GetCandles), account/trading calls (trading.go), and instrument facts (instrument.go). It uses only net/http and the standard library.
The access token is caller-supplied at construction; the package never reads environment variables, never logs the token, and sends it only in the Authorization header (never in URLs or error messages).
Index ¶
- type Candle
- type Client
- func (c *Client) AccountID(ctx context.Context) (string, error)
- func (c *Client) AvgSpread(ctx context.Context, instrument string, n int) (float64, error)
- func (c *Client) GetCandles(ctx context.Context, instrument, granularity string, start, end time.Time) ([]Candle, error)
- func (c *Client) InstrumentFacts(ctx context.Context, instrument string) (*InstrumentFacts, error)
- func (c *Client) MarketBuy(ctx context.Context, instrument string, units float64) (*Fill, error)
- func (c *Client) OpenTrades(ctx context.Context, instrument string) ([]Trade, error)
- func (c *Client) SetTakeProfit(ctx context.Context, tradeID, price string) error
- type Fill
- type InstrumentFacts
- type Option
- type Trade
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Candle ¶
Candle is one OHLCV bar as returned by GetCandles: mid prices, tick-count volume, and whether the candle has fully closed.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an Oanda REST v20 client: candle fetching (GetCandles, in candles.go) plus the account/trading calls in trading.go and instrument.go. Create with New.
func New ¶
New returns a Client for the live environment (api-fxtrade.oanda.com) authenticated with the given personal access token.
func (*Client) AccountID ¶
AccountID returns the configured account ID, resolving (and caching) the token's first account when none was pinned with WithAccount. Safe for concurrent use: the cache is mutex-guarded and concurrent first calls are serialized so the /v3/accounts lookup runs at most once.
func (*Client) AvgSpread ¶
AvgSpread returns the average bid-ask spread (in price units) over the last n complete M10 candles — a live estimate of the instrument's trading cost.
func (*Client) GetCandles ¶
func (c *Client) GetCandles(ctx context.Context, instrument, granularity string, start, end time.Time) ([]Candle, error)
GetCandles downloads mid-price OHLCV candles for instrument in [start, end), paginating in pages of 5000. Incomplete (still forming) candles are skipped. granularity is an Oanda-native granularity string (e.g. "M1", "H4", "D", "W", "M").
func (*Client) InstrumentFacts ¶
InstrumentFacts fetches the facts for instrument from the account's instruments endpoint.
func (*Client) MarketBuy ¶
MarketBuy places a FOK market buy and returns the fill. No take-profit is attached — call SetTakeProfit with the fill price for a fill-relative TP. A cancelled order (e.g. INSUFFICIENT_MARGIN) is returned as an error.
func (*Client) OpenTrades ¶
OpenTrades returns the account's open LONG trades for instrument, in the order Oanda returns them.
type InstrumentFacts ¶
type InstrumentFacts struct {
Name string
DisplayPrecision int
MarginRate float64
LongRate float64 // annual financing, long side (e.g. -0.0468 = 4.68%/yr cost)
ShortRate float64 // annual financing, short side
}
InstrumentFacts are the account-relative trading facts for one instrument: price precision, margin requirement, and annual financing rates (negative values are costs, per Oanda convention).
type Option ¶
type Option func(*Client)
Option configures a Client.
func Practice ¶
func Practice() Option
Practice targets the practice (demo) environment api-fxpractice.oanda.com.
func WithAccount ¶
WithAccount pins the account ID, skipping the /v3/accounts lookup.
func WithHTTPClient ¶
WithHTTPClient replaces the default HTTP client (30s timeout).
Directories
¶
| Path | Synopsis |
|---|---|
|
backtestsource
module
|
|
|
examples
|
|
|
account
command
Command account resolves the account ID for the given token and prints a summary-style dump of InstrumentFacts for EUR_USD.
|
Command account resolves the account ID for the given token and prints a summary-style dump of InstrumentFacts for EUR_USD. |
|
candles
command
Command candles fetches the last day of H1 candles for EUR_USD and prints the 5 most recent complete ones.
|
Command candles fetches the last day of H1 candles for EUR_USD and prints the 5 most recent complete ones. |
|
marketorder
command
Command marketorder places a 1-unit market buy on EUR_USD and attaches a take-profit at the fill price plus 0.0010.
|
Command marketorder places a 1-unit market buy on EUR_USD and attaches a take-profit at the fill price plus 0.0010. |