首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Trading View中回测用Pine脚本编写的策略时,出现“No Data”错误的原因是什么?

在Trading View中回测用Pine脚本编写的策略时,出现“No Data”错误的原因是什么?
EN

Stack Overflow用户
提问于 2020-09-06 02:01:17
回答 1查看 397关注 0票数 1

我已经制定了一个策略,如果价格超过5分形(顶部和底部)的中间平均线,则进入多头,当价格超过5分形平均线时,进入多头。

当价格低于中间均线时,该策略做空,当价格超过5个底部分形平均线时,该策略做空。

即时消息当前收到无数据错误。有没有人能帮我破解我的代码问题?

代码语言:javascript
复制
strategy(title="Fractal Breakout Strategy", shorttitle="Fractal Breakout Long", overlay=true)
    
LookBack = input(title="Periods", defval=5, minval=1, maxval=10, type=input.integer)

th = high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0

bl = low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0

tot = th + bl
pl = abs(tot)>=1 ? 1 : 0

// plotarrow(pl==1 ? tot : na,colorup=green,colordown=red,offset=-2,maxheight=10)

lowline = (valuewhen(tot==1, low[2], 0) + (LookBack>1 ? valuewhen(tot==1, low[2], 1) : 0) + (LookBack>2 ? valuewhen(tot==1, low[2], 2) : 0) + (LookBack>3 ? valuewhen(tot==1, low[2], 3) : 0) + (LookBack>4 ? valuewhen(tot==1, low[2], 4) : 0) + (LookBack>5 ? valuewhen(tot==1, low[2], 5) : 0) + (LookBack>6 ? valuewhen(tot==1, low[2], 6) : 0) + (LookBack>7 ? valuewhen(tot==1, low[2], 7) : 0) + (LookBack>8 ? valuewhen(tot==1, low[2], 8) : 0) + (LookBack>9 ? valuewhen(tot==1, low[2], 9) : 0) /LookBack)

highline = (valuewhen(tot==-1, high[2], 0) + (LookBack>1 ? valuewhen(tot==-1, high[2], 1) : 0) + (LookBack>2 ? valuewhen(tot==-1, high[2], 2) : 0) + (LookBack>3 ? valuewhen(tot==-1, high[2], 3) : 0) + (LookBack>4 ? valuewhen(tot==-1, high[2], 4) : 0) + (LookBack>5 ? valuewhen(tot==-1, high[2], 5) : 0) + (LookBack>6 ? valuewhen(tot==-1, high[2], 6) : 0) + (LookBack>7 ? valuewhen(tot==-1, high[2], 7) : 0) + (LookBack>8 ? valuewhen(tot==-1, high[2], 8) : 0) + (LookBack>9 ? valuewhen(tot==-1, high[2], 9) : 0))/LookBack

MidLine = (highline+lowline)/2

// Risk management
inpTakeProfit   = input(defval = 1000, title = "Take Profit", minval = 0)
inpStopLoss     = input(defval = 200, title = "Stop Loss", minval = 0)
inpTrailStop    = input(defval = 200, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset  = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)

// === RISK MANAGEMENT VALUE PREP ===
// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.
useTakeProfit   = inpTakeProfit  >= 1 ? inpTakeProfit  : na
useStopLoss     = inpStopLoss    >= 1 ? inpStopLoss    : na
useTrailStop    = inpTrailStop   >= 1 ? inpTrailStop   : na
useTrailOffset  = inpTrailOffset >= 1 ? inpTrailOffset : na

Long = crossover(close, MidLine)
LongClose = crossunder(close, highline)
if (Long)
    strategy.entry(id="Long Entry", long=true, when=Long)
    strategy.close(id="Long Entry", when=LongClose)

short = crossunder(close, MidLine)
shortclose = crossover(close, lowline)
if (short)
    strategy.entry(id="Short Entry", long=false, when=short)
    strategy.close(id="short Entry", when=shortclose)
    
    // === STRATEGY RISK MANAGEMENT EXECUTION ===
    strategy.exit("LongClose", from_entry = "Long", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
    strategy.exit("shortclose", from_entry = "short", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
EN

回答 1

Stack Overflow用户

发布于 2020-09-09 17:59:09

尝试创建另一个专门用于退出的"if“参数。

之前:

代码语言:javascript
复制
if (short)
    strategy.entry(id="Short Entry", long=false, when=short) ///Pay attention, you typed two times "short" condition, one into if and the other into strategy.entry. 
    strategy.close(id="short Entry", when=shortclose)

之后:

代码语言:javascript
复制
if (short)
    strategy.entry("Short Entry",strategy.short)

if shortclose
    strategy.close("short Entry")

你没有得到任何数据,因为你说了“如果空头打开和关闭位置”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63756999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档