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
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¶
-
For each company, you must buy, or sell 10 stocks, or less per transaction.
-
You are free to choose which column of stock data to use (open, close, low, high, etc).
-
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.
-
You are not allowed to “look into the future” in your data. Your simulation has the data for the current day, and the past.
-
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.
-
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.
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 |
Trending now
This is a popular solution!
Step by step
Solved in 4 steps