输入“strategy”时出现 Pinescript 语法错误

Pinescript Syntax error at input 'strategy'

提问人:Arkadiusz Kulpa 提问时间:10/11/2023 更新时间:10/11/2023 访问量:35

问:

我知道网络上提供的常用解决方案,已经搜索过了,但是我在条件块前面有选项卡,但它仍然有问题enter image description here

有趣的是,它基于 YT 视频教程,并且可以在视频:D中使用https://www.youtube.com/watch?v=h-erJbnBj6A&t=46s

import TradingView/ta/5

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arek1313

//@version=5
strategy("Simple Pullback Strategy",
     overlay=true,
     initial_capital = 50000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100, //100% of balance invested on each trade
     commission_type = strategy.commission.cash_per_contract,
     commission_value = 0.005) //Interactive Brokers Rate 

//Get User Inpu
i_ma1          = input.int(title = "MA 1 Length", defval = 200, step = 10, group = "Strategy Parameters", tooltip = "Long-term MA")
i_ma2          = input.int(title = "MA 2 Length", defval = 10, step = 10, group = "Strategy Parameters", tooltip = "Short-term MA")
i_StopPercent  = input.float(title = "Stop Loss Percent", defval = 0.10, step = 0.1, group = "Strategy Parameters", tooltip = "Failsafe Stop Loss Percent Decline")
i_StartTime    = input.time(title = "Start Filter", defval = timestamp("01 Jan 1995 13:30 +0000"), group = "Time Filter", tooltip = "Start date & time to begin strategy")
i_EndTime      = input.time(title = "End Filter", defval = timestamp("1 Jan 2099 19:30 +0000"), group = "Time Filter", tooltip = "End date & time to stop searching")

//Get Indicator values
ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)

// Check Filter(s)
f_datefilter = time >= i_StartTime and time <= i_EndTime

//Check buy / sell conditions
var float buyPrice = 0

buyCondition   = close > ma1 and close < ma2 and strategy.position_size == 0 and f_datefilter
sellCondition  = close > ma2 and strategy.position_size > 0
stopDistance   = strategy.position_size > 0 ? ((buyPrice - close)/ close) : na
stopPrice      = strategy.position_size > 0 ? buyPrice - (buyPrice * i_StopPercent) : na
stopCondition  = strategy.position_size > 0 and stopDistance > i_StopPercent

// Enter positions 
if buyCondition
     strategy.entry(id="Long", direction=strategy.long)

if buyCondition[1]
     buyPrice := open

// Exit positions
if sellCondition or stopCondition
     strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=True" : ""))
     buyPrice :=na


// Draw pretty colours
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)

plot(ma1, color = color.blue)
plot(ma2, color = color.orange)
条件语句 语法错误 pine-script

评论


答:

1赞 SpaceX 10/11/2023 #1

您的代码很好,唯一的问题是 if 语句后面的缩进。

以下是更正后的版本:

import TradingView/ta/5

//@version=5
strategy("Simple Pullback Strategy",
    overlay=true,
    initial_capital = 50000,
    default_qty_type = strategy.percent_of_equity,
    default_qty_value = 100, //100% of balance invested on each trade
    commission_type = strategy.commission.cash_per_contract,
    commission_value = 0.005) //Interactive Brokers Rate 

//Get User Inpu
i_ma1          = input.int(title = "MA 1 Length", defval = 200, step = 10, group = "Strategy Parameters", tooltip = "Long-term MA")
i_ma2          = input.int(title = "MA 2 Length", defval = 10, step = 10, group = "Strategy Parameters", tooltip = "Short-term MA")
i_StopPercent  = input.float(title = "Stop Loss Percent", defval = 0.10, step = 0.1, group = "Strategy Parameters", tooltip = "Failsafe Stop Loss Percent Decline")
i_StartTime    = input.time(title = "Start Filter", defval = timestamp("01 Jan 1995 13:30 +0000"), group = "Time Filter", tooltip = "Start date & time to begin strategy")
i_EndTime      = input.time(title = "End Filter", defval = timestamp("1 Jan 2099 19:30 +0000"), group = "Time Filter", tooltip = "End date & time to stop searching")

//Get Indicator values
ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)

// Check Filter(s)
f_datefilter = time >= i_StartTime and time <= i_EndTime

//Check buy / sell conditions
var float buyPrice = 0

buyCondition   = close > ma1 and close < ma2 and strategy.position_size == 0 and f_datefilter
sellCondition  = close > ma2 and strategy.position_size > 0
stopDistance   = strategy.position_size > 0 ? ((buyPrice - close)/ close) : na
stopPrice      = strategy.position_size > 0 ? buyPrice - (buyPrice * i_StopPercent) : na
stopCondition  = strategy.position_size > 0 and stopDistance > i_StopPercent

// Enter positions 
if buyCondition
    strategy.entry(id="Long", direction=strategy.long)

if buyCondition[1]
    buyPrice := open

// Exit positions
if sellCondition or stopCondition
    strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=True" : ""))
    buyPrice :=na


// Draw pretty colours
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)

plot(ma1, color = color.blue)
plot(ma2, color = color.orange)
0赞 Arkadiusz Kulpa 10/11/2023 #2

是的,这是制表问题!!!

每个人都说“只需在窗口上按 Tab 键”,我做到了,但是由于某种原因,Windows 上的 Tab 会创建 5 个空格,而 Pine Script 预计会有 4 个空格。因此,实际上您必须手动输入 4 个空格,而不是按 Tab 键。

瞧!