EOD data von ALV (Dax30)
---------------------------------
dieses Beispiel als scriplet erzeugt aus Datenreihen buy/sell Signale
mit grafischer Darstellung.
Lauffähiges file für Python IDLE oder Notebook jupyter im Anhang:
Charts ebenso
---------------------------------
Hat jemand im Forum py script für Optmierungsläufe der Parameter ?
feedback jeder Art wäre hilfreich
---------------------------------
py_macd_buy_sell_signal.txt --> *.txt in *.py umbenennen
(file ist virenfrei)
--------------------------------------------------------------------
Besprechung zur Generierung von crossover Signalen
--------------------------------------------------------------------
für Datenreihen aus: --High, Low, Close, Adj Close, Volume
für Indikator-Reihen z.B. : -- macd, sma, ema, etc.....
--------------------------------------------------------------------
scriplet:
erzeugen eines macd buy/sell signals :
###-------------------------------------------------------
sma12 = df['Adj Close'].rolling(12).mean()
sma26 = df['Adj Close'].rolling(26).mean()
MACD = sma12 -sma26
signal = MACD.ewm(span=9, adjust=False).mean()
###-------------------------------------------------------
# create new columns
df['MACD'] = MACD
df['Signal Line'] = signal
#-----------------------------
### generate buy sell signal from macd crossover
def buy_sell(signal):
Buy=[]
Sell=[]
flag=-1
for i in range(0,len(signal)):
if signal['MACD'][i] > signal['Signal Line'][i]:
Sell.append(np.nan)
if flag != 1:
Buy.append(signal['Close'][i])
flag = 1
else:
Buy.append(np.nan)
#------------------------------------------------
elif signal['MACD'][i] < signal['Signal Line'][i]:
Buy.append(np.nan)
if flag != 0:
Sell.append(signal['Close'][i])
flag = 0
else:
Sell.append(np.nan)
else:
Buy.append(np.nan)
Sell.append(np.nan)
return(Buy, Sell)
#-------------------------------------------------
# store macd buy and sell signal
a = buy_sell(df)
df['Buy_Signal']= a[0]
df['Sell_Signal']= a[1]
#-------------------------------------------------
# visually show stock buy sell data
plt.figure(figsize=(10,4.5))
plt.scatter(df.index,df['Buy_Signal'], color='green',label='macd Buy', marker='^',alpha=1)
plt.scatter(df.index,df['Sell_Signal'], color='red',label='macd Sell', marker='v',alpha=1)
plt.plot(df['MACD'], label='SMA26', alpha = 0.9)
plt.plot(df['Signal Line'], label='Close Price', alpha = 0.9)
plt.plot(df['Close'], label='Close Price', alpha = 0.9)
titel = symb1 + ' Price ' + '( Dax30 )' + ' \n' + 'macd buy/sell signal on close '
plt.title(titel)
plt.xlabel('Date as index -> 2.1.20 - 28.8.20')
plt.ylabel('Close Price')
plt.legend(loc='upper left')
plt.show()
#-------------------------------------------------
---------------------------------
dieses Beispiel als scriplet erzeugt aus Datenreihen buy/sell Signale
mit grafischer Darstellung.
Lauffähiges file für Python IDLE oder Notebook jupyter im Anhang:
Charts ebenso
---------------------------------
Hat jemand im Forum py script für Optmierungsläufe der Parameter ?
feedback jeder Art wäre hilfreich
---------------------------------
py_macd_buy_sell_signal.txt --> *.txt in *.py umbenennen
(file ist virenfrei)
--------------------------------------------------------------------
Besprechung zur Generierung von crossover Signalen
--------------------------------------------------------------------
für Datenreihen aus: --High, Low, Close, Adj Close, Volume
für Indikator-Reihen z.B. : -- macd, sma, ema, etc.....
--------------------------------------------------------------------
scriplet:
erzeugen eines macd buy/sell signals :
###-------------------------------------------------------
sma12 = df['Adj Close'].rolling(12).mean()
sma26 = df['Adj Close'].rolling(26).mean()
MACD = sma12 -sma26
signal = MACD.ewm(span=9, adjust=False).mean()
###-------------------------------------------------------
# create new columns
df['MACD'] = MACD
df['Signal Line'] = signal
#-----------------------------
### generate buy sell signal from macd crossover
def buy_sell(signal):
Buy=[]
Sell=[]
flag=-1
for i in range(0,len(signal)):
if signal['MACD'][i] > signal['Signal Line'][i]:
Sell.append(np.nan)
if flag != 1:
Buy.append(signal['Close'][i])
flag = 1
else:
Buy.append(np.nan)
#------------------------------------------------
elif signal['MACD'][i] < signal['Signal Line'][i]:
Buy.append(np.nan)
if flag != 0:
Sell.append(signal['Close'][i])
flag = 0
else:
Sell.append(np.nan)
else:
Buy.append(np.nan)
Sell.append(np.nan)
return(Buy, Sell)
#-------------------------------------------------
# store macd buy and sell signal
a = buy_sell(df)
df['Buy_Signal']= a[0]
df['Sell_Signal']= a[1]
#-------------------------------------------------
# visually show stock buy sell data
plt.figure(figsize=(10,4.5))
plt.scatter(df.index,df['Buy_Signal'], color='green',label='macd Buy', marker='^',alpha=1)
plt.scatter(df.index,df['Sell_Signal'], color='red',label='macd Sell', marker='v',alpha=1)
plt.plot(df['MACD'], label='SMA26', alpha = 0.9)
plt.plot(df['Signal Line'], label='Close Price', alpha = 0.9)
plt.plot(df['Close'], label='Close Price', alpha = 0.9)
titel = symb1 + ' Price ' + '( Dax30 )' + ' \n' + 'macd buy/sell signal on close '
plt.title(titel)
plt.xlabel('Date as index -> 2.1.20 - 28.8.20')
plt.ylabel('Close Price')
plt.legend(loc='upper left')
plt.show()
#-------------------------------------------------