We will test two trading strategies based on return runs.
In [1]:
# load libraries
library(quantstrat)
library(Quandl)
In [2]:
# define instruments
currency("USD")
stock("BIST", currency="USD", multiplier=1)
# get data
date_from = "2005-08-01"
date_to = "2016-05-25"
BIST<-Quandl("GOOG/INDEXIST_XU100", type="xts", start_date = date_from, end_date = date_to)
BIST<-na.omit(BIST)
BIST<-xts(coredata(BIST), as.POSIXct(time(BIST)))
In [3]:
# define strategy component names
portfolio_name = "investiphi"
strategy_trend = "trend_following"
strategy_mean = "mean_reversion"
account_trend = "account_trend"
account_mean = "account_mean"
# remove if defined before
rm.strat(portfolio_name)
rm.strat(strategy_trend)
rm.strat(strategy_mean)
rm.strat(account_trend)
rm.strat(account_mean)
# create .blotter and .strategy environments
.blotter<-new.env()
.strategy<-new.env()
# init portfolio and accoiunt in .blotter
init_eq <- 100000 # 100k
init_date <- as.character(as.Date(date_from) - 1)
initPortf(portfolio_name, symbols="BIST", initDate=init_date, currency="USD")
initAcct(account_trend, portfolios=portfolio_name, initDate=init_date, currency="USD", initEq = init_eq)
initAcct(account_mean, portfolios=portfolio_name, initDate=init_date, currency="USD", initEq = init_eq)
initOrders(portfolio_name, initDate=init_date)
In [4]:
# init strategies
strategy(strategy_trend, store=TRUE)
strategy(strategy_mean, store=TRUE)
In [5]:
# you can see whats inside
temp <- get("USD", envir=FinancialInstrument:::.instrument)
summary(temp)
In [6]:
consecutive_days<-function(days_pos,days_neg, stock, posneg = TRUE) {
#days_pos <- 4
#days_neg <- 4
#n_day_signals <- data.frame(positive = logical(length(time(stock))), negative = logical(length(time(stock))))
n_day_signals <- data.frame(sigcol = logical(length(time(stock))))
n_day_signals <- xts( n_day_signals, as.POSIXct(time(stock)) )
n_day_signals[1,1] <- NA
#Signal <- xts(c("Positive", "Negative"), as.POSIXct(time(BIST)))
sign_counter <- 1
sign_last <- -1
for (i in 2:length(time(stock))) {
sign_temp <- sign( as.numeric ( as.numeric( stock[i,4]) - as.numeric( stock[i-1,4]) ) )
if (sign_temp == sign_last) {
sign_counter <- sign_counter + 1
} else {
sign_counter <- 1
sign_last <- sign_temp
}
if (posneg) {
if (sign_counter == days_pos && sign_last == 1) {
n_day_signals[i,1] <- TRUE
} else {
n_day_signals[i,1] <- NA
}
} else {
if (sign_counter == days_neg && sign_last == -1) {
n_day_signals[i,1] <- TRUE
} else {
n_day_signals[i,1] <- NA
}
}
}
if (posneg == TRUE) {
return( n_day_signals$sigcol)
} else {
return(n_day_signals$sigcol)
}
}
In [7]:
add.signal(strategy_mean, name="consecutive_days",
arguments = list(days_pos = 4, days_neg = 4, stock=BIST, posneg=TRUE),
label="short"
)
add.signal(strategy_mean, name="consecutive_days",
arguments = list(days_pos = 4, days_neg = 4, stock=BIST, posneg=FALSE),
label="long"
)
add.signal(strategy_trend, name="consecutive_days",
arguments = list(days_pos = 4, days_neg = 4, stock=BIST, posneg=TRUE),
label="tlong"
)
add.signal(strategy_trend, name="consecutive_days",
arguments = list(days_pos = 4, days_neg = 4, stock=BIST, posneg=FALSE),
label="tshort"
)
In [8]:
order_qty = 1
In [9]:
# strategy_mean rules
add.rule(strategy_mean, name='ruleSignal',
arguments=list(sigcol='sigcol.short',
sigval=1,
orderside='short',
ordertype='market',
orderqty=-order_qty,
TxnFees=0,
replace=FALSE),
type='enter',
label='EnterShort'
)
add.rule(strategy_mean, name='ruleSignal',
arguments=list(sigcol='sigcol.long',
sigval=1,
orderside='long',
ordertype='market',
orderqty='all',
TxnFees=0,
replace=TRUE),
type='exit',
label='Exit2Long'
)
add.rule(strategy_mean, name='ruleSignal',
arguments=list(sigcol='sigcol.long',
sigval=TRUE,
orderside='long',
ordertype='market',
orderqty=order_qty,
TxnFees=0,
replace=FALSE),
type='enter',
label='EnterLong'
)
add.rule(strategy_mean, name='ruleSignal',
arguments=list(sigcol='sigcol.short',
sigval=TRUE,
orderside='short',
ordertype='market',
orderqty='all',
TxnFees=0,
replace=TRUE),
type='exit',
label='Exit2Short'
)
In [10]:
# strategy_trend rules
add.rule(strategy_trend, name='ruleSignal',
arguments=list(sigcol='sigcol.tshort',
sigval=1,
orderside='short',
ordertype='market',
orderqty=-order_qty,
TxnFees=0,
replace=FALSE),
type='enter',
label='EnterShort'
)
add.rule(strategy_trend, name='ruleSignal',
arguments=list(sigcol='sigcol.tlong',
sigval=1,
orderside='long',
ordertype='market',
orderqty='all',
TxnFees=0,
replace=TRUE),
type='exit',
label='Exit2Long'
)
add.rule(strategy_trend, name='ruleSignal',
arguments=list(sigcol='sigcol.tlong',
sigval=TRUE,
orderside='long',
ordertype='market',
orderqty=order_qty,
TxnFees=0,
replace=FALSE),
type='enter',
label='EnterLong'
)
add.rule(strategy_trend, name='ruleSignal',
arguments=list(sigcol='sigcol.tshort',
sigval=TRUE,
orderside='short',
ordertype='market',
orderqty='all',
TxnFees=0,
replace=TRUE),
type='exit',
label='Exit2Short'
)
In [11]:
#summary(get("mean_reversion", envir=.strategy))
In [12]:
# apply strategy
applyStrategy(strategy_mean, portfolio_name)
updatePortf(portfolio_name)
updateAcct(account_mean)
updateEndEq(account_mean)
applyStrategy(strategy_trend, portfolio_name)
updatePortf(portfolio_name)
updateAcct(account_trend)
updateEndEq(account_trend)
In [13]:
getEndEq(account_mean, date_to)
In [14]:
getEndEq(account_trend, date_to)
In [15]:
chart.Posn(portfolio_name, "BIST")