# Buy a random sample of call and put options on simulation environment
library(tidyverse)
library(httr)
theme_set(theme_minimal() + theme(legend.justification = c(1, 0), legend.position = c(1, 0)))
devtools::load_all()
# 24 hour token for simulation account
# https://www.developer.saxo/openapi/token/current#/lst/1650285935777
token24 <- "eyJhbGciOiJFUzI1NiIsIng1dCI6IkRFNDc0QUQ1Q0NGRUFFRTlDRThCRDQ3ODlFRTZDOTEyRjVCM0UzOTQifQ.eyJvYWEiOiI3Nzc3NSIsImlzcyI6Im9hIiwiYWlkIjoiMTA5IiwidWlkIjoibU0zV1o1YU1WTXwyZ201Zk95ckxrdz09IiwiY2lkIjoibU0zV1o1YU1WTXwyZ201Zk95ckxrdz09IiwiaXNhIjoiRmFsc2UiLCJ0aWQiOiIyMDAyIiwic2lkIjoiNjM5MDNkOThhODkwNGYxZWE3NTNlNjU4NzIyNzk5ZTMiLCJkZ2kiOiI4NCIsImV4cCI6IjE2NjI0MDIxMTUiLCJvYWwiOiIxRiJ9.ML75IVjSE81du7lRJLZiC-lObXsohGzr7H1tiL7jz8kg4_hDuRNq4RxGRtpfpxsqEbcPfNRc2fw-SchcRfZVxA"
token24 <- paste("Bearer", token24)
# Token for live environment
my_token <- authorize_live()
get_balance(my_token, live = TRUE)
# My info
sim_info <- get_client_info(token = token24, live = FALSE)
# My default key
sim_key <- sim_info$ClientKey[1]
# 0. reset demo balance:
get_balance(token24)
reset_sim_balance(token24, sim_key, new_balance = 1000000)
# Get existing positions
sim_po <- get_positions(token = token24)
liv_po <- get_positions(token = my_token, live = T)
# 1. Get list of options via instruments end point
sim_opt <- get_instruments(token = token24, live = FALSE, exchange_id = "EUREX", asset_type = "StockOption")
# Note, get_instruments return more stock options when using live evironment
# and the intersections differ by having different GroupId's
# Filter out assets in which we already have options
# (This may or may not be a good thing; we don't want to by the same option twice;
# we don't want to bet against ourselves. On the other hand maybe we do, if previous
# decisions were bad. Or we may want to repeat good decisions despite the extra fee.)
sim_opt <- sim_opt %>% filter(!Data.Description %in% sim_po$Data.DisplayAndFormat.UnderlyingInstrumentDescription)
# 2. Identifier field will contain Option Root ID (collection of related options). E.g.:
sim_root_ids <- sim_opt %>% slice_sample(n = sample_size) %>% pull(Data.Identifier)
# 3. Use root id to get (many) option spaces from contractoptionspaces end point:
specific_options <- sim_root_ids %>%
map_dfr(
~ get_optionspace(
token = token24,
live = FALSE,
client_key = sim_key,
option_root_id = .x
)
)
# Explore a bit:
# Many options are related:
specific_options %>% count(OptionRootId, sort = T)
# Does a common OptionRootId imply same underlying UIC? Yes.
specific_options %>%
count(OptionRootId, UnderlyingUic, sort = T) %>%
add_count(OptionRootId, UnderlyingUic, name = "combo") %>%
arrange(-combo)
# 5. get prices
sample_options <-
specific_options %>%
filter(TradingStatus == "Tradable") %>%
slice_sample(n = 2000)
sample_prices <- sample_options %>%
pull(Uic) %>%
map_dfr( ~ make_subscription(
token = token24,
live = FALSE,
uic = .x,
asset_type = "StockOption"
))
# Plot prices
sample_prices %>%
filter(!is.na(Snapshot.PriceInfo.Low)) %>%
mutate(Identical = Snapshot.PriceInfo.Low == Snapshot.PriceInfo.High) %>%
ggplot(aes(x = Snapshot.PriceInfo.Low,
y = Snapshot.PriceInfo.High,
color = Identical,
size = Snapshot.PriceInfoDetails.Volume)
) +
geom_abline(slope = 1, intercept = 0, alpha = 0.3) +
geom_point(alpha = 0.5) +
guides(size = "none", color = "legend")
sample_prices %>%
filter(!is.na(Snapshot.PriceInfo.Low)) %>%
mutate(Identical = Snapshot.PriceInfo.Low == Snapshot.PriceInfoDetails.LastTraded) %>%
ggplot(aes(x = Snapshot.PriceInfo.Low,
y = Snapshot.PriceInfoDetails.LastTraded,
color = Identical,
size = Snapshot.PriceInfoDetails.Volume)
) +
geom_abline(slope = 1, intercept = 0, alpha = 0.3) +
geom_point(alpha = 0.5) +
guides(size = "none", color = "legend")
# 6. Place order
good_options <- sample_prices %>%
filter(!is.na(Snapshot.PriceInfo.Low), Snapshot.PriceInfo.Low > 0)
my_orders <- good_options %>%
mutate(order_price = (Snapshot.PriceInfo.Low + Snapshot.PriceInfo.High) / 2,
token = token24,
live = FALSE,
buy_sell = "Buy",
order_type = "Limit",
to_open_close = "ToOpen",
uic = Snapshot.Uic,
asset_type = Snapshot.AssetType) %>%
select(token, live, uic, buy_sell, asset_type, order_type, order_price, to_open_close)
my_orders %>%
pmap(place_order)
# 6. View orders
ord <- get_orders(token = token24, live = FALSE)
cancel_all_orders(token = token24, live = FALSE, account_key = sim_key)
url <- "https://gateway.saxobank.com/sim/openapi/port/v1/positions/me/"
r <- httr::GET(url = url,
query = list(PriceMode = "RegularTradingHours"),
config = httr::add_headers(Authorization = token24))
http_status(r)
content(r)
temp <- content(r)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.