Display Trade Volume with MA Angle and Price Velocity
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BoredDev

//@version=5
indicator("Display Trade Volume with MA Angle", shorttitle="DTVMA", overlay=true)

lookback = input.int(500, minval=1)
tablePosition = input.string(title="Table Position", defval="Top Right", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left"])
maType = input.string(title="MA Type", defval="SMA", options=["SMA", "EMA", "HMA"])
maLength = input.int(title="MA Length", defval=20)
velocityLookback = input.int(title="Velocity Lookback Periods", defval=14)

positionInput = position.top_right
if tablePosition == "Bottom Right"
    positionInput := position.bottom_right
else if tablePosition == "Top Left"
    positionInput := position.top_left
else if tablePosition == "Bottom Left"
    positionInput := position.bottom_left

var ma = close
if maType == "SMA"
    ma := ta.sma(close, maLength)
else if maType == "EMA"
    ma := ta.ema(close, maLength)
else if maType == "HMA"
    ma := ta.hma(close, maLength)

volumeAvg = ta.sma(volume, lookback)

var volumeColor = color.new(color.red, 0)
var volumeText = ""
if barstate.isconfirmed
    volumeColor := volume[1] < volumeAvg[1] ? color.red : color.green
    volumeText := volume[1] < volumeAvg[1] ? "Low" : "Trade"

angle = math.atan((ma - ma[1]) / syminfo.mintick) * (180 / math.pi)
angleColor = angle > 0 ? color.green : color.red

// Calculate velocity and average velocity over the last 'velocityLookback' periods
highLowDiffArray = array.new_float(0)
for i=0 to velocityLookback-1
    array.push(highLowDiffArray, high[i]-low[i])
avgHighLowDiffArray=array.avg(highLowDiffArray)

velocityColor=color.new(color.red, 0)
velocityText=""
if barstate.isconfirmed
    velocityColor:=array.get(highLowDiffArray, 0)<avgHighLowDiffArray?color.red:color.green 
    velocityText:=array.get(highLowDiffArray, 0)<avgHighLowDiffArray?"Slow":"Fast"

var table myTable = table.new(positionInput, 3 ,2)

if barstate.isfirst
    table.cell(myTable, 0 ,0 ,"Volume")
    table.cell(myTable, 1 ,0 ,"MA Angle")
    table.cell(myTable, 2 ,0 ,"Velocity")

table.cell(myTable, 0 ,1 ,volumeText)
table.cell_set_bgcolor(myTable, 0 ,1 ,volumeColor)
table.cell_set_text_color(myTable, 0 ,0 ,color.white)
table.cell_set_text_color(myTable, 0 ,1 ,color.white)

angleText = str.tostring(math.floor(angle)) + "°"
table.cell(myTable, 1 ,1 ,angleText)
table.cell_set_bgcolor(myTable, 1 ,1 ,angleColor)
table.cell_set_text_color(myTable, 1 ,0 ,color.white)
table.cell_set_text_color(myTable, 1 ,1 ,color.white)

if barstate.isconfirmed
    table.cell(myTable,2,1,str.tostring(velocityText))
    table.cell_set_bgcolor(myTable,2,1,velocityColor)
    table.cell_set_text_color(myTable,2,0,color.white)
    table.cell_set_text_color(myTable,2,1,color.white)

plot(ma)

// Add alerts for high and low volume conditions
highVolumeCondition = volume > volumeAvg // true when current volume is higher than average volume
lowVolumeCondition = volume < volumeAvg // true when current volume is lower than average volume

alertcondition(highVolumeCondition,title="High Volume Alert",message="The current bar has high trading activity.")
alertcondition(lowVolumeCondition,title="Low Volume Alert",message="The current bar has low trading activity.")