Code Monkey home page Code Monkey logo

rig's Introduction

---
title: "rIG - IG Markets API Vignette"
date: "11 March 2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

For some reason (which I'm yet to find out) reamme.html is not correctly showing-up on Git!
All help in maintaining this library is very much appreciated!


## Install and load up libraries

To get started you will need loging id/password from your IG account along with your api key. The current link is pointed at the Dev verson of the API.

The usual warnings apply for use of this library, I built this for my personal use, there is no regular support for this library. I have published this to help and advance the R community. Use at own risk.


To get started install **rIG** along with its dependancies: 

```{}
library(ghit)
install_github("JoeFernando/rIG")

library(rIG)
library(tidyverse)
library(lubridate)
library(stringr)
library(jsonlite)
library(httr)
library(rlist)
library(glue)
```



## Login/Logout

```{}

b   <- '{"identifier":"your_login_id", "password": "insert_password_here"}'
api <- "insert_api_key_here"

ig_login(b, api) 

ig_logout() # this will end your session

```



## Searching for Markets

A tibble is retuned for all matching markets:

```{}
ig_search("Copper")
ig_search("AUDUSD")
```



## Time resolutions for history data

There is a helper `time_resolution` vector to list out the options available. Presently, `SECOND`is not working.

```{r, echo = TRUE}

rIG::time_resolution

```



## Downloading history

When you query the API the returned data gives both `bid` and `ask`. Hence, there is a two step process to getting data in the familiar `ohlcv` form, however the process is straighforward. If you prefer you could wrap the whole thing within a function. I prefer to have these separately. The result is presented in `tibble` format:

```{}
raw_dl <- ig_history(epic = "CS.D.GBPUSD.CFD.IP", mkt.name = "GBP/USD", 
                          from = "2018-07-02", to = "2018-07-03", res = "DAY")

ohlc_dl <- get_ohlc(raw_dl, "ask")                    # you can change between "bid" or "ask" - default is set for "ask"
ohlc_dl_rename <- get_ohlc_rename(raw_dl, "ask")     # this assigns mkt.name as prefix to ohlcv column names

```



Additionally, the `mkt.name` is set as the `attribute` name for the returned data. This is very handy to name within  `lists` to hold data for analysis. 

``` {}
epic_names <- read_csv("epic_names.csv")   # table with epic names and instrument names


raw_list <- map2(epic_names$epic, epic_names$instrumentName, ~ig_history(epic = .x, mkt.name = .y,
                                                                   from = "2018-07-01", to = "2018-07-03", 
                                                                   res = "DAY"))

names(raw_list) <- map_chr(raw_list, ~attr(.x, "name")) # This is to name the tibbles within the list


ohlc_list         <- map(raw_list, ~get_ohlc(.x))
ohlc_list_renamed <- map(raw_list, ~get_ohlc_rename(.x))  


# Check names
names(raw_list)
names(ohlc_list)
names(ohlc_list_renamed)
```


I download all my data in one go, so to speed-up your download use the `furrr` package, change `map2` to `future_map2`.

###Remaining Allowance
Everytime you download data a global object named `ig_remaining_allowance` is updated with the remaining weekly allowance. 



## Updating EOD Data
Two functions are available to update EOD data, `ig_eod` and `ig_eod_list`. As I hold my data in a list, I use the latter function. To use `ig_eod`, supply the `raw_dl` file to the function along with the `epic.code`. 

The list function is pretty simple, it assumes that you have a table containing `epic codes` and `mkt.name` as used in history download function. Usage of both are follows:


```{}

ig_eod <- (mkt.name,
to.date = (Sys.Date() + lubridate::ddays(1)),   # this is pre set to download up to the latest date
df.w.hist,                                      # previously download file from IG Markets using ig_history()
epic.code)                                      # epic name as character



ig_eod_list <- (mkt.name,
to.date = (Sys.Date() + lubridate::ddays(1)), # this is pre set to download up to the latest date
list.of.df,                                   # list containing your data
epic.table,                                   # this should be a df with 2 columns, epic and its name
epic.code.col_name = "epic",                  # these two epic.table field names 
epic.name.col.name = "instrumentName")



# This is how I use this function. All my historic data is held inlist "bb"
bbb <- map(names(bb), ~ig_eod_list(df.name = ., 
                                    list.of.df = bb,
                                    epic.table = epic_names))

names(bbb) <- purrr::map_chr(bbb, ~attr(.x, "name"))

```

rig's People

Contributors

joefernando avatar

Stargazers

 avatar

Watchers

 avatar

rig's Issues

Bad Request (HTTP 400)

Hi Joe!

Firstly, thanks for putting this code together - it gives me somewhere to start.

All I want to be able to do it login to my IG account and pull in the active positions.

I've tried your ig_login() function, but I get the following when using the correct login/password/api key:

Error in ig_login(b, api) : Bad Request (HTTP 400).

If I change the api key, I get:

Error in ig_login(b, api) : Forbidden (HTTP 403)

If I change the login name, I get:

Error in ig_login(b, api) : Unauthorized (HTTP 401).

The code I'm running is:

b   <- '{"identifier":"mylogin", "password": "mypassword"}'
api <- "myAPIkey"
ig_login(b, api) 

Any ideas will be appreciated! (I'm fairly new to R and HTML!)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.