尝试正确配置Pinescript回测中的输入顺序

时间:2019-02-26 22:29:06

标签: quantitative-finance pine-script tradingview-api

嗨,我正在用Pinescript编写脚本,以回测简单的StochasticRSI策略。

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

为便于调试,我在图表上绘制了限价单和止损单。但是,当我进行回溯测试时,当买入/卖出条件成立时,我的订单就会以市场价格输入。我已经使用limit和stop参数配置了strategy.entry(),但是它们似乎没有作用。

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

//@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)

1 个答案:

答案 0 :(得分:1)

//@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日完成订单后,它才执行订单。希望对您有所帮助。