【问题标题】:Stop Adding More Positions Once I have A Position On In Quantstrat R一旦我在 Quantstrat R 中有一个职位,就停止添加更多职位
【发布时间】:2019-10-30 03:42:11
【问题描述】:

我正在研究基本的 RSI 交易信号。当股票低于 20 RSI 时买入 100 股,当股票高于 80 RSI 时平仓。

发生的情况是,一旦股票跌破 20,我就买入 100 股,如果股票再次跌破 20 而没有先达到 80 RSI,我最终会再买入 100 股(总共 200 股)。

一旦我有了一个职位,我就不想再添加了。谢谢你。

rm.strat(portfolio.st)
rm.strat(strategy.st)
rm.strat(account.st)

#setup
Sys.setenv(TZ = "UTC")
stock.str = "AAPL"
currency('USD')
stock("AAPL", currency= "USD", multiplier = 1)

initDate = "2010-01-01"
startDate = "2011-01-01"
to = Sys.Date()
initEq = 100000

portfolio.st = account.st = strategy.st = 'rsi'

getSymbols("AAPL", from = initDate)

initPortf(portfolio.st, symbols = stock.str,
          initDate = initDate)
initAcct(account.st,
         portfolio.st,
         initDate = initDate, initEq = initEq)
initOrders(portfolio.st, initDate = initDate)
strategy(strategy.st, store = T)

add.indicator(strategy.st, 
              name = "RSI",
              arguments = list(
                    price = quote(Cl(mktdata)),
                    n = 14,
                    maType = "EMA"
              ),
              label = "rsi14")
add.signal(strategy.st,
           name = "sigThreshold",
           arguments = list(
                 column = "rsi14",
                 threshold = 20,
                 cross = T,
                 relationship = "lt"

           ),
           label = "crossBelow")
add.signal(strategy.st,
           name = "sigThreshold",
           arguments = list(
                 column = "rsi14",
                 threshold = 80,
                 cross = T,
                 relationship = "gt"
           ),
           label = "crossAbove")

add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(
               sigcol = "crossBelow",
               sigval = T,
               orderqty = 100,
               ordertype = "market",
               orderside = "long"

         ),
         type = "enter",
         label = "enter")
add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(
               sigcol = "crossAbove",
               sigval = T, 
               orderqty = "all",
               ordertype = "market",
               orderside = "long"),
         type = "exit",
         label = "exit"
         )
out = applyStrategy(strategy.st,
                    portfolio.st)

【问题讨论】:

    标签: r quantmod quantstrat


    【解决方案1】:

    您可以做的一件事是考虑编写自己的订单大小函数。这将作为参数传递给 add.rule 中的 quanstrat - osFUN

    类似这样的:

    my_sizing_fun <- function(data, timestamp, symbol, portfolio, ...) {
    
    
        equity <- getEndEq(strategy.st, Date = timestamp) 
    
        # Get current Position and return 0 if we already have a position
        pos <- getPosQty(portfolio, symbol, timestamp) 
        if(pos != 0) {
          return(0)
        } else {
    
        return(100)
        }
    

    如果你已经有一个位置,这将返回 0,否则返回 100。您可以在定单大小函数中执行一些相当复杂的操作,以帮助增强您的策略。

    现在只需在add.rule 中添加osFUN=my_sizing_fun 作为参数并删除orderqty,您就应该准备好了。

    当您开始想尝试做空或处理当前的股权价值时,以下是一个有用的示例:

    ### Order Size Function ### 
    ## Calculates Order Size as a percent risk of account equity - currently does not account for multiple symbols or a max trade size like we may implement in real life
    ## Takes in arguments passed from 'RuleSignal'
    ## riskPct is the percentage of account equity to risk per trade
    ## Returns the order quantity as a numeric value
    ## to do - round order qty to lot sizes
    osATR <- function(data, orderside, timestamp, symbol, portfolio, riskPct, ...) {
    
      # Update Accounts and get the Ending Equity
      updatePortf(strategy.st)
      updateAcct(strategy.st)
      updateEndEq(strategy.st)
    
      equity <- getEndEq(strategy.st, Date = timestamp) 
    
      # Get current Position and return 0 if we already have a position
      pos <- getPosQty(portfolio, symbol, timestamp) 
      if(pos != 0) {
        return(0)
      }
    
    
      # Calculate Order Quantity
      qty <- # Add your logic here
      qty <-  as.numeric(trunc(qty)) # return a numeric and round the qty
    
      # Long / Short Check & Set
      if(orderside == "short") {
        qty <- -qty
      } 
    
      # Make sure not to end up with net positions on the wrong side
      if (orderside == 'long' && qty < 0 | orderside == 'short' && qty > 0) {
        stop("orderqty is of the wrong sign")
      }
    
      return(qty)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 2020-06-19
      • 1970-01-01
      相关资源
      最近更新 更多