Tradingview multi-security strategy and resolution setiings

Viewed 1298

I have a basic Pine script which is provided below for reference, and there are two concepts that I would like assistance in clarifying.

  1. 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.

  2. 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)
1 Answers

For Number 1

What you need to do is use the security() function of tradingview to replace all the src values with the daily resolution like these:

src = security(syminfo.ticker, "D", close)

Then just replace all close with src and it should now work whatever timeframe you are on.

But note that there is loss of data when you go to a higher timeframe than daily this is because the security() is not meant to use to import data from a lower time frame.


For Number 2

You can create a screener like these ones: enter image description here This can be done using security() function too. Here's an example:

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())

Then you can use these values to display it on a table or etc.

But, if you just want to analyze the net profit%, profit factor or backtest different symbols and timeframes with your strategy script then you will need a web scraper. You can build one to do this for you than manually checking every symbols and timeframes one by one.

Related