Python

Python ile RSI MACD EMA SMA

Python ile RSI MACD EMA SMA değerlerini hesaplayan fonksiyonlar

import pandas as pd
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
plt.style.use("ggplot")
dataset=yf.download("TSLA",start='2018-01-03', end="2019-12-31")
dataset=dataset.dropna()
def SMA(data,period=30, column='Adj Close'):
    return data[column].rolling(window=period).mean() #rollingin ortalaması

def EMA(data,period=20,column='Adj Close'):
    return data[column].ewm(span=period, adjust=False).mean()  #üstel ağırlığın ortalaması

def MACD(data,period_long=26, period_short=12, period_signal=9,column='Adj Close'):
    #short hesaplayalım
    ShortEMA=EMA(data,period_short,column=column)
    #long
    LongEMA=EMA(data,period_long,column=column)
    #MACD
    data["MACD"]=ShortEMA-LongEMA
    #Signal
    data["Signal_Line"]=EMA(data,period_signal,column="MACD")
    
    return data

def RSI(data,period=14 , column="Close"):
    delta=data[column].diff(1)
    delta=delta[1:] #ilksatırı aldık
    up=delta.copy() #kopyaladık
    down=delta.copy()
    up[up<0]=0
    down[down>0]=0
    data["up"]=up
    data["down"]=down
    AVG_Gain=SMA(data,period,column='up')
    AVG_Loss=abs(SMA(data,period,column='down'))
    RS=AVG_Gain/AVG_Loss
    RSI=100.0-(100.0/(1.0+RS))
    data["RSI"]=RSI   
    return data

 

0 0 vote
Article Rating
Subscribe
Bildir
guest
1 Yorum
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
fatih
1 ay önce

rsi da saatlik dakikalık dilimi nasıl ayarlayacağız peki ?