-
Notifications
You must be signed in to change notification settings - Fork 31
/
rsi_dips.py
executable file
·170 lines (149 loc) · 5.25 KB
/
rsi_dips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""
A script to analyze RSI dips below a specified lower value before a significant rise above a specified higher value.
Usage:
./rsi_dips.py -h
./rsi_dips.py -s <symbol> -d <start_date> --lower <value> --higher <value> -v # To log INFO messages
./rsi_dips.py -s <symbol> -d <start_date> --lower <value> --higher <value> -vv # To log DEBUG messages
Optional arguments:
-s, --symbol <symbol> Stock symbol to analyze (required)
-d, --start <date> Start date for fetching stock data in YYYY-MM-DD format (required)
--lower <value> Set the lower RSI threshold (default: 20)
--higher <value> Set the higher RSI threshold (default: 80)
--rsi-period <value> Set the RSI period (default: 3)
"""
import logging
from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter
from datetime import datetime
from datetime import timedelta
from finta import TA
from rich.console import Console
from rich.table import Table
from rich.text import Text
from common.logger import setup_logging
from common.market import download_ticker_data
def parse_args():
parser = ArgumentParser(
description=__doc__, formatter_class=RawDescriptionHelpFormatter
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=1,
dest="verbose",
help="Increase verbosity of logging output",
)
parser.add_argument(
"-s", "--symbol", type=str, required=True, help="Stock symbol to analyze"
)
parser.add_argument(
"-d",
"--start",
type=str,
default=(datetime.now() - timedelta(days=200)).strftime("%Y-%m-%d"),
help="Start date for fetching stock data in YYYY-MM-DD format",
)
parser.add_argument(
"--rsi-period",
type=int,
default=3,
help="RSI period (default: 3)",
)
parser.add_argument(
"--lower",
type=int,
default=20,
help="Set the lower RSI threshold (default: 20)",
)
parser.add_argument(
"--higher",
type=int,
default=80,
help="Set the higher RSI threshold (default: 80)",
)
return parser.parse_args()
def identify_dips(df, lower, higher):
dips_below_threshold = 0
dip_dates = []
above_lower_once = False
max_continuous_dips = 0
max_continuous_dips_date = None
total_dips = 0
positions = []
table = Table()
table.add_column("Date", justify="center", style="cyan", no_wrap=True)
table.add_column("total_shares", justify="right", style="magenta")
table.add_column("sold_price", justify="right", style="green")
table.add_column("pnl", justify="right", style="bold")
for index, row in df.iterrows():
close = row["Close"]
rsi = row["RSI"]
if rsi > lower:
above_lower_once = True
if rsi < lower:
total_dips += 1
if above_lower_once:
dips_below_threshold += 1
dip_dates.append(index)
above_lower_once = False
logging.debug(f"📉 Buy: {index} Close: {close} RSI: {rsi}")
positions.append(
dict(
date_purchased=index,
close=close,
shares=100 * dips_below_threshold,
purchase_price=close * 100 * dips_below_threshold,
)
)
if rsi > higher and dips_below_threshold > 0:
logging.debug(
f"✅ Dips below threshold: {dips_below_threshold} Date: {index} Close: {close} RSI: {rsi}"
)
total_shares = sum([p["shares"] for p in positions])
invested_amount = sum(p["purchase_price"] for p in positions)
sold_price = close * total_shares
date_sold = index.date()
pnl = sold_price - invested_amount
if pnl > 0:
pnl_text = Text(f"{pnl:.2f}", style="green")
elif pnl < 0:
pnl_text = Text(f"{pnl:.2f}", style="red")
else:
pnl_text = Text(f"{pnl:.2f}")
table.add_row(
str(date_sold), str(total_shares), f"{sold_price:.2f}", pnl_text
)
if dips_below_threshold > max_continuous_dips:
max_continuous_dips = dips_below_threshold
max_continuous_dips_date = index.date()
dips_below_threshold = 0
positions.clear()
print(
f"Maximum continuous dips: {max_continuous_dips} on {max_continuous_dips_date}"
)
print(f"Total dips: {total_dips}")
console = Console()
console.print(table)
def main(args):
ticker = args.symbol
end_date = datetime.now().strftime("%Y-%m-%d")
df = download_ticker_data(
ticker,
args.start,
end_date,
)
if df.empty:
logging.error(
f"Failed to fetch data for symbol {args.symbol}. Please check the symbol and try again."
)
return
# Calculate RSI
df["RSI"] = TA.RSI(df, period=args.rsi_period)
# Identify dips below the specified lower threshold
identify_dips(df, args.lower, args.higher)
if __name__ == "__main__":
args = parse_args()
setup_logging(args.verbose)
main(args)