[R-SIG-Finance] questions about order price and timestamp in quantstrat
Ilya Kipnis
ilya.kipnis at gmail.com
Wed Sep 3 15:53:56 CEST 2014
RSI 50 and RSI 70 will do exactly what you want on one caveat--that if
the indicator waggles around 50 before reaching 70, you'll sell twice
at the 50 cross. Or you can use a chain type of order (see R/Finance
2013 quantstrat presentation), with a separate signal generating
column, using the 50 sell rule as a parent.
Regarding "rule execution on signal bar", there's an argument in
ruleOrderProc called allowMagicalThinking. However, that's not
recommended. If you wish to delay the SMA computations and add lag,
just write a custom indicator function.
lagSMA <- function(x, n=200, k=2) {
sma <- SMA(x, n=n)
out <- lag.xts(sma, k=k)
colnames(out) <- "lagSMA"
return(out)
}
Regarding pyramiding, what I'd suggest you look at is chain rules, and
as above, use your initial buy rule as a parent, but use a separate
signal column, so when you hit your RSI10, you'll also check the
profitability of your position from the RSI30, and if both conditions
hold, you should get an order. I don't use pyramiding code myself (and
this also seems like a contradictory rule--an RSI going from 30 to 10
probably means the price has gone down, not up).
Hope this helps.
-Ilya
On Wed, Sep 3, 2014 at 3:21 AM, domodo <1111938 at qq.com> wrote:
> hi,guys,I'm learning quantstrat and get some questions as below:
>
> 1. transaction's order occurs and price
>
> #add an indicator: monthly 5-sma
> add.indicator(strategy = mystrategy, name = "SMA", arguments = list(x =
> quote(Cl(mktdata)), n = 5), label = "SMA5")
>
> add.indicator(strategy = mystrategy, name = "SMA", arguments = list(x =
> quote(Cl(mktdata)[,1]), n = 10), label = "SMA10")
>
>
> #add signal: SMA5 cross over / under sma10
> add.signal(mystrategy, name = "sigCrossover", arguments = list(columns =
> c("SMA5",
> "SMA10"), relationship = "gt"), label = "SMA5.gt.SMA10")
>
> add.signal(mystrategy, name = "sigCrossover", arguments = list(columns =
> c("SMA5",
> "SMA10"), relationship = "lt"), label = "SMA5.lt.SMA10")
>
>
> #add rules
> #open long position
> add.rule(mystrategy, name = "ruleSignal", arguments = list(sigcol =
> "SMA5.gt.SMA10",
> sigval = TRUE, orderqty = 900, ordertype = "market", orderside = "long",
> pricemethod = "market"), type = "enter", path.dep = TRUE) #buy 900 shares
> #exit position
>
> add.rule(mystrategy, name = "ruleSignal", arguments = list(sigcol =
> "SMA5.lt.SMA10",
> sigval = TRUE, orderqty = "all", ordertype = "market", orderside = "long",
> pricemethod = "market"), type = "exit", path.dep = TRUE)
>
> according to the codes above, when sma5 cross above sma10, a trading signal
> occurs,and the order price is current bar's close price. it could be coded
> in tradestation's easylanguage as below:
>
> if average(close, 5) cross above average(close, 10) then
> buy this bar at close;
>
> if average(close, 5) cross under average(close, 10) then
> sell this bar at close;
>
> how if I want to set the next bar's open price as order price ? it means the
> codes in easylanguage as below
>
> if average(close, 5) cross above average(close, 10) then
> buy next bar at open;
>
> if average(close, 5) cross under average(close, 10) then
> sell next bar at open;
>
> my initial modification is:
> 1) make sma5 and sma10 delay a bar,that's,if sma5 of 2 bars ago is less than
> sma10 of 2 bars ago,and previous bar's sma5 is greater than previous bar's
> sma10,
> 2) then a signal occurs and an order with current bar's open price could be
> sent out.
>
> the codes below deal with point 1):
>
> #add signal: SMA5 cross over / under sma10
> add.signal(mystrategy, name = "sigCrossover", arguments = list(columns =
> c("SMA5"[1],
> "SMA10"[1]), relationship = "gt"), label = "SMA5.gt.SMA10")
> add.signal(mystrategy, name = "sigCrossover", arguments = list(columns =
> c("SMA5"[1],
> "SMA10"[1]), relationship = "lt"), label = "SMA5.lt.SMA10")
>
> but how to specify the order price as current bar's open price.
>
> 2. how to transfer timestamp from add.rule to ruleSignal
> timestamp is a argment of ruleSignal,but ruleSignal's documents doesn't give
> a detailed description,just says:
> timestamp
> timestamp coercible to POSIXct that will be the time the order will be
> inserted on.
>
> I don't quite catch on to what it is saying. further more,it seems that
> seldom examles in demo folder of quantstrat specifies this argument ?
>
> 3. how to code 'pyramiding' in quantstrat ?
>
> I have no idea about how to code 'pyramiding', it means the rule below,in
> quantstrat:
> 1) when RSI is less than 30, enter long with 300 shares of stock A,
> 2) when RSI is less than 10, and previous position is profitable, enter long
> with 100 more shares of stock A
> 3) when RSI is greater than 50, 200 shares of stock A exit
> 4) when RSI is greater than 70, rest 200 shares of stock A exit
>
> much appreciated if any guys give me some help about the question.
>
> regards.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/questions-about-order-price-and-timestamp-in-quantstrat-tp4696424.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list