OPEN-SOURCE SCRIPT

veeru S&R with Previous/Weekly/Important Levels

Key improvements and explanations:

Previous Day High/Low: The get_previous_day_hl() function now correctly retrieves the previous day's high and low using high[1] and low[1].

Weekly High/Low: The get_weekly_hl() function provides a basic implementation. Important: It's not perfectly accurate for all timeframes because TradingView doesn't directly give you the beginning of the week. A better approach would involve more advanced date/time calculations (which can be complex in Pine Script). The current version resets the weekly high/low on every day change. This means that on the first day of the week, it will start tracking from that day's high/low.

Important High/Low: The get_important_hl() function uses ta.pivothigh and ta.pivotlow with a separate important_lookback input, allowing you to define a longer lookback for these levels.

Plotting: The plot() function is used to draw the levels on the chart with different colors and styles for clarity. The style=plot.style_linebr creates a broken line, which can be useful for these types of levels.

Alerts: Example alerts are included to demonstrate how you can trigger alerts when the price crosses above/below these levels. You can customize the alert conditions and messages as needed. I've added examples for the important levels and the previous day levels. You can easily add similar alerts for the weekly levels.

Clearer Variable Names and Comments: The code uses more descriptive variable names and includes comments to explain the logic.

Further Enhancements (For more advanced users):

Accurate Weekly Levels: For a truly accurate weekly high/low, you'll need to use the dayofweek() and weekofyear() functions, and potentially store the weekly high/low in a var variable that gets reset only at the start of the week. This requires careful handling of time and date changes.
Dynamic Lookback: You could make the lookback and important_lookback inputs more dynamic based on volatility or other factors.
Visualizations: Consider using line.new() or box.new() to draw more prominent lines or boxes for the important levels.
Breaks and Retests: If you want to integrate the break and retest logic from your original script, you can combine the level detection with your existing code. The key would be to use the prev_day_high, prev_day_low, weekly_high, weekly_low, important_high, and important_low variables in your break/retest conditions.
This improved script provides a solid foundation for tracking and alerting on key support and resistance levels. Remember to adjust the important_lookback and other parameters to suit your trading style and the specific market you're analyzing.

Penafian