jarvisx17 commited on
Commit
0e32775
·
verified ·
1 Parent(s): e711955

Upload 2 files

Browse files
stratagy/__pycache__/rsi_stratagy.cpython-38.pyc ADDED
Binary file (3.82 kB). View file
 
stratagy/rsi_stratagy.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import pandas as pd
3
+
4
+ def process_dataframe(df):
5
+ def get_rsi(close, lookback):
6
+ ret = close.diff()
7
+ up = []
8
+ down = []
9
+ for i in range(len(ret)):
10
+ if ret[i] < 0:
11
+ up.append(0)
12
+ down.append(ret[i])
13
+ else:
14
+ up.append(ret[i])
15
+ down.append(0)
16
+ up_series = pd.Series(up)
17
+ down_series = pd.Series(down).abs()
18
+ up_ewm = up_series.ewm(com=lookback - 1, adjust=False).mean()
19
+ down_ewm = down_series.ewm(com=lookback - 1, adjust=False).mean()
20
+ rs = up_ewm / down_ewm
21
+ rsi = 100 - (100 / (1 + rs))
22
+ rsi_df = pd.DataFrame(rsi).rename(columns={0: 'RSI'}).set_index(close.index)
23
+ rsi_df = rsi_df.dropna()
24
+ return rsi_df[3:]
25
+
26
+ df['RSI'] = get_rsi(df['Close'], 14)
27
+ df['SMA20'] = df['Close'].rolling(window=20).mean()
28
+ df.drop(['Adj Close'], axis=1, inplace=True)
29
+ df = df.dropna()
30
+
31
+ return df
32
+
33
+ def fin_data(ticker, startdate, enddate):
34
+ df=yf.download(ticker,start=startdate,end=enddate, progress=False)
35
+ df = process_dataframe(df)
36
+ df.reset_index(inplace=True)
37
+ df = df.dropna()
38
+ df.reset_index(drop=True, inplace=True)
39
+ df[['Open', 'High', 'Low', 'Close',"RSI"]] = df[['Open', 'High', 'Low', 'Close',"RSI"]].round(2)
40
+ df = df[200:]
41
+ df.reset_index(drop=True,inplace=True)
42
+ return df
43
+
44
+ def eqt(ticker, startdate, enddate, share_qty = 90):
45
+ df = fin_data(ticker, startdate, enddate)
46
+ entry = False
47
+ trading = False
48
+ shares_held = 0
49
+ buy_price = 0
50
+ target1 = False
51
+ target2 = False
52
+ target3 = False
53
+ tgt1 = 0
54
+ tgt2 = 0
55
+ tgt3 = 0
56
+ total_profit = 0
57
+ profits = []
58
+ stop_loss = 0
59
+ capital_list = []
60
+ start_date = []
61
+ end_date = []
62
+
63
+ for i in range(1, len(df)-1):
64
+ try:
65
+ if df.at[i, 'RSI'] > 60 and df.at[i - 1, 'RSI'] < 60 and df.at[i, 'High'] < df.at[i + 1, 'High'] and not entry and not trading:
66
+ buy_price = df.at[i, 'High']
67
+ stop_loss = df.at[i, 'Low']
68
+ start_date.append(df.at[i, 'Date'])
69
+ capital = buy_price * share_qty
70
+ capital_list.append(round(capital, 2))
71
+ shares_held = share_qty
72
+ entry = True
73
+ trading = True
74
+
75
+ if trading and not target1:
76
+ if (df.at[i + 1, 'High'] - buy_price) >= 0.02 * buy_price:
77
+ stop_loss = buy_price
78
+ target1 = True
79
+ tgt1 = 0.02 * buy_price * (share_qty / 3)
80
+ shares_held -= (share_qty / 3)
81
+ total_profit = tgt1
82
+
83
+ if trading and target1 and not target2:
84
+ if (df.at[i + 1, 'High'] - buy_price) >= 0.04 * buy_price:
85
+ target2 = True
86
+ tgt2 = 0.04 * buy_price * (share_qty / 3)
87
+ total_profit += tgt2
88
+ shares_held -= (share_qty / 3)
89
+
90
+ if trading and target2 and not target3:
91
+ if (df.at[i + 1, 'Open'] < df.at[i + 1, 'SMA20'] < df.at[i + 1, 'Close']) or (df.at[i + 1, 'Open'] > df.at[i + 1, 'SMA20'] > df.at[i + 1, 'Close']):
92
+ stop_loss = df.at[i + 1, 'Low']
93
+ if df.at[i + 2, 'Low'] < stop_loss:
94
+ target3 = True
95
+ tgt3 = stop_loss * (share_qty / 3)
96
+ shares_held -= (share_qty / 3)
97
+ total_profit += tgt3
98
+
99
+ if (df.at[i + 1, 'Low'] < stop_loss and trading):
100
+ profit_loss = (shares_held * stop_loss) - (shares_held * buy_price)
101
+ total_profit += profit_loss
102
+ profits.append(total_profit)
103
+ end_date.append(df.at[i, 'Date'])
104
+ shares_held = 0
105
+ buy_price = 0
106
+ entry = False
107
+ trading = False
108
+ target1 = target2 = target3 = False
109
+ tgt1 = tgt2 = tgt3 = 0
110
+ total_profit = 0
111
+
112
+
113
+
114
+
115
+ except IndexError:
116
+ continue
117
+
118
+ print("\n")
119
+ print(f"Stock: {ticker} - From {df.at[1, 'Date']} to {df.at[len(df) - 1, 'Date']}")
120
+ print(f"Required capital Range equity per trade: {round(capital_list[0],2)} ₹ - {round(capital_list[-1],2)} ₹")
121
+ print("Duration Total Trading Profit:", round(sum(profits), 2),"₹")
122
+ if profits:
123
+ if len(start_date) > len(end_date):
124
+ rr = len(end_date)
125
+ df = pd.DataFrame({"Start" : start_date[:rr], "End": end_date, "profit" : profits, "Capital" : capital_list[:rr]})
126
+ df['percentage'] = (df['profit'] / df['Capital']) * 100
127
+ df['percentage'] = df['percentage'].apply(lambda x: f"{x:.2f}%" if x >= 0 else f"-{-x:.2f}%")
128
+ else:
129
+ df = pd.DataFrame({"Start" : start_date, "End": end_date, "profit" : profits, "Capital" : capital_list})
130
+ df['percentage'] = (df['profit'] / df['Capital']) * 100
131
+ df['percentage'] = df['percentage'].apply(lambda x: f"{x:.2f}%" if x >= 0 else f"-{-x:.2f}%")
132
+ return df
133
+ else:
134
+ return 0