-
Notifications
You must be signed in to change notification settings - Fork 12
/
api_orders_options.R
141 lines (109 loc) · 5.2 KB
/
api_orders_options.R
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
#' RobinHood API: Option Orders
#'
#' @param RH object of class RobinHood
#' @param action (string) one of "order", "status", "cancel", "history"
#' @param status_url (string) if action = "status", status_url is required (output from place_order_options())
#' @param cancel_url (string) if action = "cancel", cancel_url is required (output from place_order_options())
#' @param quantity (int) number of contracts you want to buy
#' @param direction (string) one of "debit" or "credit"
#' @param stop_price (numeric) stop price on a limit order
#' @param type (string) one of "limit" or "market"
#' @param time_in_force (string) Good Till Canceled ("gtc"), Immediate or Cancel ("ioc"), or Opening ("opg")
#' @param side (string) one of "buy" or "sell"
#' @param option_id (string) id column returned by get_contracts()
#' @import httr magrittr
#' @export
api_orders_options <- function(RH, action, status_url = NULL, cancel_url = NULL, quantity = NULL,
direction = NULL, stop_price = NULL, type = NULL, time_in_force = NULL,
side = NULL, option_id = NULL) {
# URL and token
url <- RobinHood::api_endpoints("option_orders")
token <- paste("Bearer", RH$api_response.access_token)
if (action == "history") {
# GET call
dta <- GET(url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token))
httr::stop_for_status(dta)
# format return
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.data.frame(dta$results)
dta <- dta %>%
dplyr::select(c("chain_symbol", "direction", "price", "premium", "processed_premium", "quantity", "processed_quantity",
"state", "time_in_force", "trigger", "type", "opening_strategy", "closing_strategy", "created_at",
"updated_at", "id")) %>%
dplyr::mutate_at(c("price", "premium", "processed_premium", "quantity", "processed_quantity"), as.numeric) %>%
dplyr::mutate_at(c("created_at", "updated_at"), lubridate::ymd_hms)
}
if (action == "order") {
# Unique ID required for each order
ref_id <- uuid::UUIDgenerate()
# Option URL
option <- paste(RobinHood::api_endpoints("option_instruments"), option_id, "/", sep = "")
# Generate body of the put call
detail <- data.frame(account = RH$url.account_id,
check.names = quantity,
direction = direction,
price = stop_price,
quantity = quantity,
type = type,
time_in_force = time_in_force,
trigger = "immediate",
ref_id = ref_id,
override_day_trade_checks = "false",
override_dtbp_checks = "false")
legs <- data.frame(side = side,
option = option,
position_effect = "open",
ratio_quantity = 1)
detail <- RobinHood::mod_json(detail, "toJSON")
detail <- substr(detail, 1, nchar(detail) - 1)
legs <- RobinHood::mod_json(legs, "toJSON")
# Structure of the detail required special formatting
detail <- jsonlite::prettify(paste(detail, ', "legs": [', legs, ']}'))
# Post call to place order
dta <- POST(url = url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token),
body = detail)
httr::stop_for_status(dta)
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.list(dta)
# Formatting return
dta$canceled_quantity <- as.numeric(dta$canceled_quantity)
dta$pending_quantity <- as.numeric(dta$pending_quantity)
dta$premium <- as.numeric(dta$premium)
dta$processed_premium <- as.numeric(dta$processed_premium)
dta$price <- as.numeric(dta$price)
dta$processed_quantity <- as.numeric(dta$processed_quantity)
dta$quantity <- as.numeric(dta$quantity)
dta$updated_at <- lubridate::ymd_hms(dta$updated_at)
dta$created_at <- lubridate::ymd_hms(dta$created_at)
# Create a staus_url to be consistent with other order functions
dta$status_url <- gsub("cancel/", "", dta$cancel_url)
}
if (action == "cancel") {
# Post call to place order
dta <- POST(url = cancel_url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token))
httr::stop_for_status(dta)
# Format return
dta <- RobinHood::mod_json(dta, "fromJSON")
}
if (action == "status") {
# GET call
dta <- GET(status_url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token))
httr::stop_for_status(dta)
# Format return
dta <- RobinHood::mod_json(dta, "fromJSON")
dta <- as.list(dta)
}
return(dta)
}