好吧,如果你仔细观察,你确实有一笔交易。进入条件已于 2000-04-24 满足,价格为 12.80。退出条件仍然是Open,这意味着您的退出条件尚未满足,您仍然做多。
我将通过向您展示如何调试来尝试向您展示您的策略发生了什么。
首先,让我们将您的策略转换为指标。我总是从一个指标开始,然后当我对它感到满意时将其转换为策略。
为了显示我们是否有买入信号或卖出信号,我们将使用plotshape()
函数和一些附加变量。
//@version=4
study("Darshan 52 week low", overlay=true)
var isLong = false // Flag to see if we are currently long
var isShort = false // Flag to see if we are currently short
var highAfterPurchase = 0.0
// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)
buySignal = not isLong and (close < weekly_lc) // Buy only if not already long
highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1])) // If we are already long, update highAfterPurchase with the current close
// else, keep the old value
// is close price 15% lesser than high then exit
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)
sellSignal = not isShort and (close < closeHighDelta) // Sell only if not alread short
if (buySignal) // Reset signals
isLong := true
isShort := false
if (sellSignal) // Reset signals
isLong := false
isShort := true
plotshape(series=buySignal, text="BUY", style=shape.triangleup, color=color.green, location=location.belowbar, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, color=color.red, location=location.abovebar, size=size.small)
现在,为了调试它,我们将创建另一个指标,唯一的区别是,overlay=false
和不同plot
的 s。在这里,我们想要绘制我们感兴趣的信号。我们想要查看其值的信号。
//@version=4
study("Darshan 52 week low Debug", overlay=false)
var isLong = false // Flag to see if we are currently long
var isShort = false // Flag to see if we are currently short
var highAfterPurchase = 0.0
// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)
buySignal = not isLong and (close < weekly_lc) // Buy only if not already long
highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1])) // If we are already long, update highAfterPurchase with the current close
// else, keep the old value
// is close price 15% lesser than high then exit
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)
sellSignal = not isShort and (close < closeHighDelta) // Sell only if not alread short
if (buySignal) // Reset signals
isLong := true
isShort := false
if (sellSignal) // Reset signals
isLong := false
isShort := true
plot(series=close, title="close", color=color.green, linewidth=2)
plot(series=weekly_lc, title="weekly_lc", color=color.blue, linewidth=2)
plot(series=highAfterPurchase, title="highAfterPurchase", color=color.orange, linewidth=2)
plot(series=closeHighDelta, title="closeHighDelta", color=color.red, linewidth=2)
现在,您的买入条件是,当绿线(收盘)低于蓝线(weekly_lc)时,您的卖出条件是当绿线(收盘)低于红线(closeHighDelta)时。
如果您查看图表(如果您看不清楚,可以将其中一些从设置中隐藏),您的买入条件只会发生一次,而您的卖出条件永远不会变为TRUE
。所以,你总是LONG。
这是修改后的策略:
//@version=4
strategy("Darshan 52 week low", overlay=true)
// Time inputs that the strategy is going to apply on
FromMonth = input(defval = 01, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 01, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth = input(defval = 08, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 31, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2019, title = "To Year", minval = 2017)
// Time frame calculations
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"
var isLong = false // Flag to see if we are currently long
var isShort = false // Flag to see if we are currently short
var highAfterPurchase = 0.0
// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)
buySignal = not isLong and (close < weekly_lc) // Buy only if not already long
highAfterPurchase := iff(buySignal, close, nz(highAfterPurchase[1])) // If we are already long, update highAfterPurchase with the current close
// else, keep the old value
// is close price 15% lesser than high then exit
closeHighDelta = highAfterPurchase - (highAfterPurchase * 0.15)
sellSignal = not isShort and (close < closeHighDelta) // Sell only if not alread short
if (buySignal) // Reset signals
isLong := true
isShort := false
if (sellSignal) // Reset signals
isLong := false
isShort := true
strategy.entry(id="darshan-long", long=strategy.long, when=buySignal and window())
strategy.close(id="darshan-long", when=sellSignal and window())
:=
附带说明一下,当您想将值重新分配给变量时,您应该使用运算符。