为什么我的策略没有显示任何数据?

Why does my strategy doesnt show any data?

我正在用 pine 脚本编写一个简单的策略,用于在 TradingView 中进行回测。逻辑很简单。如果今天的收盘价低于 52 周低点,则购买价值 10000 印度卢比的股票。我的代码如下所示:

//@version=4
strategy("Darshan 52 week low", overlay=true)

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

// is close/open/high/low is less than 52 week low
if (close < weekly_lc)
    // if yes buy INR 10000 worth of stocks
    quantityToBuy = 10000/close
    strategy.entry("long", strategy.long, quantityToBuy)

当 运行 对 NSE:ITC 股票时,这不会产生任何数据。我不确定为什么,也没有可用的调试器来逐行查看行为。我尝试绘制 weekly_lc 并且效果很好。

更新 1:我将整个脚本放在这里并带有退出条件。

    //@version=4
strategy("Darshan 52 week low", overlay=true)

// Get 52 week low value
weekly_lc = security(syminfo.tickerid,"W", lowest(close,52), lookahead=barmerge.lookahead_on)

highAfterPurchase=0

// is close/open/high/low is less than 52 week low
if (close <= weekly_lc)
    // if yes buy INR 10000 worth of stocks
    quantityToBuy = 10000/close
    strategy.entry("darshan-long", strategy.long, quantityToBuy)

    // Set the purchase price as high
    highAfterPurchase = close

if (close > highAfterPurchase)
    highAfterPurchase = close

// is close price 15% lesser than high then exit 
closeHighDelta = highAfterPurchase - highAfterPurchase * 0.15
if (close < closeHighDelta)
    strategy.exit("exit", "darshan-long")

策略测试器屏幕如下所示:

好吧,如果你仔细观察,你确实有一笔交易。入市条件已于 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。在这里,我们想要绘制我们感兴趣的信号。我们想要查看其值的信号。

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

附带说明一下,当您想要为变量重新赋值时,您应该使用 := 运算符。