我正在用松本脚本编写一个简单的策略,以便在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")
Strategy Tester屏幕如下:
答案 0 :(得分: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
。在这里,我们想要绘制我们感兴趣的信号。我们想要查看它们的值的信号。
//@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())
作为旁注,当您想将值重新分配给变量时,应使用:=
运算符。