Repository navigation
Expand file tree
/
Copy pathbuy_sell_signals_pandas_ta.py
More file actions
223 lines (188 loc) · 7.6 KB
/
Copy pathbuy_sell_signals_pandas_ta.py
File metadata and controls
223 lines (188 loc) · 7.6 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'''
Author: Utkarsh Singhal
Adapted from: Bruno
Blog: https://www.trading-et-data-analyses.com/p/strategies-dans-tradinginpython.html
This code has implementation for MACD, RSI and Bollinger Bands
'''
import numpy as np
import pandas as pd
import yfinance
import pandas_ta as ta
import matplotlib.pyplot as plt
from datetime import date
plt.style.use('fivethirtyeight')
stocksymbols = ['TATACOMM.NS']
startdate = date(2025,1,1)
end_date = date.today()
print(end_date)
def getMyPortfolio(stocks = stocksymbols ,start = startdate , end = end_date):
data = yfinance.download(
stocks, start=startdate, end=end_date, prepost=True, auto_adjust=False
)
return data
data = getMyPortfolio(stocksymbols)
########### Simple Moving Average Implementation #############
data['SMA 30'] = ta.sma(data['Close'],30)
data['SMA 100'] = ta.sma(data['Close'],100)
# Remplir les valeurs manquantes après calcul des indicateurs
data.fillna(0, inplace=True)
#SMA BUY SELL
#Function for buy and sell signal
def buy_sell(data):
signalBuy = []
signalSell = []
position = False
for i in range(len(data)):
if data['SMA 30'][i] > data['SMA 100'][i]:
if position == False :
signalBuy.append(data['Adj Close'][i])
signalSell.append(np.nan)
position = True
else:
signalBuy.append(np.nan)
signalSell.append(np.nan)
elif data['SMA 30'][i] < data['SMA 100'][i]:
if position == True:
signalBuy.append(np.nan)
signalSell.append(data['Adj Close'][i])
position = False
else:
signalBuy.append(np.nan)
signalSell.append(np.nan)
else:
signalBuy.append(np.nan)
signalSell.append(np.nan)
return pd.Series([signalBuy, signalSell])
data['Buy_Signal_price'], data['Sell_Signal_price'] = buy_sell(data)
fig, ax = plt.subplots(figsize=(14,8))
ax.plot(data['Adj Close'] , label = stocksymbols[0] ,linewidth=0.5, color='blue', alpha = 0.9)
ax.plot(data['SMA 30'], label = 'SMA30', alpha = 0.85)
ax.plot(data['SMA 100'], label = 'SMA100' , alpha = 0.85)
ax.scatter(data.index , data['Buy_Signal_price'] , label = 'Buy' , marker = '^', color = 'green',alpha =1 )
ax.scatter(data.index , data['Sell_Signal_price'] , label = 'Sell' , marker = 'v', color = 'red',alpha =1 )
ax.set_title(stocksymbols[0] + " Price History with buy and sell signals",fontsize=10, backgroundcolor='blue', color='white')
ax.set_xlabel(f'{startdate} - {end_date}' ,fontsize=18)
ax.set_ylabel('Close Price INR (₨)' , fontsize=18)
legend = ax.legend()
ax.grid()
plt.tight_layout()
plt.show()
########### MACD Implementation #############
close = data['Close'].squeeze()
macd = ta.macd( close )
data = pd.concat([data, macd], axis=1).reindex(data.index)
if 'Adj Close' not in data.columns:
data['Adj Close'] = close
def MACD_Strategy(df, risk):
MACD_Buy=[]
MACD_Sell=[]
position=False
for i in range(0, len(df)):
if df['MACD_12_26_9'][i] > df['MACDs_12_26_9'][i] :
MACD_Sell.append(np.nan)
if position ==False:
MACD_Buy.append(df['Adj Close'][i])
position=True
else:
MACD_Buy.append(np.nan)
elif df['MACD_12_26_9'][i] < df['MACDs_12_26_9'][i] :
MACD_Buy.append(np.nan)
if position == True:
MACD_Sell.append(df['Adj Close'][i])
position=False
else:
MACD_Sell.append(np.nan)
elif position == True and df['Adj Close'][i] < MACD_Buy[-1] * (1 - risk):
MACD_Sell.append(df["Adj Close"][i])
MACD_Buy.append(np.nan)
position = False
elif position == True and df['Adj Close'][i] < df['Adj Close'][i - 1] * (1 - risk):
MACD_Sell.append(df["Adj Close"][i])
MACD_Buy.append(np.nan)
position = False
else:
MACD_Buy.append(np.nan)
MACD_Sell.append(np.nan)
data['MACD_Buy_Signal_price'] = MACD_Buy
data['MACD_Sell_Signal_price'] = MACD_Sell
MACD_strategy = MACD_Strategy(data, 0.025)
def MACD_color(data):
MACD_color = []
for i in range(0, len(data)):
if data['MACDh_12_26_9'][i] > data['MACDh_12_26_9'][i - 1]:
MACD_color.append(True)
else:
MACD_color.append(False)
return MACD_color
data['positive'] = MACD_color(data)
plt.rcParams.update({'font.size': 10})
fig, ax1 = plt.subplots(figsize=(14,8))
fig.suptitle(stocksymbols[0], fontsize=10, backgroundcolor='blue', color='white')
ax1 = plt.subplot2grid((14, 8), (0, 0), rowspan=8, colspan=14)
ax2 = plt.subplot2grid((14, 12), (10, 0), rowspan=6, colspan=14)
ax1.set_ylabel('Price in ₨')
ax1.plot('Adj Close',data=data, label='Close Price', linewidth=0.5, color='blue')
ax1.scatter(data.index, data['MACD_Buy_Signal_price'], color='green', marker='^', alpha=1)
ax1.scatter(data.index, data['MACD_Sell_Signal_price'], color='red', marker='v', alpha=1)
ax1.legend()
ax1.grid()
ax1.set_xlabel('Date', fontsize=8)
ax2.set_ylabel('MACD', fontsize=8)
ax2.plot('MACD_12_26_9', data=data, label='MACD', linewidth=0.5, color='blue')
ax2.plot('MACDs_12_26_9', data=data, label='signal', linewidth=0.5, color='red')
ax2.bar(data.index,'MACDh_12_26_9', data=data, label='Volume', color=data.positive.map({True: 'g', False: 'r'}),width=1,alpha=0.8)
ax2.axhline(0, color='black', linewidth=0.5, alpha=0.5)
ax2.grid()
plt.show()
############# Bollinger Bands Implementation #############
def bb_strategy(data):
bbBuy = []
bbSell = []
position = False
bb = ta.bbands(data['Adj Close'], length=20,std=2)
data = pd.concat([data, bb], axis=1).reindex(data.index)
for i in range(len(data)):
if data['Adj Close'][i] < data['BBL_20_2.0'][i]:
if position == False :
bbBuy.append(data['Adj Close'][i])
bbSell.append(np.nan)
position = True
else:
bbBuy.append(np.nan)
bbSell.append(np.nan)
elif data['Adj Close'][i] > data['BBU_20_2.0'][i]:
if position == True:
bbBuy.append(np.nan)
bbSell.append(data['Adj Close'][i])
position = False #To indicate that I actually went there
else:
bbBuy.append(np.nan)
bbSell.append(np.nan)
else :
bbBuy.append(np.nan)
bbSell.append(np.nan)
data['bb_Buy_Signal_price'] = bbBuy
data['bb_Sell_Signal_price'] = bbSell
return data
#storing the function
data = bb_strategy(data)
#plot
fig, ax1 = plt.subplots(figsize=(14,8))
fig.suptitle(stocksymbols[0], fontsize=10, backgroundcolor='blue', color='white')
ax1 = plt.subplot2grid((14, 8), (0, 0), rowspan=8, colspan=14)
ax2 = plt.subplot2grid((14, 12), (10, 0), rowspan=6, colspan=14)
ax1.set_ylabel('Price in ₨')
ax1.plot(data['Adj Close'],label='Close Price', linewidth=0.5, color='blue')
ax1.scatter(data.index, data['bb_Buy_Signal_price'], color='green', marker='^', alpha=1)
ax1.scatter(data.index, data['bb_Sell_Signal_price'], color='red', marker='v', alpha=1)
ax1.legend()
ax1.grid()
ax1.set_xlabel('Date', fontsize=8)
ax2.plot(data['BBM_20_2.0'], label='Middle', color='blue', alpha=0.35) #middle band
ax2.plot(data['BBU_20_2.0'], label='Upper', color='green', alpha=0.35) #Upper band
ax2.plot(data['BBL_20_2.0'], label='Lower', color='red', alpha=0.35) #lower band
ax2.fill_between(data.index, data['BBL_20_2.0'], data['BBU_20_2.0'], alpha=0.1)
ax2.legend(loc='upper left')
ax2.grid()
plt.show()
################ The End #############