from the Moving Average algorithm, you’ll now try to develop a more sophisticated algorithm to outdo it! With this algorithm you will make trading decisions with Relative Strength Index, or RSI. How does this algorithm compare to your moving average algorithm? Algorithm Description The relative strength index (RSI) is defined as a momentum indicator that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock. The index is calculated by the average gain and average loss over a rolling time window. ???????????=?????????/????????AverageGain=TotalGain/Lookb

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter19: Probabilistic Dynamic Programming
Section19.4: Further Examples Of Probabilistic Dynamic Programming Formulations
Problem 7P
icon
Related questions
Question

Index (RSI) Algorithm

Building on the momentum from the Moving Average algorithm, you’ll now try to develop a more sophisticated algorithm to outdo it! With this algorithm you will make trading decisions with Relative Strength Index, or RSI.

How does this algorithm compare to your moving average algorithm?

Algorithm Description

The relative strength index (RSI) is defined as a momentum indicator that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock. The index is calculated by the average gain and average loss over a rolling time window.

  • ???????????=?????????/????????AverageGain=TotalGain/Lookback

    • ?????Gains: When there was a price increase compared to the previous day, the gain is equal to that price gain value, otherwise set to zero.

    • ?????????TotalGain: The sum of the gains over the lookback period.

    • ????????Lookback: The number of days of previous data to look back.

  • ???????????=?????????/????????AverageLoss=TotalLoss/Lookback

    • ??????Losses: When there was a price decrease compared to the previous day, the loss is equal to that price loss value, otherwise set to zero.

    • ?????????TotalLoss: The sum of the losses over the lookback period.

    • ????????Lookback: The number of days of previous data to look back. The typically used value for the “lookback” is 14 or 21 days.

  • ???=100−(100/(1+(???????????/???????????)))RSI=100−(100/(1+(AverageGain/AverageLoss)))

Your task is to develop a Python program to calculate the RSI and see if the market is trending towards a positive or a negative momentum associated with a stock. In your code, a trading decision needs to be made based on RSI.

  • Buying position: When the RSI for the stock is below 30, it indicates a good buying position. This signifies that the market is oversold.

  • Selling position: When the RSI for the stock is above 70, it indicates a good selling position. This signifies that the market is overbought.

On each day, you need to make trading decisions on two distinct data files based on the criteria above. You can test using AAPL.csv and MSFT.csv, but I may test with any two stock data files from random companies.

You can trust that any tested stock files will have the same date ranges for the data.

A starting balance of $10,000 is recommended.

Additional Requirements¶

  1. For each company, you must buy, or sell 10 stocks, or less per transaction.

  2. You are free to choose which column of stock data to use (open, close, low, high, etc).

  3. When your algorithm reaches the last day of data, have it sell all remaining stock. Your function will return the number of stocks you own (should be zero, at this point), and your cash balance.

  4. You are not allowed to “look into the future” in your data. Your simulation has the data for the current day, and the past.

  5. You are not allowed to use test_data() directly in Milestone III, so be sure to write new functions that use what you learned from writing that function.

  6. You must use the transact() function you wrote in Milestone I, as it will help you make some of the basic decisions for buying and selling you will undoubtedly make.

def alg_rsi(filename_1, filename_2): """This function implements the Relative Strength Index algorithm. Using the CSV stock data from two stock files that are loaded into your program, use that data to make decisions using the Relative Strength Index (RSI) algorithm. Algorithm: [REPLACE THIS WITH A CLEAR DESCRIPTION OF THE ALGORITHM.] [I AM LEAVING THIS YOUR RESPONSIBILITY TO FILL OUT.] Arguments: filename_1 (str): A filename, as a string, for one set of stock data for a first company. filename_2 (str): A filename, as a string, for one set of stock data for a second company. Returns: Two values, stocks and balance OF THE APPROPRIATE DATA TYPE. Prints: Nothing. """ # Last thing to do, return two values: one for the number of stocks you # end up owning after the simulation, and the amount of money you have # after the simulation. Remember, all your stocks should be sold at the # end! return stocks_owned, cash_balance
 
AAPL

Date

Open

High

Low

Close

Adj Close

Volume

2000-01-03

3.745536

4.017857

3.631696

3.997768

2.695920

133949200

2000-01-04

3.866071

3.950893

3.613839

3.660714

2.468626

128094400

2000-01-05

3.705357

3.948661

3.678571

3.714286

2.504751

194580400

2000-01-06

3.790179

3.821429

3.392857

3.392857

2.287994

191993200

2000-01-07

3.446429

3.607143

3.410714

3.553571

2.396373

115183600

2000-01-10

3.642857

3.651786

3.383929

3.491071

2.354226

126266000

2000-01-11

3.426339

3.549107

3.232143

3.312500

2.233805

110387200

 

Date

Open

High

Low

Close

Adj Close

Volume

2000-01-03

58.687500

59.312500

56.000000

58.281250

42.641369

53228400

2000-01-04

56.781250

58.562500

56.125000

56.312500

41.200928

54119000

2000-01-05

55.562500

58.187500

54.687500

56.906250

41.635361

64059600

2000-01-06

56.093750

56.937500

54.187500

55.000000

40.240646

54976600

2000-01-07

54.312500

56.125000

53.656250

55.718750

40.766510

62013600

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr