Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python rsi trading strategy

import yfinance as yf
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
import pandas as pd


def download_data(stock, start, end):
    data = {}
    ticker = yf.download(stock, start, end)
    data['Price'] = ticker['Adj Close']
    return pd.DataFrame(data)


if __name__ == '__main__':

    start_date = dt.datetime(2015, 1, 1)
    end_date = dt.datetime(2020, 1, 1)

    stock_data = download_data('IBM', start_date, end_date)

    stock_data['Return'] = np.log(stock_data['Price'] / stock_data['Price'].shift(1))
    stock_data['Move'] = stock_data['Price'] - stock_data['Price'].shift(1)

    stock_data['Up'] = np.where(stock_data['Move'] > 0, stock_data['Move'], 0)
    stock_data['Down'] = np.where(stock_data['Move'] < 0, stock_data['Move'], 0)

    stock_data['Average_Gain'] = stock_data['Up'].rolling(14).mean()
    stock_data['Average_Loss'] = stock_data['Down'].abs().rolling(14).mean()

    RS = stock_data['Average_Gain'] / stock_data['Average_Loss']

    stock_data['RSI'] = 100.0 - (100.0 / (1.0 + RS))

    stock_data = stock_data.dropna()

    print(stock_data)
    plt.plot(stock_data['RSI'])
    plt.show()
 
PREVIOUS NEXT
Tagged: #Python #rsi #trading #strategy
ADD COMMENT
Topic
Name
5+5 =