在 TradingView 中打开 on/off 个 SMA
Turn on/off an SMA in TradingView
我有以下代码:
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Coded by dUdU
strategy("My Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=15000, process_orders_on_close=true)
// Settings
candleLookback = input(5, minval=1)
atrExitMultiplier = input(0.0, step=0.1)
slowSMA = input(90, minval=0)
fastSMA = input(10, minval=1)
sSMA= sma(close, slowSMA)
fSMA= sma(close, fastSMA)
// Calculation
inMarket = strategy.opentrades != 0
opened = strategy.opentrades[0] > strategy.opentrades[1]
// Range check
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 10, title = "From Month", minval = 1, maxval = 12)
FromYear = input(defval = 2020, title = "From Year", minval = 1990)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToYear = input(defval = 2021, title = "To Year", minval = 1990)
Start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
Finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
Timerange() =>
time >= Start and time <= Finish ? true : false
// Entry order
longCondition = close > fSMA and close > sSMA
if longCondition
strategy.entry("Long", true, comment="Entry",when=Timerange())
// Exit order
exitPrice = highest(candleLookback) + atr(candleLookback) * atrExitMultiplier
plot(longCondition or inMarket ? exitPrice : na, "Exit Price", color.white, style=plot.style_linebr, offset=1)
strategy.exit("Long", limit=exitPrice, comment="Exit")
plot(sSMA,"slow",#FFFFFF)
plot(fSMA,"fast",#EB4034)
我希望能够将数字“0”放入 slowSMA 中,这样我就可以禁用它并只使用 fastSMA。但是,当我输入“0”时出现错误!
我试过这样放置 'if':
...
if slowSMA == 0
longCondition = close > fSMA
else
longCondition = close > fSMA and close > sSMA
...
没有成功!谁有其他解决方案?
PS.: 解释算法的操作:当价格收于慢速 SMA 和快速 SMA 上方时买入,并在最后 5 根蜡烛的最大值处卖出,并可能关闭慢速 SMA 并且只使用一个 SMA。
我在顶部添加了一个使用 slowMA 的输入:
useSlow = input(title="Use Slow MA", type=input.bool, defval = false)
然后将 longcondition 语句更改为:
// Entry order
longCondition = iff(useSlow, close > fSMA and close > sSMA, close > fSMA)
if longCondition
strategy.entry("Long", true, comment="Entry",when=Timerange())
如果我理解你的问题,我想这就是你要找的。或者使用简单的 if 或三元设置值。
我有以下代码:
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Coded by dUdU
strategy("My Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=15000, process_orders_on_close=true)
// Settings
candleLookback = input(5, minval=1)
atrExitMultiplier = input(0.0, step=0.1)
slowSMA = input(90, minval=0)
fastSMA = input(10, minval=1)
sSMA= sma(close, slowSMA)
fSMA= sma(close, fastSMA)
// Calculation
inMarket = strategy.opentrades != 0
opened = strategy.opentrades[0] > strategy.opentrades[1]
// Range check
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 10, title = "From Month", minval = 1, maxval = 12)
FromYear = input(defval = 2020, title = "From Year", minval = 1990)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToYear = input(defval = 2021, title = "To Year", minval = 1990)
Start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
Finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
Timerange() =>
time >= Start and time <= Finish ? true : false
// Entry order
longCondition = close > fSMA and close > sSMA
if longCondition
strategy.entry("Long", true, comment="Entry",when=Timerange())
// Exit order
exitPrice = highest(candleLookback) + atr(candleLookback) * atrExitMultiplier
plot(longCondition or inMarket ? exitPrice : na, "Exit Price", color.white, style=plot.style_linebr, offset=1)
strategy.exit("Long", limit=exitPrice, comment="Exit")
plot(sSMA,"slow",#FFFFFF)
plot(fSMA,"fast",#EB4034)
我希望能够将数字“0”放入 slowSMA 中,这样我就可以禁用它并只使用 fastSMA。但是,当我输入“0”时出现错误!
我试过这样放置 'if':
...
if slowSMA == 0
longCondition = close > fSMA
else
longCondition = close > fSMA and close > sSMA
...
没有成功!谁有其他解决方案?
PS.: 解释算法的操作:当价格收于慢速 SMA 和快速 SMA 上方时买入,并在最后 5 根蜡烛的最大值处卖出,并可能关闭慢速 SMA 并且只使用一个 SMA。
我在顶部添加了一个使用 slowMA 的输入:
useSlow = input(title="Use Slow MA", type=input.bool, defval = false)
然后将 longcondition 语句更改为:
// Entry order
longCondition = iff(useSlow, close > fSMA and close > sSMA, close > fSMA)
if longCondition
strategy.entry("Long", true, comment="Entry",when=Timerange())
如果我理解你的问题,我想这就是你要找的。或者使用简单的 if 或三元设置值。