-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of decoupling strategy and backtesting #140
Comments
Hi Simon, thanks. @braverock and the rest of the crew who made this code open source did the R and Trading community a massive service. To your specific question, the package is mainly for backtesting and research (although the package can be integrated with a live trading system). What you backtest is a strategy, stored as a strategy object. Its possible to perform a backtest without quanstrat, using just blotter. See this demo for an example. There are multiple quantstrat demos which you can use to benchmark with. In terms of viewing the results, there are several options. You can use perTradeStats(), DailyStats() or tradeStats(). There is pretty good documentation for all functions. The below example uses the maCross demo, which uses AAPL daily bar data. If you have bars with 1-min frequencies, you could use that instead. require(quantstrat)
demo("maCross")
perTradeStats('macross')
dailyStats('macross')
tradeStats('macross')
Not sure if this helps or not. Regards |
As @jaymon0703 already pointed out, Here is a minimal example of some random 1-minute 'Close' data and random entry and exit signals added as columns to the data. You would of course replace these with your own signals and close data. The strategy in this case just consists of two rules, one for entries and one for exits, to tell require(quantstrat)
strat.st <- "FAKESTRAT"
nsamples <- 30
len<- 600
rm.strat(strat.st)
# generate random 1-min returns from a Student's t distribution, and add drift
rnd.rets<-xts(((rt(len,6)/200)+.00024),order.by=as.POSIXct(x=60*1:len, origin='2021-07-01'))
# turn that into a price series
rnd.price<-100*exp(cumsum(rnd.rets))
colnames(rnd.price)<-'Close'
FAKE<-rnd.price
# now generate some random signal columns
FAKE$long<-0
FAKE$long[sample(1:nrow(FAKE),size = nsamples, replace=FALSE)]<-1
FAKE$short<-0
FAKE$short[sample(1:nrow(FAKE),size = nsamples, replace=FALSE)]<-1
currency("USD")
stock("FAKE",currency = "USD")
initPortf(strat.st, symbols="FAKE")
initEq<-100000
initAcct(strat.st, portfolios=strat.st, initEq=initEq)
initOrders(portfolio=strat.st)
strategy(name=strat.st,store=TRUE)
add.rule(strat.st,"ruleSignal",
arguments=list(sigcol="long",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long'),
type="enter",
label="enter"
)
add.rule(strat.st,"ruleSignal",
arguments=list(sigcol="short",
sigval=TRUE,
orderqty="all",
ordertype='market',
orderside='long'),
type="exit",
label="exit"
)
out<-applyStrategy(strat.st , portfolios=strat.st, verbose=TRUE)
updatePortf(strat.st)
chart.Posn(strat.st) |
Wonderful! I would not have had the time to get into that depth. But this will help a lot when comparing frameworks. I will let you know about the results as soon there is something to report. Cheers guys! |
Looking forward to the report. |
Hi - just a small question: Is it possible to get statistics in percent instead of absolute value? For example, if i want to know what is the equity and max_drawdown in percent without compounding? Compound is of course very important but also misleading due to increased leveraging (higher weight per trade over time). Any ideas how to compare to a fixed size investment amount per trade?
I really like your event driven backtest and thanks for your help! |
The short answer is no. You can of course divide the End.Equity and Max.Drawdown statistics by initEq. If you have a custom order sizing function which commits a fixed number of dollars to a trade, then it would be possible to back out the stats in percent terms. If not, then it would be more work. Re your last point, technically |
Hi guys, just some more information. Now I got 4 packages and we can sparsely compare the metrics. First of all I very happy that there is already a lot of constituency. Some questions remain though: 1.) The biggest difference so far is drawdown. Any ideas why this metric is all over the place?
For equity_cum I would have to run
Do you have an idea which framework to port next, how to achieve higher constituency or wehere to expand performance metrics? |
If you are looking to audit the statistics, you can step through the code in debug mode of course, or use a combination of
Not sure i understand your last question...are you asking if there is another backtesting package in R which you can compare with? If so, then no, unfortunately i do not. |
Okay perfect. I will look into blotter for the drawdown issue, the n_trade difference and if needed provide an repex. The feature request should be also for blotter and not for quantstrat? |
Yes, it would be for |
Hi guys,
very nice package! I am planning in comparing the most prominent open source backtesting packages and wonder whether it is possible to use only your backtest tool without strategies? Do you have an example how to input something like timestamps (minute), close (price) and action (buy/sell) and return a backtest dataframe?
Thanks for your help!
The text was updated successfully, but these errors were encountered: