-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.R
128 lines (128 loc) · 3.47 KB
/
test.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
# ts_static$TSUpdateAccountDetail()
#
# #
# # Test account details
# #
# acc_info <- IBrokers::reqAccountUpdates(ts_static$ts_conn)
# acc1 <- acc_info[[1]]
# acc2 <- acc_info[[2]]
#
# TSProcessAccBal <- function(acc_bal){
# ##
# # convert info from list to data.frame
# res <- lapply(1:length(acc_bal), function(i, acc_bal1){
# tmp <- acc_bal1[[i]]
#
# tmp_df <- data.frame(
# Metric = names(acc_bal1)[i],
# Value = as.numeric(tmp['value']),
# Currency = tmp['currency'],
# stringsAsFactors = FALSE
# ) %>% dplyr::filter(!is.na(Value))
# return(tmp_df)
# }, acc_bal)
# res1 <- dplyr::bind_rows(res)
# return(res1)
# }
# port_info <- ProcessAccBal(acc1)
#
# TSProcessHolding <- function(holding){
# ##
# # convert info from list to data.frame
# res <- lapply(1:length(holding), function(i, holding1){
# tmp <- holding1[[i]]
# contract <- tmp$contract
# pv <- tmp$portfolioValue
#
# tmp2 <- cbind.data.frame(
# data.frame(t(unlist(contract)), stringsAsFactors = FALSE),
# data.frame(t(unlist(pv)), stringsAsFactors = FALSE)
# ) %>% dplyr::mutate(
# multiplier = ifelse(is.na(as.numeric(multiplier)), 1, as.numeric(multiplier))
# ) %>% dplyr::mutate(
# position = as.numeric(position),
# marketPrice = as.numeric(marketPrice)*multiplier,
# marketValue = as.numeric(marketValue),
# averageCost = as.numeric(averageCost),
# unrealizedPNL = as.numeric(unrealizedPNL),
# realizedPNL = as.numeric(realizedPNL)
# )
#
# return(tmp2)
# }, holding)
# res1 <- dplyr::bind_rows(res)
# return(res1)
# }
# holding <- ProcessHolding(acc2)
#
# #
# # Buy forex contract
# #
# cont <- twsCurrency(
# symbol = 'AUD',
# currency = 'CAD'
# )
# det <- IBrokers::reqContractDetails(ts_static$ts_conn, cont)
# trd_res <- placeOrder(ts_static$ts_conn,
# cont,
# twsOrder(orderId = reqIds(ts_static$ts_conn),
# action = 'buy',
# totalQuantity = 100,
# orderType = "MKT",
# transmit = TRUE,
# #goodTillDate = gtd,
# outsideRTH = "1"))
#
# ##
# # Test buy equity
# ##
# stk_blotter <- data.frame(
# Symbol = "RY",
# Right = "",
# Expiry = "20181221",
# Strike = "",
# Exchange = "",
# Action = "Buy",
# Quantity = 10,
# OrderType = "Mkt",
# LimitPrice = 20,
# `Security Type` = "FUT",
# Currency = "USD",
# TradeSwitch = TRUE,
# stringsAsFactors = FALSE,
# check.names = FALSE
# )
# res <- UtilTradeWithIB(stk_blotter)
#
# ##
# # Test buy forex
# ##
# forex_blotter <- data.frame(
# LocalTicker = "CAD",
# Right = "",
# Expiry = "",
# Strike = "",
# Exchange = "",
# Action = "",
# Quantity = 100,
# OrderType = "Mkt",
# LimitPrice = 20,
# SecurityType = "FOREX",
# Currency = "AUD",
# TradeSwitch = TRUE,
# stringsAsFactors = FALSE
# )
# res <- UtilTradeWithIB(forex_blotter)
#
# # Test cancel all trades
# res <- UtilCancelAllTrades()
#
# # Test get Econ indicators
#x <- UtilGetEconIndicators(ei_fred, ei_quandl)#
#x <- UtilGetMarketReturn(watchlist)
#x <- UtilGetStockLastestPrice("AAPL-USD")
#x <- UtilGetStockHistPrcAndRet("AAPL-USD")
#UtilPlotMarketReturn(x$cumret, "AAPL-USD", "1Y")
#UtilPlotMarketPrice(x$prc, "AAPL-USD", "1Y")
#x <- UtilGetPortfPerfor(c(acct_number1, acct_number2))
#y <- UtilGetPortfPerfor_OA()