Here's stats for an automated test of bull flags on the daily chart in BTC since 2015.
Bull Flag Pattern P&L Stats in BTC:
- Total Bull Flags: 1602
- Wins: 576
- Losses: 1026
- Win Rate: 36.0%
- Average Gain per Flag: -2.11%
- Total Gain: -3373.25%
--
This was on settings people use quite often. Code attached if you want to test your own settings.
--
import yfinance as yf
import pandas as pd
# Parameters for bull flag detection
PULLBACK_DAYS = 5
BREAKOUT_DAYS = 5
WIN_THRESHOLD = 0 # 0% return or higher is considered a "win"
def download_data(ticker="BTC-USD", start="2015-01-01", end="2023-12-31"):
"""Download BTC daily data from Yahoo Finance"""
data = yf.download(ticker, start=start, end=end)
return data
def detect_bull_flags(data, pullback_days=PULLBACK_DAYS, breakout_days=BREAKOUT_DAYS):
"""Detect bull flags and calculate post-breakout performance"""
flags = []
data['High_Close'] = data['Close'].rolling(pullback_days).max()
for i in range(pullback_days, len(data) - breakout_days):
pullback_data = data['Close'].iloc[i-pullback_days:i]
if (pullback_data[-1] < pullback_data.max()) and (pullback_data.min() < pullback_data[-1]):
breakout_level = data['High_Close'].iloc
post_breakout_data = data['Close'].iloc[i:i + breakout_days]
breakout_gain = (post_breakout_data[-1] - breakout_level) / breakout_level
flags.append({
'Date': data.index,
'Breakout_Gain (%)': breakout_gain * 100,
'Win': breakout_gain > WIN_THRESHOLD
})
return pd.DataFrame(flags)
def display_pl_stats(bull_flags):
"""Display win/loss and P&L statistics for bull flags."""
wins = bull_flags['Win'].sum()
losses = len(bull_flags) - wins
win_rate = (wins / len(bull_flags)) * 100
avg_gain = bull_flags['Breakout_Gain (%)'].mean()
total_gain = bull_flags['Breakout_Gain (%)'].sum()
print("Bull Flag Pattern P&L Stats in BTC:\n")
print(f"- Total Bull Flags: {len(bull_flags)}")
print(f"- Wins: {wins}")
print(f"- Losses: {losses}")
print(f"- Win Rate: {win_rate:.1f}%")
print(f"- Average Gain per Flag: {avg_gain:.2f}%")
print(f"- Total Gain: {total_gain:.2f}%")
def main():
# Step 1: Download Data
data = download_data()
# Step 2: Detect Bull Flags
bull_flags = detect_bull_flags(data)
# Step 3: Display P&L Stats
display_pl_stats(bull_flags)
if __name__ == "__main__":
main()