OPEN-SOURCE SCRIPT

Winning and Losing Streaks

Updated
The Pine Script indicator "Winning and Losing Streaks" tracks and visualizes the length of consecutive winning and losing streaks in a financial series, such as stock prices. Here’s a detailed description of the indicator, including the relevance of statistical analysis and streak tracking.
Indicator Description

The "Winning and Losing Streaks" indicator in Pine Script is designed to analyze and display streaks of consecutive winning and losing days in trading data. It helps traders and analysts understand the persistence of trends in price movements.

Here’s how it functions:

Streak Calculation:

Winning Streak: A series of consecutive days where the closing price is higher than the previous day's closing price.

Losing Streak: A series of consecutive days where the closing price is lower than the previous day's closing price.

Doji Candles: The indicator also considers Doji candles, where the difference between the opening and closing prices is minimal relative to the high-low range, and excludes these from being counted as winning or losing days.

Statistical Analysis:

The indicator computes the maximum and average lengths of winning and losing streaks.

It also tracks the current streak lengths and maintains arrays to store the historical streak data.

Visualization:

Histograms: Winning and losing streaks are visualized using histograms, which provide a clear graphical representation of streak lengths over time.

Relevance of Statistical Analysis and Streak Tracking

1. Statistical Significance of Streaks

Tracking winning and losing streaks has significant statistical implications for trading strategies and risk management:

Autocorrelation: Streaks in financial time series can reveal autocorrelation, where past returns influence future returns. Studies have shown that financial time series often exhibit autocorrelation, which can be used to forecast future price movements (Lo, 1991; Jegadeesh & Titman, 1993). Understanding streaks helps in identifying and leveraging these patterns.

Behavioral Finance: Streak analysis aligns with concepts from behavioral finance, such as the "hot-hand fallacy," where investors may perceive trends as more persistent than they are (Gilovich, Vallone, & Tversky, 1985). Statistical streak analysis provides a more objective view of trend persistence, helping to avoid biases.

2. Risk Management and Strategy Development

Risk Assessment: Identifying the length and frequency of losing streaks is crucial for managing risk and adjusting trading strategies. Long losing streaks can indicate potential strategy weaknesses or market regime changes, prompting a reassessment of trading rules and risk management practices (Brock, Lakonishok, & LeBaron, 1992).

Strategy Optimization: Statistical analysis of streaks can aid in optimizing trading strategies. For example, understanding the average length of winning and losing streaks can help in setting more effective stop-loss and take-profit levels, as well as in determining the optimal position sizing (Fama & French, 1993).

Scientific References:

Lo, A. W. (1991). "Long-Term Memory in Stock Market Prices." Econometrica, 59(5), 1279-1313. This paper discusses the presence of long-term memory in stock prices, which is relevant for understanding the persistence of streaks.

Jegadeesh, N., & Titman, S. (1993). "Returns to Buying Winners and Selling Losers: Implications for Stock Market Efficiency." Journal of Finance, 48(1), 65-91. This study explores momentum and reversal strategies, which are related to the concept of streaks.

Gilovich, T., Vallone, R., & Tversky, A. (1985). "The Hot Hand in Basketball: On the Misperception of Random Sequences." Cognitive Psychology, 17(3), 295-314. This paper provides insight into the psychological aspects of streaks and persistence.

Brock, W., Lakonishok, J., & LeBaron, B. (1992). "Simple Technical Trading Rules and the Stochastic Properties of Stock Returns." Journal of Finance, 47(5), 1731-1764. This research examines the effectiveness of technical trading rules, relevant for streak-based strategies.

Fama, E. F., & French, K. R. (1993). "Common Risk Factors in the Returns on Stocks and Bonds." Journal of Financial Economics, 33(1), 3-56. This paper provides a foundation for understanding risk factors and strategy performance.

By analyzing streaks, traders can gain valuable insights into market dynamics and refine their trading strategies based on empirical evidence.
Release Notes
The "Winning and Losing Streaks with Percentages" indicator is designed to analyze and visualize the frequency and duration of winning and losing streaks in financial data, such as stock prices or other assets. This indicator plots and tabulates the length of streaks along with their historical occurrence rates and estimated future probabilities.

Key Features

Doji Sensitivity: The indicator includes a sensitivity parameter for detecting "doji" candles—candles with very small body sizes compared to their high-low range. This helps to exclude indecisive or neutral price movements from the streak analysis.

Streak Calculation:

Winning Streak: Consecutive periods where the closing price is higher than the previous period, excluding doji candles.

Losing Streak: Consecutive periods where the closing price is lower than the previous period, excluding doji candles.

Streak Length Storage: The indicator maintains arrays to record the length of winning and losing streaks, updating these records as new data is processed.

Percentage Calculation: It calculates the percentage occurrence of streaks of various lengths (e.g., 1-day, 2-day, 3-day streaks) for both winning and losing streaks.

Future Probability Estimation: Based on historical data, the indicator estimates the future probability of streaks of various lengths continuing, offering insights into potential future performance.

Visualization:

Table Display: A table displays the percentages of different streak lengths and their estimated future probabilities.

Histograms: Bar plots show the current length of winning and losing streaks.

Benefits for Portfolio Management

Risk Assessment: Understanding the frequency and duration of streaks can help assess the stability and risk of a portfolio. Long losing streaks might indicate heightened risk or volatility, prompting a reassessment of positions.

Trend Identification: Identifying prolonged winning or losing streaks can provide insights into current market trends. For instance, extended winning streaks might signal a strong trend, suggesting a potential for trend-following strategies.

Probabilistic Forecasting: The future probability estimates offer a quantitative basis for predicting the likelihood of streaks continuing. This can assist in making more informed decisions about maintaining or altering positions based on anticipated future performance.

Performance Metrics: By analyzing streak lengths and their occurrences, investors can evaluate the performance metrics of their trading strategies and adjust their portfolio management tactics accordingly.

Behavioral Insights: Streak analysis can reveal patterns related to market psychology and behavior. For instance, understanding how often and how long streaks last can provide insights into market sentiment and investor behavior.

Scientific Basis

Behavioral Finance: Research in behavioral finance suggests that streaks and patterns can influence investor behavior and market dynamics. Analyzing streak lengths helps in understanding how market participants might react during different streaks, influencing decision-making (Barberis & Thaler, 2003).

Statistical Analysis: Streak analysis is supported by statistical methods to understand the distribution and probability of streaks occurring. This aligns with principles from probability theory and statistics used in financial forecasting (Feller, 1968).

By integrating these features, the indicator provides a comprehensive tool for analyzing market streaks and making informed portfolio management decisions based on historical patterns and future probabilities.
forecastingPortfolio managementstatistics

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer