尝试在 Pinescript 回测中正确配置入场订单

Trying to correctly configure entry orders in Pinescript Backtesting

您好,我正在用 Pinescript 编写脚本来回测简单的 StochasticRSI 策略。

我已经设置了当 20 > K > 80 时输入限价订单的代码。限价订单随每根蜡烛动态移动,并基于过去 3 根蜡烛的最低高点或最高低点。

为了便于调试,我在图表上绘制了限价单和止损单。但是当我 运行 回测时,当 buy/sell 条件为真时,我的订单以市场价格输入。我已经为 strategy.entry() 配置了 limit 和 stop 参数,但它们似乎没有效果。

如何配置我的挂单以仅在 buy/sell 条件下以限价订单价格入场,而不是市场价格?

//@version=3
strategy(title="StochasticRSI Strategy",
   shorttitle="SRSI",
   overlay = true,
   default_qty_type=strategy.percent_of_equity,
   default_qty_value=1,
   pyramiding=0)

Debug = input(title='DEBUG', defval=1) 

// ****************** INDICATOR SETUP ****************** \

// Inputs for indicators
smoothK = input(title = 'Smooth K', defval = 3, minval = 1)
smoothD = input(title = 'Smooth D', defval = 3, minval = 1)
lengthStoch = input(title = 'Stochastic Length', defval = 14, minval = 1)
lengthRSI = input(title = 'RSI Length', defval = 14, minval = 1)
src = input(close, title="RSI Source")
oversold = input(title='RSI Oversold Level', defval=20)
overbought = input(title='RSI Overbought Level', defval=80)

// Computation of algorithms
rsi1 = rsi(src, lengthRSI)
K = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
D = sma(K, smoothD)

// Plotting StochasticRSI
plot(Debug ? na : K, color = aqua, title='K%')
plot(Debug ? na : D, color = orange, title='D%')

// Plotting RSI
// rsi1_color = rsi1 < oversold ? green : rsi1 > overbought ? red : blue // change color of rsi if overbought/sold
// rsi1_toggle = input(title="RSI On/Off", type=bool, defval=true)
// plot(rsi1_toggle ? rsi1 : na, color=rsi1_color)
// h0 = hline(overbought, title='Overbought')
// h1 = hline(oversold, title='Oversold')
// fill(h0, h1, color=purple, transp=80, title='Fill')



// ****************** MONEY MANAGEMENT ****************** \
trade_value = ((strategy.netprofit + strategy.initial_capital) * input(title='% Equity per trade', defval=0.01)) // this will be a percentage of total equity normally 1-3%
trade_quantity = trade_value/close // Should be current price at the time of exectuion ask or bid depending on direction
profit = strategy.equity - (strategy.netprofit + strategy.initial_capital) // Positive if trade is in profit and negative if trade is at a loss
ticksize = 10 // 10 ticks for every dollar 



// ****************** STRATEGY ****************** \

// buy_condition = crossunder(rsi1, oversold)
// sell_condition = crossunder(rsi1, overbought)
buy_condition = K < oversold
sell_condition = K > overbought


// ****************** Limit Orders ****************** \

manual_limit_lookback = input(defval=5, title='Limit Lookback')
limit_lookback = manual_limit_lookback
limitbuffer = input(defval=10, title='Limit Buffer')
limit_short = highest(low, limit_lookback) - limitbuffer
limit_long = lowest(high, limit_lookback) + limitbuffer

// ****************** Stop Loss ****************** \

stop_strategy = input(title='Stop Strategy', options=[1, 2, 3], defval=1)
stop_lookback = input(title='Stop Lookback Period', defval=3)
stop_buffer = input(title='S1 StopBuffer', defval=10) * syminfo.mintick 

// Stop Loss Strategy #1 Use a manual input
stop_loss_short = if stop_strategy == 1
    highest(high, stop_lookback) + stop_buffer

stop_loss_long = if stop_strategy == 1
    lowest(low, stop_lookback) - stop_buffer

