Pine script 尝试在脚本回溯测试中正确配置条目顺序

Pine script 尝试在脚本回溯测试中正确配置条目顺序,pine-script,quantitative-finance,Pine Script,Quantitative Finance,嗨,我正在用Pinescript编写一个脚本,用于对一个简单的随机RSI策略进行回溯测试 我已设置代码,以便在20>K>80时输入限制订单。限制顺序随每支蜡烛动态移动,并基于过去3支蜡烛的最低高点或最高低点 为了便于调试,我在图表上绘制了限制顺序和停止顺序。但当我运行回溯测试时,当买入/卖出条件为真时,我的订单以市场价格输入。我已经用limit和stop参数配置了strategy.entry(),但它们似乎没有效果 如何将我的输入订单配置为在买入/卖出条件下仅以限制订单价格输入,而不以市场价格输

嗨,我正在用Pinescript编写一个脚本,用于对一个简单的随机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)

/@version=3
策略(title=“随机RSI策略”,
shorttitle=“SRSI”,
叠加=真,
默认数量类型=策略。权益百分比,
默认数量值=1,
聚合=0)
调试=输入(title='Debug',deffal=1)
//**********************指示器设置******************************\\
//指标投入
smoothK=输入(标题='smoothK',defval=3,minval=1)
smoothD=输入(标题='smoothD',defval=3,minval=1)
lengthStoch=输入(标题='随机长度',定义值=14,最小值=1)
长度RSI=输入(标题=‘RSI长度’,定义值=14,最小值=1)
src=输入(关闭,title=“RSI源”)
超卖=输入(title='RSI超卖级别',deffal=20)
超买=输入(title='RSI超买水平',deffal=80)
//算法计算
rsi1=rsi(src,长度rsi)
K=sma(stoch(rsi1,rsi1,rsi1,长度stoch),smoothK)
D=sma(K,平滑D)
//绘制随机rsi
绘图(调试?na:K,颜色=aqua,title='K%'))
绘图(调试?na:D,颜色=橙色,title='D%')
//标绘RSI
//rsi1_color=rsi1<超卖?绿色:rsi1>超买?红色:蓝色//超买/卖出时改变rsi的颜色
//rsi1_切换=输入(title=“RSI开/关”,type=bool,deffal=true)
//绘图(rsi1\u切换?rsi1:na,颜色=rsi1\u颜色)
//h0=hline(超买,title='overbund')
//h1=hline(超卖,title='overseld')
//填充(h0,h1,颜色=紫色,传输=80,标题='fill')
//**********************资金管理***************************\\
交易价值=((strategy.netprofit+strategy.initial_capital)*输入(标题='%Equity per trade',defval=0.01))//这将占总股本的百分比,通常为1-3%
交易数量=交易价值/成交//应为执行买卖时的当前价格,具体取决于方向
利润=strategy.equity-(strategy.netprofit+strategy.initial_capital)//如果交易盈利,则为正值;如果交易亏损,则为负值
ticksize=10//10滴答每一美元
//**********************战略******************************\\
//买入条件=交叉低于(rsi1,超卖)
//卖出条件=交叉低于(rsi1,超买)
买入条件=K<超卖
卖出条件=K>超买
//****************************限价单***************************\\
手动限制回顾=输入(deffal=5,title='limit lookback')
限制\u回溯=手动\u限制\u回溯
limitbuffer=输入(defval=10,title='limitbuffer')
limit\u short=最高(低,limit\u lookback)-limitbuffer
limit_long=最低(高,limit_lookback)+limitbuffer
//**********************止损******************************\\
停止策略=输入(title='stop strategy',options=[1,2,3],deffal=1)
停止\回望=输入(title='stop lookback Period',deffal=3)
停止缓冲=输入(title='S1 StopBuffer',deffal=10)*syminfo.mintick
//止损策略#1使用手动输入
停止策略=1时停止损失
最高(高,停止\u回望)+停止\u缓冲区
止损策略=1时止损策略=1
最低(低,停止\u回望)-停止\u缓冲区
////止损策略#2使用平均真实范围
//否则
//如果停止策略==2
//atr(14)*尺寸
////止损策略#3:
//否则
//如果停止策略==3
//交易价值*(输入(标题='%Risk per trade',defval=1,minval=1))/100//计算止损的风险百分比
//**********************获利回吐******************************\\
利润策略=输入(title='利润策略',type=string,options=['P1',P2',P3'],deffal='P3')
//获利策略#3:使用相反信号
策略。结束(“卖出”,时间=(利润策略='P3'和买入条件))
策略。结束(“买入”,何时=(利润策略='P3'和卖出条件))
//**********************回测配置**************************\\
如果购买条件和时间>时间戳(2019、02、23、09、30)//和策略。位置大小<交易数量
//strategy.order(“买入”,strategy.long,limit=limit\u long,oca\u name=“买入条件”,oca\u type=strategy.oca.cancel)
strategy.entry(“买入”,strategy.long,limit=limit\u long,stop=stop\u loss\u long,when=strategy.position\u size==0)
//策略.订单(“购买”,策略.多头,数量=交易数量)
//strategy.exit(“买入-退出”、“买入”,利润=获利,亏损=止损)//损益以PIP计算
//策略.退出(“买入退出”,“买入”,限制=收盘+交易风险,停止=收盘-交易风险)//限制和停止是价格退出
其他的
策略。取消(“购买”)
如果销售条件和时间>时间戳(2019、02、23、09、30)//和策略。位置大小>交易数量
//strategy.order(“卖出”,strategy.short,limit=limit\u short,oca\u name=“买入条件”,oca\u type=strategy.oca.cancel)
strategy.entry(“卖出”,strategy.short,limit=limit\u short,stop=stop\u loss\u short,when=strategy.position\u size==0)
//策略.订单(“卖出”,策略.空头,数量=交易数量)
//策略退出(“卖出退出”,“卖出”,利润=获利,亏损=止损)
//策略.exit(“卖出-退出”、“卖出”,止损=收盘-交易风险,止损=收盘+交易风险)//止损和止损是价格退出
其他的
策略。取消(“出售”)
//调试
//绘图(调试?限制\u短:na,颜色=黄色,样式=阶梯线,标题=限制短)
//绘图(调试?限制长度:na,颜色=黄色,样式=步进线,标题=“限制长度”)
//绘图(调试?极限长度:
//@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)