[R-SIG-Finance] Position Limits

John Klingensmith johnsk00 at gmail.com
Fri May 27 22:48:49 CEST 2016


Hi,
 I am relatively new to R and QuantStrat, etc. I am playing with a real
simple strategy that goes long when the market closes at its lowest close
in 3 days and reverses the position when the market closes at its highest
close in 3 day.  I am trying to limit the position size to 1 lot in either
direction by using osMaxPos and addPosLimit, but for whatever reason the
position limits are not getting imposed. I also read the "Long Enter
Position do not "block" Short Enter Orders" question and tried making a
custom function.

My questions are as follows:

1) I currently have a long entry order and a short entry order - do I need
to have dedicated exit orders? I am just looking to flip the position from
long to short, and the strategy is in the market 100% of the time.

2) Why would it keep trading if I am using osMaxPos and addPosLimt, as
follows:

#Position Limits
addPosLimit(portfolio.st, "GSPC", timestamp = initDate, maxpos = 1,
longlevels = 1, minpos = -1, shortlevels = 1)


#rules
add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longSig",
                          sigval=TRUE,
                          ordertype = "market",
                          orderside = "long",
                          prefer="PST", # PST is just an adjusted price
column i created
                          replace = FALSE,
                          orderqty = 1,
                          osFun =  "osMaxPos"
                          ),
         type = "enter")


add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "shortSig",
                          sigval=TRUE,
                          ordertype = "market",
                          orderside = "short",
                          prefer="PST", # PST is just an adjusted price
column i created
                          replace = FALSE,
                          orderqty = -1,
                          osFun = "osMaxPos"
                          ),
        type = "enter")

3) Can I just create a custom function and pass it to add.rule as follows:


#Custom Function
osMaxPosMR = function(data, timestamp, orderqty, ordertype, ordersize,
portfolio, symbol, ruletype, ...){
  maxpos = osMaxPos(data, timestamp, orderqty, ordersize, portfolio,
symbol, ruletype, ...)
  curpos = getPosQty(portfolio, symbol, timestamp)

  if(orderside == 'long' && curpos > 0)
    maxpos = 0

  if(orderside == 'short' && curpos < 0)
    maxpos = 0

  return(maxpos)
  print(curpos)
}

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longSig",
                          sigval=TRUE,
                          ordertype = "market",
                          orderside = "long",
                          prefer="PST",
                          replace = FALSE,
                          orderqty = 1,
                          osFun = "osMaxPosMR"
                          ),
         type = "enter")


add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "shortSig",
                          sigval=TRUE,
                          ordertype = "market",
                          orderside = "short",
                          prefer="PST",
                          replace = FALSE,
                          orderqty = -1,
                          osFun = "osMaxPosMR"
                          ),
        type = "enter")

4) I am using S&P index data; although you cannot actually trade the index,
it is a good proxy for what I am trying to test. When I download the index
data (symbol ^GSPC) the symbol stored is "GSPC" without the carrot;
therefore, I cannot simply use "symbols <- "^GSPC" and reference "symbols"
in my code as I would normally if it were an ETF. Can someone explain why
this is?

Thanks for any help.
Best,
 John

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list