如何在策略中使用情节来研究松树脚本

时间:2021-02-20 09:28:06

标签: pine-script

我有这个策略脚本,我会把它转换成学习脚本。

//@version=4
strategy("SSL Channel Cross", overlay=true)
period=input(title="Period", defval=10)
len=input(title="Period", defval=10)
// From Date Inputs
fromDay = input(defval=16, title="From Day", minval=1, maxval=31)
fromMonth = input(defval=2, title="From Month", minval=1, maxval=12)
fromYear = input(defval=2021, title="From Year", minval=1970)
// To Date Inputs
toDay = input(defval=17, title="To Day", minval=1, maxval=31)
toMonth = input(defval=2, title="To Month", minval=1, maxval=12)
toYear = input(defval=2021, title="To Year", minval=1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate

smaHigh=sma(high, len)
smaLow=sma(low, len)
Hlv = 0
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh: smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh
    
plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)
    
longCondition = crossover(sslUp, sslDown)
shortCondition = crossunder(sslUp, sslDown)
    
if (longCondition)
    strategy.close("Short", strategy.long, when=time_cond)
    strategy.entry("Long", strategy.long, when=time_cond)
        
if (shortCondition)
    strategy.close("Long", strategy.long, when=time_cond)
    strategy.entry("Short", strategy.short, when=time_cond)

所以我改变了

策略(“SSL 通道交叉”,overlay=true)

study("SSL 通道交叉",overlay=true)

现在我需要转换这个

if (longCondition)
    strategy.close("Short", strategy.long, when=time_cond)
    strategy.entry("Long", strategy.long, when=time_cond)
        
if (shortCondition)
    strategy.close("Long", strategy.long, when=time_cond)
    strategy.entry("Short", strategy.short, when=time_cond)

显示进入/退出长文本或标签以及显示进入/退出短文本或标签

我尝试使用

  plotchar(time_cond,char='SHORT')
  plotchar(longCondition,char='SHORT')

但我总是收到错误消息。哪个是正确的方法?

1 个答案:

答案 0 :(得分:2)

如果你想输出文本,那么试试这个选项

plotshape(longCondition and time_cond, text='Long')