I have a basic Pine script which is provided below for reference, and there are two concepts that I would like assistance in clarifying.
How do I set the resolution of this strategy so that I can look at 1Y, 5y, YTD, etc. charts but always have the strategy run on a specific resolution (daily bars for example). I have made a few attempts at this which you can see commented out in the code, but I don't think it is working properly; I would like to see how someone more experienced would achieve this.
I would like to write a strategy that analyzes multiple securities at once, however, I can't find a good way to do this in Tradingview. I previously used Quantconnect, and in that system, I could choose any amount of securities that I wanted to work with. For example, the strategy below is the relatively low frequency with its trades (about four per year), so I would like to run this strategy with multiple securities to potentially reduce downtime. It is a pretty big downside for me to not be able to run a strategy that analyzes and trades multiple securities.
I appreciate all help you can provide. The referenced code is provided below.
//@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)
