Getting Started with igfetchr

knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

Introduction

igfetchr is a beginner-friendly wrapper around the IG Trading REST API (labs.ig.com). It provides functions to authenticate, fetch markets, current prices, historical prices, account summaries, execute trades, and close sessions.

Disclaimer: Trading CFDs and spread bets carry a high risk of losing money. The package igfetchr is not financial advice.

Offline / testing mode

To make examples and tests deterministic and CRAN-friendly, igfetchr supports an offline testing mode. Set the environment variable IGFETCHR_TESTING = "true" to return mock tokens and accept mock_response data frames for endpoints.

Sys.setenv(IGFETCHR_TESTING = "true")
library(igfetchr)

Authenticate (mock)

This example uses the package's testing mode and returns a mock auth list instantly.

auth <- ig_auth(
  username = "demo_user",
  password = "demo_pass",
  api_key = "demo_api_key",
  acc_type = "DEMO",
  acc_number = "ABC123"
)
auth

Search markets (mock)

Use ig_search_markets() with mock_response to simulate the API returning market results.

mock_markets <- data.frame(
  epic = c("CS.D.USDCHF.CFD.IP"),
  instrumentName = c("USD/CHF"),
  stringsAsFactors = FALSE
)

markets <- ig_search_markets("USD/CHF", auth = auth, mock_response = mock_markets)
markets

Current price (mock)

Simulate a current price response for USD/CHF.

mock_price <- data.frame(
  bid = 0.8500,
  offer = 0.8504,
  timestamp = Sys.time(),
  stringsAsFactors = FALSE
)
price <- ig_get_price("CS.D.USDCHF.CFD.IP", auth = auth, mock_response = mock_price)
price

Historical prices (mock)

Simulate historical OHLC data for USD/CHF.

mock_hist <- data.frame(
  snapshotTime = as.character(Sys.Date() - 2:0),
  open = c(0.8500, 0.8550, 0.8520),
  high = c(0.8520, 0.8570, 0.8540),
  low = c(0.8480, 0.8530, 0.8500),
  close = c(0.8510, 0.8540, 0.8530),
  stringsAsFactors = FALSE
)

hist <- ig_get_historical(
  epic = "CS.D.USDCHF.CFD.IP",
  from = Sys.Date() - 2,
  to = Sys.Date(),
  resolution = "D",
  auth = auth,
  mock_response = mock_hist
)
hist

Accounts (mock)

Simulate account summary retrieval.

mock_accounts <- data.frame(
  accountId = "ACCT123",
  balance = 10000,
  preferred = TRUE,
  stringsAsFactors = FALSE
)
accounts <- ig_get_accounts(auth = auth, mock_response = mock_accounts)
accounts

Execute Trade (mock)

Simulate executing a trade for USD/CHF.

mock_trade <- data.frame(
  dealId = "DIXXXX",
  dealReference = "REF123",
  status = "OPEN",
  stringsAsFactors = FALSE
)
trade <- ig_execute_trade(
  epic = "CS.D.USDCHF.CFD.IP",
  direction = "BUY",
  size = 1.0,
  auth = auth,
  mock_response = mock_trade
)
trade

Close Session (mock)

Close the session.

ig_close_session(auth, mock_response = TRUE)
Sys.unsetenv("IGFETCHR_TESTING")

Notes



Try the igfetchr package in your browser

Any scripts or data that you put into this service are public.

igfetchr documentation built on Nov. 21, 2025, 5:07 p.m.