我知道网上提供的常用解决方案,已经搜索过,但是我在条件块前面有选项卡,但它仍然有问题
有趣的是它是基于 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)
你的代码没问题,唯一的问题是 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)