-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQuantStrat_IB.R
143 lines (121 loc) · 3.35 KB
/
QuantStrat_IB.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
142
143
############BEGIN###########
#clear workspace (for testing)
rm(list = ls())
#load libraries
library(IBrokers)
library(tawny)
library(quantmod)
library(PerformanceAnalytics)
library(blotter)
library(FinancialInstrument)
try(library(twsInstrument))
try(library(quantstrat))
#Reset TWS
tws<-twsConnect()
#clear portfolio and acct
suppressWarnings(rm("account.stocky","portfolio.stocky",pos=.blotter))
suppressWarnings(rm("order_book.stocky",pos=.strategy))
suppressWarnings(rm(stocky))
#Initialize contract
EURUSD<-getContract("EUR.USD")
############ Get Data ###############
EURUSD<-reqHistoricalData(tws,EURUSD,barSize='15 mins',duration ="20 D", whatToShow="BID")
#close tws
twsDisconnect(tws)
#Set Initial Date and Equity
initDate = start(EURUSD)
initEq = 10000
#Set up currencies
currency("EUR")
currency("USD")
#loop to make exchange rates
symbols =("EURUSD")
for(symbol in symbols) {
exchange_rate(symbol, currency="USD")
}
#Clean up Data from IB
EURUSD<-subset(EURUSD, select= -c(EUR.USD.WAP,EUR.USD.hasGaps,EUR.USD.Count,EUR.USD.Volume))
################## Set up portfolio orders and Acct #################
initPortf(name="stocky",symbols,initPosQty=0,initDate=initDate,currency="USD")
initAcct("stocky",portfolios="stocky",initDate=initDate,initEq=initEq)
initOrders("stocky",symbols,initDate=initDate)
#position limits
addPosLimit("stocky","EURUSD",timestamp=initDate,maxpos=20000, minpos=-20000)
#Set up Strategy
stratstocky<-strategy("stocky")
##############################FUNCTIONS#######################
# PUT YOUR CUSTOM Indicator here
movingavgret<-function(x,n){
step1<-ROC(x)
step2<-SMA(step1,n)
return(step2)
}
########################indicators#############################
stratstocky<-add.indicator(
strategy = stratstocky,
name = "movingavgret",
arguments = list(
x = quote(Cl(mktdata)),
n = 30),
label = "movavg")
################################### Signals ###############
stratstocky<-add.signal(
strategy = stratstocky,
name = "sigThreshold",
arguments = list(
threshold = 0,
column = "movavg",
relationship = "gte",
cross = TRUE),
label = "movavgPOS")
stratstocky<-add.signal(
strategy = stratstocky,
name = "sigThreshold",
arguments = list(
threshold = 0,
column = "movavg",
relationship = "lt",
cross = TRUE),
label = "movavgNEG")
################################## Rules ##################
#Entry Rule
stratstocky<- add.rule(stratstocky,
name = "ruleSignal",
arguments = list(
sigcol = "movavgPOS",
sigval = TRUE,
orderqty = 20000,
ordertype = "market",
orderside = "long",
pricemethod = "market",
replace = FALSE,
TxnFees = -2.50,
osFUN = osMaxPos),
type = "enter",
path.dep = TRUE,
label = "Entry")
#Exit Rules
stratstocky <- add.rule(stratstocky,
name = "ruleSignal",
arguments = list(
sigcol = "movavgNEG",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "long",
pricemethod = "market",
replace = FALSE,
TxnFees = -2.50),
type = "exit",
path.dep = TRUE,
label = "Run AWAY!")
############################## Apply Strategy ##########################
out <- applyStrategy(strategy=stratstocky, portfolios="stocky")
updatePortf("stocky")
######################### Portfolio Return Characterics #################
#get portfolio data
portRet <- PortfReturns("stocky")
portRet$Total <- rowSums(portRet, na.rm=TRUE)
charts.PerformanceSummary(portRet$Total)
tradeStats("stocky")[,c("Symbol","Num.Trades","Net.Trading.PL","maxDrawdown")]
chart.Posn("stocky","EURUSD")