Update Trend Logic: For UP Trend, High of Pivot 1 Should be Lower than Pivot 2 High and…
- User
Update Trend Logic: For UP Trend, High of Pivot 1 Should be Lower than Pivot 2 High and The High of Pivot 2 Should be Higher than High of Pivot 3 High
For Downtrend: Low of Pivot 1 should be Higher than Low of Pivot 2 and Pivot 2 Should be Lower than Low of Pivot 3:
Code: //@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)
code - Assistant