【问题标题】:Why does my strategy doesnt show any data?为什么我的策略没有显示任何数据?
【发布时间】:2020-02-29 03:57:16
【问题描述】:

我正在用 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")

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

【问题讨论】:

  • 会不会因为没有退出条件而没有得到任何结果?您是否在“交易列表”选项卡中看到零条目?
  • @BarisYakut 交易列表为零。我把退出条件也没有效果。
  • 不是,我已经为你准备了答案。

标签: pine-script back-testing


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试了您修改后的策略,但我没有得到一笔交易。我将销售标准从 15% 更改为 1%。
  • 您使用的是哪个时间段?
  • 我没有指定任何时间范围。老实说,我不知道如何指定时间范围。我删除了我的代码并粘贴了您的代码。作为我研究的一部分,我发现了一只应该立即产生卖出信号的股票。股票名称为 IRB Infrastructure。但我们的两种策略都未能发出退出信号。
  • 看看我的第一张截图。在最左上角的符号 (ITC) 旁边有一个 M。这表明图表的时间范围是每月。或者,您可以查看图表的左上线,您可以在其中看到条形图的开盘价、收盘价、最低价、最高价。 ITC- 1M - NSE。你在那里看到了什么?
  • 我有 W。我把它改成了 M,我得到了一笔多头交易。我为 IRB 使用了相同的配置和你的策略代码,它应该立即生成退出。我不确定我们的两个逻辑哪里出了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 2012-05-20
  • 2016-02-16
相关资源
最近更新 更多