如何解决在 TradingView 中打开/关闭 SMA
//@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",maxval = 12)
FromYear = input(defval = 2020,title = "From Year",minval = 1990)
ToDay = input(defval = 1,title = "To Day",maxval = 31)
ToMonth = input(defval = 1,title = "To Month",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 slowSMA == 0
longCondition = close > fSMA
else
longCondition = close > fSMA and close > sSMA
...
没用!有人有其他解决方案吗?
PS.:解释算法的操作:当价格收于慢 SMA 和快 SMA 上方时买入,并在最后 5 根蜡烛中的最大值卖出,有可能关闭慢 SMA 并仅使用一个 SMA。
解决方法
我在顶部添加了一个使用slowMA的输入:
library(dplyr)
library(ggplot2)
library(patchwork)
repeats<-c(1,2,3) # for the outer loop.
List <- list()
cylinder<-unique(mtcars$cyl)
#Loop 1:
for (pic in seq_along(repeats)) {
# loop 2:
for (value in seq_along(cylinder))
# same code:
m<-mtcars%>%
filter(cyl==cylinder[value])%>%
group_by (gear)%>%
summarise(number=n(),average=mean(mpg),se=sd(mpg))
print(m) # reporting the numbers
a<-m%>%
mutate(gear=factor(gear,levels=unique(gear)))%>%
ggplot()+
geom_bar(aes(x=gear,y=average),stat = 'identity',fill ='red') +
geom_errorbar( aes(x= gear,ymin=average-se,ymax=average+se),width=0.2,colour="black",alpha=1,size=1) +
xlab("gears") + ylab("average mpg") +
ggtitle (paste( "cyliner:",value ))+
theme(axis.ticks.x=element_blank())
print(a)
List[[value]]<-a}
#Wrap plots
wrap_plots(List,nrow = 1)
List[[value]]<-a }
#Wrap plots
wrap_plots(List,nrow = 1)
print (List) #reporter
List <- list()
print(List) #reporter
}
然后将longcondition语句改成这样:
useSlow = input(title="Use Slow MA",type=input.bool,defval = false)
如果我理解你的问题,我想这就是你要找的。或者,使用简单的 if 或三元组设置值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。