-3

I am trying to develop a strategy in quanstrat that buys when the QQQ is greater than the SMA 200 and sells when the SMA 200 is less than the QQQ. But there is a propblem with my buy and sell signals .

This is the error

Error in sigComparison(label = label, data = data, columns = columns[c(i, : comparison of more than two columns not supported, see sigFormula

Current code is below:

add.indicator(strategy=tr1.st2,name='SMA',
          arguments=list(x=quote(Cl(mktdata)),n=200),label='SMA 200')

Add Buying and Selling Signals

add.signal(strategy=tr1.st2,
name='sigThreshold',
arguments=list(threshold=200,column='SMA',relationship='lt'),
label='BuySignal')

add.signal(strategy=tr1.st2,
name='sigThreshold',
arguments=list(threshold=200,column='SMA',relationship='gt'),
label='SellSignal')

Add Enter and Exit Rules

addPosLimit(portfolio=tr1.st2,
symbol='QQQ',
timestamp=start.date,maxpos=10)

add.rule(strategy=tr1.st2,
name='ruleSignal',
arguments=list(sigcol='BuySignal',sigval=T,orderqty=10,
osFUN=osMaxPos,ordertype='market',orderside='long'),
type='enter',
label='EnterRule',enabled=T)

add.rule(strategy=tr1.st2,
name='ruleSignal',
arguments=list(sigcol='SellSignal',sigval=T,orderqty='all',ordertype='market',orderside='long',TxnFees=-6),
type='exit',label='ExitRule',enabled=T)

MDEWITT
  • 2,338
  • 2
  • 12
  • 23
  • 2
    Welcome to SO. You are getting a bunch of downvotes because you did not leave a reproducible example of your problem. For example you should include the packages you used (e.g. `library(quanstrat)`), a small data set that we could use, and the above code that will trigger an error. If that is there odds are someone can help you. – MDEWITT Sep 09 '19 at 17:54

1 Answers1

3

Before we jump in to the answer. You have a ton of spelling mistakes - In the title and in the question. You also did not produce a reproducible example. This means, I can not copy and paste what you have written into my console and test it out. Lastly, have you looked at the demos on the github quantstrat page? Or read tim trice book? Remember, people on this forum are professionals and will not waste their time answering a question that looks like little effort has been applied.

I would suggest you hit the edit button on the bottom and fix this up. Before Asking a question try to use the search function and find a similar answer.

Besides that, I have reluctantly provided the answer you are looking for.

library(quantstrat)
getSymbols("QQQ")
rm.strat("SMACross")
Sys.setenv(TZ = "UTC")
currency("USD")
ticker = "QQQ"
stock(ticker, currency  = "USD", multiplier = 1)
strat = "SMACross"

initPortf(strat, ticker)
initOrders(strat, ticker)
initAcct(strat, strat, initEq = 100000)

strategy(strat, store = T)

add.indicator(strat, "SMA", arguments = list(x = quote(Cl(mktdata)[,1]),
                                             n = 200),
              label = "SMA200")

add.signal(strat, "sigCrossover", arguments = list(columns = c("Close", "SMA200"),
                                                   relationship = "gte"),
           label= "Cross.FB")

add.signal(strat, "sigCrossover", arguments = list(columns = c("Close", "SMA200"),
                                                   relationship = "lt"),
           label = "Cross.FA")

add.rule(strat, "ruleSignal", arguments = list(sigcol = "Cross.FB",
                                               sigval = T, 
                                               ordertype = "market", 
                                               orderqty = 100, 
                                               orderside = "long"),
         type = "enter",
         label = "Enter2Long")

add.rule(strat, "ruleSignal", arguments = list(sigcol = "Cross.FA", 
                                               sigval = T, 
                                               orderqty = "all", 
                                               ordertype = "market",
                                               orderside = "long"),
         type = 'exit', 
         label = "Exit2Close")

out = applyStrategy(strat, strat)

updatePortf(strat, ticker)

chart.Posn(strat, ticker, TA = "add_SMA(n = 200)")

enter image description here

Jordan Wrong
  • 1,205
  • 1
  • 12
  • 32