-
Notifications
You must be signed in to change notification settings - Fork 1
/
ibg_get_fx_quotes.R
63 lines (48 loc) · 1.72 KB
/
ibg_get_fx_quotes.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
###############################################################################
# This file is an example of pulling live data from IB thu the Gatway
# The data is analyzed for mean, slope and clustered with mclust
# The data is then continoususly plotted
###############################################################################
# Load the required libraries
#library(IBrokers)
#library(twsInstrument)
#library(mclust)
# Connect to the IBgateway client
#tws <- ibgConnect()
# clear price array fi necessary
#price <- vector()
# Define the forex insturments (if empty then only majors are used)
#define_FX()t
# Loop to recieve data from IB
while(x<-1){
# May be nenessary as as IB has quote limits
Sys.sleep(1)
# Retrieve the current instrument(s) quote
fx <- get_quote(ls_twsInstruments())
#plot((fx[3,2]),ylim=c(1.349,1.359))
# Print the colums one and two of the fourth row (GBP/JPY)
print(fx[4,1])
print(fx[4,2])
# Append the new quote to the active array
price <- c(price,fx[4,2])
# Plot the price action
plot(index(price),price,type="l")
#lines(index(price),price)
# Calculate horizontal mean line and plot
mdl1 <- lm(coredata(price) ~ 1)
abline(mdl1, col='Blue', lwd=2)
# Calulcate the slope Line and plot
try(mdl2 <- lm(coredata(price) ~ index(as.numeric(index(price)))))
try(abline(mdl2, col='Red', lwd=1))
# Cluster model
shrink <- 1
try(mdl3 <- Mclust(price, prior = priorControl(functionName="defaultPrior", shrinkage=shrink)))
try(summary(mdl3, parameters = TRUE, classification = F))
# Plot all of the caluclated clusters
try(
for(i in 1:length(mdl3$parameters$mean)) {
abline(mdl3$parameters$mean[i],0, col=i+2)
}
)
}
## End(Not run)