我正在使用pine编辑器制定策略,即当绿色的D线跌破17.5时买入,然后在D线超过78时卖出。从图表中可以看出,我应该买入卖了几次,但只买了一次,什么也没做。我似乎无法弄清楚我所缺少的。代码的底部是我告诉该策略并在该策略中出售的位置。谢谢
strategy("stochastic")
length = input(21, minval=1, title="length")
rsilength = input(21, minval=1, title="rsi length")
smoothk = input(4, minval=1, title="smoothk")
smoothd = input(10, minval=1, title="smoothd")
rsi = rsi(close, rsilength)
sto = stoch(close,highest(length),lowest(length), length)
K = sma(sto,smoothk)
D = sma(K,smoothd)
//plot(rsi, title="rsi", color=color.black)
plot(D, title="%D",color=color.green)
plot(K, title="%K",color=color.red)
hline(78,title="upper limit", color=color.red)
hline(17, title="lower limit",color=color.blue)
//plot(sto, title = "sto",color=color.black)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 5, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
// === EXECUTION ===
shares = 10000/close
buy = crossunder(D,17.5)
sell = crossover(D,78)
strategy.entry("buy", shares, when = window() and buy) // buy long when "within window of time" AND crossover
strategy.close("sell", when = window() and sell) // sell long when "within window of time" AND crossunder
这应该买卖两次,如图中突出显示。 https://ibb.co/37L0wSK这是链接,很抱歉,我没有足够的代表来发布图像。
答案 0 :(得分:0)
您的strategy.close()
呼叫与strategy.entry()
呼叫使用的订单ID不同。我在最后3行中添加了标记(您可以通过输入将其关闭)以显示满足条件的位置。
注意:请确保您在脚本中包含第一行 传输Pine脚本。这是指定的编译器指令 代码使用的是哪个版本的Pine,因此很重要。
//@version=4
strategy("stochastic")
length = input(21, minval=1, title="length")
rsilength = input(21, minval=1, title="rsi length")
smoothk = input(4, minval=1, title="smoothk")
smoothd = input(10, minval=1, title="smoothd")
showMarkers = input(true, "Show Markers")
rsi = rsi(close, rsilength)
sto = stoch(close,highest(length),lowest(length), length)
K = sma(sto,smoothk)
D = sma(K,smoothd)
//plot(rsi, title="rsi", color=color.black)
plot(D, title="%D",color=color.green)
plot(K, title="%K",color=color.red)
hline(78,title="upper limit", color=color.red)
hline(17, title="lower limit",color=color.blue)
//plot(sto, title = "sto",color=color.black)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 5, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
// === EXECUTION ===
shares = 10000/close
buy = crossunder(D,17.5)
sell = crossover(D,78)
strategy.entry("buy", shares, when = window() and buy) // buy long when "within window of time" AND crossover
strategy.close("buy", when = window() and sell) // sell long when "within window of time" AND crossunder
plotshape(showMarkers and buy, "buy", shape.triangleup, location.bottom, color.green, 0, text = "buy", size = size.tiny)
plotshape(showMarkers and sell, "sell", shape.triangledown, location.top, color.maroon, 0, text = "sell", size = size.tiny)
bgcolor(showMarkers ? window() ? color.green : color.maroon : na)