我有一个基本的 Pine 脚本,下面提供供参考,并且有两个概念需要帮助澄清。
如何设置此策略的分辨率,以便我可以查看 1 年、5 年、年初至今等图表,但始终让策略以特定分辨率运行(例如每日柱线)。我已经对此进行了一些尝试,您可以在代码中看到注释掉,但我认为它不能正常工作;我想看看更有经验的人如何实现这一目标。
我想编写一个同时分析多种证券的策略,但是,我在 Tradingview 中找不到实现此目的的好方法。我以前使用过 Quantconnect,在该系统中,我可以选择我想要使用的任何数量的证券。例如,下面的策略的交易频率相对较低(大约每年四次),因此我想使用多种证券运行此策略,以减少停机时间。对我来说,无法运行分析和交易多种证券的策略是一个相当大的缺点。
我感谢您提供的所有帮助。下面提供了参考代码。
//@version=4
strategy(title="Basic Bollinger",
overlay=true,
default_qty_type=strategy.percent_of_equity,
default_qty_value=95)
// Strategy Rules:
// 1. Enter trade when price crosses above the lower band
// 2. Exit trade when price touches the upper band
//
// Chart Properties
testStartYear = input(2018, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)
testStopYear = input(2021, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(1, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
// A switch to control background coloring of the test period
testPeriodBackground = input(title="Color Background?", type=input.bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and time >= testPeriodStart and time <= testPeriodStop ? #6c6f6c : na
bgcolor(testPeriodBackgroundColor, transp=97)
// User provided values
smaLength = input(title="SMA Length", type=input.integer, defval=20) // Middle band period length (moving average)
stdLength = input(title="StdDev Length", type=input.integer, defval=20) // Range to apply bands to
ubOffset = input(title="Upper Band Offset", type=input.float, defval=2.0, step=0.5) // Number of standard deviations above MA
lbOffset = input(title="Lower Band Offset", type=input.float, defval=2.0, step=0.5) // Number of standard deviation below MA
res = input(title='Resolution', type=input.resolution, defval='1D') // Resolution
testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false
//closePrice = security(syminfo.tickerid, res, close)
stdDev = stdev(close, stdLength) // security(syminfo.tickerid, res, stdev(close, stdLength)) // Standard Deviation
smaValue = sma(close, smaLength) // security(syminfo.tickerid, res, sma(close, smaLength)) // Middle band
upperBand = smaValue + stdDev * ubOffset // Top band
lowerBand = smaValue - stdDev * lbOffset // Bottom band
// Plot Bands
plot(series=smaValue, title="SMA", color=color.blue)
plot(series=upperBand, title="UB", color=color.green, linewidth=2)
plot(series=lowerBand, title="LB", color=color.red, linewidth=2)
longCondition = (crossover(close, lowerBand))
closeLongCondition = (close >= upperBand)
if (longCondition and testPeriod())
strategy.entry(id="CALL", long=true)
strategy.close(id="CALL", when=closeLongCondition)
对于1号
您需要做的是使用tradingview的
security()
功能将所有src值替换为每日分辨率,如下所示:
src = security(syminfo.ticker, "D", close)
然后只需将所有
close
替换为 src
,现在无论您所处的时间范围如何,它都应该可以工作。
但请注意,当您进入比每日更高的时间范围时,会丢失数据,这是因为 security() 不适用于从较低时间范围导入数据。
对于2号
security()
函数来完成。
这是一个例子:
ema_5 = ema(close, 5)
getConditions()=>
buy = crossover(close, ema_5)
sell = crossunder(close, ema_5)
[buy, sell]
[btc_buy, btc_sell] = security("BINANCE:BTCUSDTPERP", timeframe.period, getConditions())
[ltc_buy, ltc_sell] = security("BINANCE:LTCUSDTPERP", timeframe.period, getConditions())
[eth_buy, eth_sell] = security("BINANCE:ETHUSDTPERP", timeframe.period, getConditions())
然后您可以使用这些值将其显示在表格等上。
但是,如果您只想分析净利润%、利润因素或使用策略脚本回测不同的交易品种和时间范围,那么您将需要一个网络抓取工具。您可以构建一个来为您执行此操作,而不是一一手动检查每个品种和时间范围。
朋友你好,这是什么指标?