【问题标题】:Cannot call 'strategy.order' with arguments (id=literal string, stop=series[float]);无法使用参数调用“strategy.order”(id=literal string,stop=series[float]);
【发布时间】:2021-05-17 18:08:45
【问题描述】:

我正在尝试将追踪止损纳入策略,为此我使用了多个 strategy.exit 但它不起作用,所以我输入了strategy.order,但我一次又一次地遇到同样的错误
Error :
line 100: Cannot call 'strategy.order' with arguments (id=literal string, stop=series[float]); available overloads: strategy.order(series[string], series[bool], series[float], series[float], series[float], series[string], const string, series[string], series[bool], series[string]) => void

请帮我把尾随 sl 放在 stipt 中

脚本
//@版本=4 strategy("SAMPLE STRAT", overlay=true)

//RULES FOR BACKTEST TIME PERIOAD------------------------------------------------

startDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
     defval=2018, minval=1800, maxval=2100)

endDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
     defval=7, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
     defval=2020, minval=1800, maxval=2100)
     
//RULES FOR BACKTEST TIME PERIOAD END---------------------------------------------
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
         startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
     
     
ema = input(title="Small Ema", type=input.integer,
     defval=20)
     
ema2 = input(title="Big Ema", type=input.integer,
     defval=150)

sma20 = ema(close,ema)
sma50 = ema(close,ema2)

long = sma20 > sma50
short = sma20 < sma50

// Set stop loss level with input options (optional)    SL
longLossPerc = input(title="Long Stop Loss (%)",type=input.float, minval=0.0, step=0.1, defval=8) * 0.01

shortLossPerc = input(title="Short Stop Loss (%)",type=input.float, minval=0.0, step=0.1, defval=8) * 0.01

//SL
longStopPrice  = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)

//Backtesting Time Period
START = timestamp(startYear,startMonth,startDate,0,0,0)
END = timestamp(endYear,endMonth,endDate,0,0,0)
longcondition = crossover(sma20,sma50)
shortcondition = crossunder(sma20,sma50)
// if (crossover(mafast, maslow))
//  strategy.entry("MA2CrossLE", strategy.long, comment="MA2CrossLE")
// if (crossunder(mafast, maslow))
//  strategy.entry("MA2CrossSE", strategy.short, comment="MA2CrossSE")

// Configure trail stop level with input options (optional)
longTrailPerc = input(title="Trail Long Loss (%)",
     type=input.float, minval=0.0, step=0.1, defval=3) * 0.01

shortTrailPerc = input(title="Trail Short Loss (%)",
     type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
     
longStopPrice2 = 0.0

longStopPrice2 := if (strategy.position_size > 0)
    stopValue = close * (1 - longTrailPerc)
    max(stopValue, longStopPrice2[1])
else
    0

// Determine trailing short price
shortStopPrice2 = 0.0

shortStopPrice2 := if (strategy.position_size < 0)
    stopValue = close * (1 + shortTrailPerc)
    min(stopValue, shortStopPrice2[1])
else
    999999

if time >= START and time <=END
    strategy.entry("Buy", strategy.long, when = longcondition)
    strategy.entry("Sell", strategy.short, when = shortcondition)
    


if (strategy.position_size > 0)
    strategy.exit(id="XL STP", stop=longStopPrice)
    if(longStopPrice2)
        strategy.order(id="TL SL", stop=longStopPrice2)

if (strategy.position_size < 0)
    strategy.exit(id="XS STP", stop=shortStopPrice)
    if(shortStopPrice2)
        strategy.order(id="TL SL", stop=shortStopPrice2)

strategy.close("Buy", when = short)
strategy.close("Sell", when = long)

plot(sma20, title="fast", color=color.green, linewidth=2)
plot(sma50, title="Slow", color=color.red)

我一直在尝试这个,但我找不到任何解决方案,请帮我解决这个错误

谢谢

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    请参考参考手册:

    strategy.order(id, long, qty, limit, stop, oca_name, oca_type, comment, when, alert_message) → void
    

    long 是 strategy.order 订单调用的必需参数。 下单方向:“true”或“strategy.long”为买入,“false”或“strategy.short”为卖出。

    
    if (strategy.position_size > 0)
        strategy.exit(id="XL STP", stop=longStopPrice)
        if(longStopPrice2)
            strategy.order(id="TL SL", long = true, stop=longStopPrice2)
    
    if (strategy.position_size < 0)
        strategy.exit(id="XS STP", stop=shortStopPrice)
        if(shortStopPrice2)
            strategy.order(id="TL SL", long = true, stop=shortStopPrice2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多