I am thinking about using R and quantstrat for backtesting some strategies. I have looked at some documentation and youtube videos to find out if it is possible to do what I want. I am completely new to R and I am willing to dive into the necessary documentation but I would like to get a hint if what I want is even possible.
I have worked through some examples for backtesting with quantstrat and what is common to all of them is that they use indicators which are calculated by R functions based on the price data
However what I need to use is a couple of indicators that have been externally calculated and which I have as true/false information along with the price data. So I have a number of additional columns in a CSV that contains OHLC data and the values of those columns is false most of the time but it is true on some rows. I want to generate signals which for example use two of those columns and say "if both are true, place a stop buy on the high of the last day with a stop loss at the low of the last day and calculate a position size so that the maximum loss is a fixed amount of money based on high-low difference.
There are many more rules to be applied then about when to close the position in profit, when to adjust the stop loss or when to cancel a not triggered stop buy order and i have also to deal with situations where the next days open is already above the stop buy price but thats another story. Also that I have to think about how to backtest an End of day strategy having only End of Day data (the next days candle might high both the stop buy and stop sell and than its ambigious what would have happened)
To start with I would like to know if its possible to include such external indicator data as sources for the quantstrat strategy
It would help me if you say yes or no and maybe point me to applicable documentation. Thanks for your advice!
Edit:
I actually made up now a CSV file with OHLC columns and just added another column with ones and zeroes like this very simplistic example:
Date,Open,High,Low,Close,SRot
2016-02-01,28,31,20,20,0
2016-01-29,34,35,30,31,1
2016-01-28,22,30,21,28,0
2016-01-27,18,23,17,20,0
2016-01-26,30,32,20,25,0
2016-01-25,30,35,25,32,1
I got this into an xts object and did the setup of a quantstrat strategy which I managed to fire stop limit short orders after those days where the last column is 1.
add.rule(strategy.st,name = "ruleSignal",
arguments=list(sigcol="srotxsignal",
sigval=TRUE,
ordertype="stoplimit",
orderside="short",
replace=FALSE,
prefer="Low",
orderqty=10,
tradeSize=tradeSize),
type="enter",path.dep=TRUE,label="normalEntryShort")
Having the first signal already on January 25th on the first candle, with a low of 25, I expect the system to fire a sell stop limit order with a price level of 25. This is also what the orderbook seems to show. However, the system actually sells at a level of 30 on the next day which is the open price. For the second signal on the second last day, an order is fired, but never executed. This might be correct, since the open is already below the stop limit level to start with, however the price goes up as far as 31 on that last day so the order should have been triggered.
I guess that for the second signal I need to have much more logic programmed, but for the first one I have no idea why the order executes at 30.
out<-applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
[1] "2016-01-26 00:00:00 adidas 10 @ 30"
getOrderBook(portfolio.st)
$dshort
$dshort$adidas
Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime Prefer
2016-01-25 "10" "25" "stoplimit" "short" NA "closed" "2016-01-26 00:00:00" "Low"
2016-01-29 "10" "30" "stoplimit" "short" NA "open" NA "Low"