// // Stop loss strategy #2 Use Average true range
// else
//     if stop_strategy == 2
//         atr(14)*ticksize


// // Stop loss strategy #3: 
//     else
//         if stop_strategy == 3
//             trade_value*(input(title='% Risk per Trade', defval=1, minval=1))/100 // Percentage of risk for calculating stop


// ****************** Take Profit ****************** \

profit_strategy = input(title='Profit Strategy', type=string, options=['P1', 'P2', 'P3'], defval='P3')

// Take Profit Strategy #3: Use Opposite Signal
strategy.close("SELL", when=(profit_strategy =='P3' and buy_condition))
strategy.close("BUY", when=(profit_strategy =='P3' and sell_condition))


// ****************** BACKTESTING CONFIGURATION ****************** \

if buy_condition and time > timestamp(2019, 02, 23, 09, 30) // and strategy.position_size < trade_quantity 
    // strategy.order("BUY", strategy.long, limit=limit_long, oca_name="Buy condition", oca_type=strategy.oca.cancel)
    strategy.entry("BUY", strategy.long, limit=limit_long, stop=stop_loss_long, when=strategy.position_size == 0)
    // strategy.order("BUY", strategy.long, qty=trade_quantity)
    // strategy.exit("Buy Exit", "BUY",  profit=take_profit, loss=stop_loss) // profit and loss are computed in pips
    // strategy.exit("Buy Exit", "BUY",  limit=close + trade_risk, stop=close - trade_risk) // limit and stop are price exits
else
    strategy.cancel("BUY")


if sell_condition and time > timestamp(2019, 02, 23, 09, 30)  //and strategy.position_size > trade_quantity 
    // strategy.order("SELL", strategy.short, limit=limit_short, oca_name="Buy condition", oca_type=strategy.oca.cancel)
    strategy.entry("SELL", strategy.short, limit=limit_short, stop=stop_loss_short, when = strategy.position_size == 0)
    // strategy.order("SELL", strategy.short, qty=trade_quantity)
    // strategy.exit("Sell Exit", "SELL", profit=take_profit, loss=stop_loss)
    // strategy.exit("Sell Exit", "SELL",  limit=close - trade_risk, stop=close + trade_risk) // limit and stop are price exits
else
    strategy.cancel("SELL")


// DEBUG
// plot(Debug ? limit_short : na, color=yellow, style=stepline, title='Limit Short')
// plot(Debug ? limit_long : na, color=yellow, style=stepline, title="Limit Long")
// plot(Debug ? limit_long : na, color=yellow, style=stepline, title="Limit Long")

plot(buy_condition ? limit_long : sell_condition ? limit_short : na, color=yellow, style=stepline, title='LO Debug')
plot(buy_condition ? stop_loss_long : sell_condition ? stop_loss_short : na, color=red, style=stepline, title='SL Debug')
// plot(buy_condition ? take_profit_long : sell_condition ? take_profit_short : 0, color=green, style=stepline, title='TP Debug')
// StochRSI overlay
// plotchar(buy_condition ? 1 : 0, color=green, location=location.belowbar)
// plotchar(sell_condition ? 1 : 0, color=red)
bcol = sell_condition ? red : buy_condition ? green : na
bgcolor(bcol)

//@version=3
strategy("My Strategy", overlay=true)
condition = time > timestamp(2019, 1, 1, 0, 0)
limit_price=input(defval=100, type=integer)
strategy.entry("My Short Entry Id", strategy.long, limit=limit_price, when=condition)

我用代码TSLA检查了这个策略。直到我将输入中的值更改为 280,然后订单才在 2019 年 1 月 24 日填写。希望这会有所帮助。

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

例子

strategy(title = "simple gap strategy example")
strategy.entry("enter long", true, 1, when = open > high[1], limit = <limit order criteria>) // enter long by market if current open great then previous high
strategy.entry("enter short", false, 1, when = open < low[1], limit = <limit order criteria>) // enter short by market if current open less then previous low

您的策略将post指定价格的限价订单。