-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
150 lines (91 loc) · 4.72 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
output: github_document
always_allow_html: yes
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE
)
```
# OptionsAnalytics
<!-- badges: start -->
[](https://travis-ci.org/zumthor86/OptionsAnalytics)
<!-- badges: end -->
The goal of OptionsAnalytics is to provide a high level interface for constructing and analysing options strategies using [IG Index](https://www.ig.com/uk/welcome-page) REST API
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("zumthor86/OptionsAnalytics")
```
## .Renviron file
Before using this package you need to obtain API keys from IG index. Its recommended to request one for your live account and demo account as the API limits are quite stringent, so once you have used up your allowance in your live account you can switch to the demo API key. Store these environment variables in an `.Renviron` file as follows:
```{r, purl=FALSE}
LIVE_IG_API_KEY="<your live API key>"
LIVE_IG_USERNAME="<your live API username>"
LIVE_IG_PASSWORD="<your live API password>"
LIVE_IG_HOST="api.ig.com"
DEMO_IG_API_KEY="<your demo API key>"
DEMO_IG_USERNAME="<your demo username>"
DEMO_IG_PASSWORD="<your demo password>"
DEMO_IG_HOST="demo-api.ig.com"
```
## Start IG session
To start an IG session:
```{r}
library(OptionsAnalytics)
initiate_ig_session(env = "LIVE")
```
## Understanding the IG Index API
All IG instruments are organized into a market hierarchy. For example, an Option on the SP500 has a child node for each option expiry and each of those expiries in turn could have a child node for a particular range of strike prices. The "leaf" nodes contain the actual tradable instruments.
IG uses an instrument ID known as an 'epic'. Unfortunately the epics dont always map neatly to option characteristics, and you wont know what the epic is even if you know exactly what option you are looking for. For this reason you can create a market environment that will make instrument lookup easier (and faster). Names of instruments in this environment have the form Expiry, Underlyer, Strike, Option type.
## Create market enviroment
```{r}
# Commodity options node_id = 166251
comm_mkts <- create_market_env(166251, pause = 1)
head(names(comm_mkts))
comm_mkts$`DEC-19 Gold Futures 1515 CALL`
```
## Strategy Object
There are three ways to create a strategy object:
### Instantiate new strategy from scratch
Using the market environment created you can look up the options you'd like to add to your strategy, and then specify the positions for each leg, as well as the resolution of the plots and amount of price history. Note that when creating a strategy this way, the most recent prices will be used as the opening prices for the strategy.
```{r }
gold_strat <- create_strategy(c(comm_mkts$`DEC-19 Gold Futures 1525 CALL`,
comm_mkts$`DEC-19 Gold Futures 1565 CALL`),
positions = c(1,-3),
resolution = "HOUR",
n_prices = 100)
```
### Load strategy from previous session
Strategies can be persisted as a json file and then loaded subsequently
```{r persist_strat}
tmp <- tempfile()
save_strategy(strategy = gold_strat, path = tmp)
gold_strat1 <- load_strategy(tmp)
identical(gold_strat, gold_strat1)
unlink(tmp)
```
### Using a live strategy
The `get_current_positions()` function will return a tibble of options legs. You can then pass the epics, positions and opening_prices to `create_strategy`
## Plot the strategy over time
`plot_strategy_prices` will provide an interactive plot of the strategy prices, the underlyer, as well as the greeks. Note that a negative price indicates that the strategy was constructed at a net credit, i.e. the value of premiums sold is greater than premiums bought.
```{r plot_strat, plotly=TRUE}
prices <- plot_strategy_prices(gold_strat)
```
## Plot strategy scenarios at a point in time
`plot_strategy_scenarios` allows you to evaluate the possible price of the strategy for different values of the underlyer and implied volatility at a chosen point in time.
```{r plot_scen, plotly=TRUE}
scenarios <- plot_strategy_scenarios(gold_strat,
scenario_datetime = gold_strat$legs[[1]]$expiry-lubridate::days(20))
```
## Plot strategy pnl
Assuming that all the legs have the same expiration, one can plot the expiration PnL
```{r, plotly=TRUE}
pnl <- plot_strategy_pnl(gold_strat)
```