Market Direction
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wellthere
// This script tracks the market looking for 2 consecutive 5 day periods that are lower or higher. It shows the trend of the overall market
// I am using the ETF VTI to represent the overall market
// This can help to determine the direction of the market when entering a position
// I am also using %B BB to determine over bought and over sold conditions in VTI. I am using > 1 and < 0 but you can easily change in the script


//@version=5
indicator('Market Direction', shorttitle=' Market Trend +++ OverBought=yellow and OverSold=white', overlay=false)
//get current value for VTTI
market = request.security('VTI', timeframe.period, low)

//set the color based on the direction of the market
dtrend = if market < market[5] and market[5] < market[10]
    color.new(color.red,50)
else
    na
utrend = if market > market[5] and market[5] > market[10]
    color.new(color.green,50)
else
    na

// these next 2 plots report the numbers only for the value 10 days ago and 5 days ago.
plot(market[10], color=color.new(color.white,100))
plot(market[5], color=color.new(color.white,100))

plot(market, 'Trends', color=dtrend, linewidth=2, style=plot.style_area)
plot(market, 'Trends', color=utrend, linewidth=2, style=plot.style_area)


[middle, upper, lower] = ta.bb(market, 20, 2)  
price = market  // price of VTI based on line 11 ( note that I am using the low and that could be changed to high or close)
bper = (price - lower) / (upper - lower)
plot(bper, color=color.new(color.yellow,100))

overbought = bper > 1  // using > 1 for over bought
oversold = bper < 0  // using < 0 for over sold

bgcolor(overbought ? color.new(color.yellow,40) : oversold ? color.new(color.white,40) : color.new(color.black,40))