Add Function to Find Lower High, Lower Low and Higher Low, Higher High. Use these Logic…
- User
Add Function to Find Lower High, Lower Low and Higher Low, Higher High. Use these Logic when Price Cross Pivot High for UP Trend and Pivot Low for Downtrend. Logic For Downtrend: Sequences like Pivot 1 = Lower High ("LH"), Pivot 2 = Low ("L"), Pivot 3 = High ("H") and For Uptrend: Sequences like Pivot 1 = Higher Low ("HL"), Pivot 2 = High ("H"), Pivot 3 = Low ("L"), If Last Pivots match these condition then Trend will Up Or Down otherwise it will be Sideways. Draw Only Recent 4 Pivots on Chart to Accurate Decision, If 3rd Pivot is High then check is 2nd Pivot is low and 1st is Lower High then trend is down and if 3rd Pivot is low then check for 2nd Pivot if 2nd Pivot is High then check for 1st pivot is that Higher low if these condition match then trend will be Up Trend.: //@version=5 indicator("Higher Time Frame Trend Detector", overlay=true)
// User Inputs higher_tf = input.timeframe("D", "Higher Time Frame") pivot_left = input.int(5, "Pivot Left Bars", minval=1) pivot_right = input.int(2, "Pivot Right Bars", minval=1) // Set default to 2 ma_length = input.int(21, "MA Length")
// Get Higher Time Frame Data htf_high = request.security(syminfo.tickerid, higher_tf, high) htf_low = request.security(syminfo.tickerid, higher_tf, low) htf_close = request.security(syminfo.tickerid, higher_tf, close) htf_bar_index = request.security(syminfo.tickerid, higher_tf, bar_index)
// Detect Pivots in Higher Time Frame pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right) pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)
// Initialize arrays to store pivots var string[] pivot_types = array.new_string() var int[] pivot_bars = array.new_int() var float[] pivot_values = array.new_float()
// Initialize series variables for pivot highs and lows var float pivot_high_plot = na var float pivot_low_plot = na
// Add pivot highs to arrays if not na(pivot_high) array.unshift(pivot_types, "H") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_high) // Assign pivot_high to pivot_high_plot for plotting pivot_high_plot := pivot_high else pivot_high_plot := na
// Add pivot lows to arrays if not na(pivot_low) array.unshift(pivot_types, "L") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_low) // Assign pivot_low to pivot_low_plot for plotting pivot_low_plot := pivot_low else pivot_low_plot := na
// Limit the size of the arrays to the last 6 pivots to conserve memory if array.size(pivot_types) > 6 array.pop(pivot_types) array.pop(pivot_bars) array.pop(pivot_values)
// Initialize Trend Variable var string trend = na
// Check if we have at least three pivots to analyze if array.size(pivot_types) >= 3 // Extract the last three pivots string pivot1_type = array.get(pivot_types, 0) // Most recent pivot float pivot1_value = array.get(pivot_values, 0) string pivot2_type = array.get(pivot_types, 1) float pivot2_value = array.get(pivot_values, 1) string pivot3_type = array.get(pivot_types, 2) float pivot3_value = array.get(pivot_values, 2)
- Assistant