Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -53,18 +53,77 @@ def average_downtrend(data, method, window=4):
|
|
53 |
return avg_diff if avg_diff < 0 else 0.0
|
54 |
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def score_today_candle(data, window=4):
|
|
|
57 |
if len(data) < window + 1:
|
58 |
-
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
trend_score = score_downward_trend(data.iloc[-window:], window=window)
|
61 |
-
candle_score = score_candle(data.iloc[-1], data, len(data) - 1)
|
62 |
-
risk_reward = calculate_risk_reward(data, len(data) - 1)
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def scan_sp500(top_n=25, progress=gr.Progress()):
|
70 |
tickers = load_sp500_tickers()
|
@@ -119,4 +178,4 @@ iface = gr.Interface(
|
|
119 |
)
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
53 |
return avg_diff if avg_diff < 0 else 0.0
|
54 |
|
55 |
|
56 |
+
def score_candle(candle, prev_candle, trend_strength):
|
57 |
+
"""Score a single candle based on its characteristics and previous candle."""
|
58 |
+
open_price = candle['Open']
|
59 |
+
close_price = candle['Close']
|
60 |
+
low_price = candle['Low']
|
61 |
+
high_price = candle['High']
|
62 |
+
prev_close = prev_candle['Close']
|
63 |
+
|
64 |
+
# Bottom and top wick lengths
|
65 |
+
bottom_wick_length = min(open_price, close_price) - low_price
|
66 |
+
top_wick_length = high_price - max(open_price, close_price)
|
67 |
+
|
68 |
+
# Initial score based on trend strength
|
69 |
+
score = trend_strength * 2
|
70 |
+
|
71 |
+
# Doji: Open and Close are almost the same (small body)
|
72 |
+
if abs(open_price - close_price) <= 0.1 * (high_price - low_price): # Adjust tolerance if needed
|
73 |
+
score += 5 # Bonus points for doji candles
|
74 |
+
|
75 |
+
# Hammer: Small body at the top, long bottom wick (typical reversal candle)
|
76 |
+
if close_price < open_price and bottom_wick_length > 2 * (open_price - close_price):
|
77 |
+
score += 7 # Extra points for hammer-like candles
|
78 |
+
|
79 |
+
# Bottom Tailing Wick: Long bottom wick compared to the overall range
|
80 |
+
if bottom_wick_length > 0.5 * (high_price - low_price):
|
81 |
+
score += 6 # Extra points for bottom tailing wick
|
82 |
+
|
83 |
+
# Additional logic: Boost red candles with long bottom wicks following a downtrend
|
84 |
+
if close_price < open_price and bottom_wick_length > 0.5 * (open_price - close_price):
|
85 |
+
score += 3 # Boost for red candle with long bottom wick
|
86 |
+
|
87 |
+
# Penalize if the current close is higher than the previous close
|
88 |
+
if close_price > prev_close:
|
89 |
+
score -= ((close_price - prev_close) / prev_close) * 100
|
90 |
+
|
91 |
+
|
92 |
+
return score
|
93 |
+
|
94 |
def score_today_candle(data, window=4):
|
95 |
+
"""Score today's candle based on the downtrend from the past 'window' days."""
|
96 |
if len(data) < window + 1:
|
97 |
+
return 0 # Not enough data
|
98 |
+
|
99 |
+
today_candle = data.iloc[-1]
|
100 |
+
prev_candle = data.iloc[-2]
|
101 |
+
|
102 |
+
close_price = today_candle['Close']
|
103 |
+
|
104 |
+
|
105 |
+
previous_data = data.iloc[-(window+1):-1]
|
106 |
+
down_High = average_downtrend(previous_data, method="High",window=window) + average_downtrend(previous_data, method="High",window=7) / 2
|
107 |
+
down_Close = average_downtrend(previous_data, method="Close",window=window) + average_downtrend(previous_data, method="Close",window=7) / 2
|
108 |
|
|
|
|
|
|
|
109 |
|
110 |
+
avg_downtrend = (down_High + down_Close) / 2
|
111 |
+
|
112 |
+
if avg_downtrend == 0.0:
|
113 |
+
return -1
|
114 |
|
115 |
+
# Calculate SMAs for the last row
|
116 |
+
sma_50 = calculate_sma(data, window=50).iloc[-1]
|
117 |
+
sma_200 = calculate_sma(data, window=200).iloc[-1]
|
118 |
+
sma_20 = calculate_sma(data, window=20).iloc[-1]
|
119 |
+
|
120 |
+
ema_10 = calculate_ema(data, window=10).iloc[-1]
|
121 |
+
|
122 |
+
if (close_price < ema_10) or (close_price < sma_20) or (close_price < sma_50) or (close_price < sma_200):
|
123 |
+
return -1
|
124 |
+
|
125 |
+
|
126 |
+
return score_candle(today_candle, prev_candle, abs(avg_downtrend))
|
127 |
|
128 |
def scan_sp500(top_n=25, progress=gr.Progress()):
|
129 |
tickers = load_sp500_tickers()
|
|
|
178 |
)
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|