尝试在Pinescript Backtesting中正确配置输入订单

问题描述 投票:0回答:1

您好我正在使用Pine Script编写一个脚本来回溯测试一个简单的随机RSI策略。

我已设置代码以在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)

quantitative-finance pine-script tradingview-api
1个回答
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日。希望,这将有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.