0

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

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

在此处输入图像描述

4

1 回答 1

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和不同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())

:=附带说明一下,当您想将值重新分配给变量时,您应该使用运算符。

在此处输入图像描述

在此处输入图像描述

于 2019-11-04T07:28:41.763 回答