Search code examples
rquantstrat

Close position on specifc month day


I'm writting a strategy in R using quantstrat that needs to close any open position at the 15th day for every month.

I've already tried using add.signal with sigTimespan, but it seems it only works with time, and not dates. Maybe I'm not passing the right data format to the it's timespan argument. It asks for a POSIXct based object, so I wrote it like this:

add.signal(strategy = strategy,
           name = 'sigTimestamp',
           arguments = list(timestamp = as.POSIXct('2016-11-15 17:00:00'))
           label = 'timestamp')

... and it doesn't work. And even if it did, the signal would be useful only for closing that month's position.

A possible solution would be to place a delayed order to close the position at the same timespan it's opened by using the delay or timespan arguments, but I also couldn't manage to make it work. Here's what I've tried:

add.rule(strategy = strategy, name = 'ruleSignal', arguments =
            list(sigcol = 'long',
                 sigval = TRUE,
                 orderqty = 'all',
                 ordertype = 'market',
                 orderside = 'long',
                 replace = FALSE,
                 prefer = preferredPrice,
                 timestamp = as.POSIXct('2016-11-15 17:00:00')),
         type = 'exit', label = 'Timestamp Position Close')

Any thoughts?

I'm running R 3.3.2 and quantstrat 0.9.1739 under Ubuntu 16.04.1 LTS with 5 minute data.


Solution

  • I've created a function that generates a trading signal at a specific timestamp and applied it on my OHLC data before calling quantstrat's applyStrategy function. That signal, together with an exit rule did the trick.