Skip to content

Instantly share code, notes, and snippets.

@bnsheehy
Last active January 17, 2022 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnsheehy/f78b005e1077c53834c4c1d6f19470b6 to your computer and use it in GitHub Desktop.
Save bnsheehy/f78b005e1077c53834c4c1d6f19470b6 to your computer and use it in GitHub Desktop.
RS Matrix Pseudocode
Set the parameter values:
boxSize = 0.035
reversalThreshold = 3
reversalAmount = boxSize x reversalThreshold = 0.105
historyDays = 180
lagTime = 0 days
endDate = today - lagTime
startDate = today - historyDays
Upload the ticker list
Create an empty dataframe for the plot symbols (Xs and Os)
Create an empty dataframe for the trend signals (BUY vs SELL)
Get the EOD close price history for each ticker symbol (I use Intrinio's API)
For each ticker on the ticker list as Ticker1:
Iterate through every other ticker on the list as Ticker 2 to:
Calculate the relative strength ratio of Ticker1 / Ticker2 for each date in the time series
Calculate the PnF stats, plot_symbol (Xs vs Os) and signal_name (BUYs vs SELLs) for each date for each RS ratio pair
# The algorithm for calculating the PnF stats is as follows:
Set the plot_symbol for the first day as X
Set the signal_name for the fist day as BUY
Set high_point = current ratio value
Set low_point = current ratio value
Set last_high_point = current ratio value
Set last_low_point = current ratio value
Set prev_high_point = current ratio value
Set prev_low_point = current ratio value
For each subsequent date:
If previous day’s plot_symbol = "X", then:
If current value >= previous value, then:
Today's plot_symbol = "X".
and copy yesterday's signal_name to today.
And if today's value is higher than the last high_point:
then make today's value the high_point,
and copy yesterday's signal_name to today.
And if today's value is higher than the high point from the last X column:
then today's signal_name = "BUY"
Else if today's value is less than the previous high_point times 1 - reversalAmount:
the plot_symbol reverses to "O",
and the low_point is today's value,
and reversal = 1,
and prev_high_point = last_high_point, saving this value to use in the Target value calc below
and last_high_point = most recent high_point
And if today's value is lower than the last_low_point from the last O column:
then today's signal = "SELL".
Else:
copy yesterday's signal to today
Else:
plot_symbol = "X" (value is down but not enough to trigger a reversal)
and copy yesterday's signal to today.
If previous plot_symbol = "O", then:
If current value <= previous value, then:
Today's plot_symbol = "O".
and copy yesterday's signal_name to today.
And if today's value is lower than the most recent low_point,
then make today's value the low_point.
and copy yesterday's signal_name to today.
And if today's value is lower than the last_low_point from the last O column,
then today's signal = "SELL".
Else if today's value is greater than the last_high_point, times 1 + reversalAmount:
the plot_symbol reverses to "X",
and the high_point is today's value,
and reversal = 1,
and prev_low_point = last_low_point, saving this value to use in the Target value calc below
and last_low_point = low_point
and if today's value is higher than the high point from the last X column:
then today's signal_name = "BUY".
Else copy yesterday's signal_name to today.
Else:
Plot Symbol = "O" (value is up but not enough to trigger a reversal)
and copy yesterday's signal_name to today.
Set high_point = current "high_point"
Set low_point = current "low_point"
Set last_high_point = current "last_high_point"
Set last_low_point = current "last_low_point"
Set prev_high_point = current "prev_high_point"
Set prev_low_point = current "prev_low_point"
Capture the most recent signal_name and save it to the trend signals dataframe for the RS pair
Capture the most recent plot_ymbol and save it to the plot symbol dataframe for the RS pair
Count the total number of BUYs for each ticker symbol
Count the total number of Xs for each ticker symbol
Create a data frame with the following columns: Ticker Symbol, Asset Name, Count of BUYs, Count of Xs, Total Count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment