Python Program to Predict Any Forex Pair price using RSI and Moving Average Crossover

Here is a simple Python program that uses OHLC (Open, High, Low, Close) data from a CSV file to predict the price of a forex pair using moving averages and the Relative Strength Index (RSI):

Predict forex pair prices using Python and OHLC data. Our program utilizes moving averages and the Relative Strength Index to accurately forecast market trends and make informed trading decisions. With our straightforward and reliable Python code, you can confidently navigate the complex world of foreign exchange trading.

import csv

import numpy as np

from scipy import signal

# Load the OHLC data from the CSV file

ohlc = []

with open('forex_data.csv', 'r') as csvfile:

    reader = csv.reader(csvfile)

    next(reader)  # Skip the header row

    for row in reader:

        ohlc.append([float(row[1]), float(row[2]), float(row[3]), float(row[4])])

# Convert the data to a NumPy array

ohlc = np.array(ohlc)

# Calculate the 50-day moving average of the close prices

ma50 = signal.convolve(ohlc[:, 3], np.ones((50,))/50, mode='valid')

# Calculate the 200-day moving average of the close prices

ma200 = signal.convolve(ohlc[:, 3], np.ones((200,))/200, mode='valid')

# Calculate the RSI

deltas = np.diff(ohlc[:, 3])

seed = deltas[:50]

up = seed[seed >= 0].sum()/50

down = -seed[seed < 0].sum()/50

rs = up/down

rsi = np.zeros_like(ohlc[:, 3])

rsi[:50] = 100. - 100./(1.+rs)

for i in range(50, len(ohlc[:, 3])):

    delta = deltas[i-1]  # cause the diff is 1 shorter

    if delta > 0:

        upval = delta

        downval = 0.

    else:

        upval = 0.

        downval = -delta

    up = (up*49 + upval)/50

    down = (down*49 + downval)/50

    rs = up/down

    rsi[i] = 100. - 100./(1.+rs)

# Predict the price based on the moving averages and RSI

if ma50[-1] > ma200[-1] and rsi[-1] > 70:

    print("Buy: the 50-day moving average is above the 200-day moving average and the RSI is overbought")

elif ma50[-1] < ma200[-1] and rsi[-1] < 30:

    print("Sell: the 50-day moving average is below the 200-day moving average and the RSI is oversold")

else:

    print("Hold: the 50-day moving average is within the 200-day moving average and the RSI is not overbought or oversold")


Comments