Created
July 1, 2020 04:42
-
-
Save cameronShadmehry/d16ddd90588533fb39ecc0f2bff94643 to your computer and use it in GitHub Desktop.
Code to conduct an OBV analysis on our stored historical data sets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# OBV Analysis, feel free to replace this section with your own analysis ------------------------------------------------------------------------- | |
list_files = (glob.glob("<Your Path>\\Daily_Stock_Report\\Stocks\\*.csv")) # Creates a list of all csv filenames in the stocks folder | |
new_data = [] # This will be a 2D array to hold our stock name and OBV score | |
interval = 0 # Used for iteration | |
while interval < len(list_files): | |
Data = pd.read_csv(list_files[interval]).tail(10) # Gets the last 10 days of trading for the current stock in iteration | |
pos_move = [] # List of days that the stock price increased | |
neg_move = [] # List of days that the stock price increased | |
OBV_Value = 0 # Sets the initial OBV_Value to zero | |
count = 0 | |
while (count < 10): # 10 because we are looking at the last 10 trading days | |
if Data.iloc[count,1] < Data.iloc[count,4]: # True if the stock increased in price | |
pos_move.append(count) # Add the day to the pos_move list | |
elif Data.iloc[count,1] > Data.iloc[count,4]: # True if the stock decreased in price | |
neg_move.append(count) # Add the day to the neg_move list | |
count += 1 | |
count2 = 0 | |
for i in pos_move: # Adds the volumes of positive days to OBV_Value, divide by opening price to normalize across all stocks | |
OBV_Value = round(OBV_Value + (Data.iloc[i,5]/Data.iloc[i,1])) | |
for i in neg_move: # Subtracts the volumes of negative days from OBV_Value, divide by opening price to normalize across all stocks | |
OBV_Value = round(OBV_Value - (Data.iloc[i,5]/Data.iloc[i,1])) | |
Stock_Name = ((os.path.basename(list_files[interval])).split(".csv")[0]) # Get the name of the current stock we are analyzing | |
new_data.append([Stock_Name, OBV_Value]) # Add the stock name and OBV value to the new_data list | |
interval += 1 | |
df = pd.DataFrame(new_data, columns = ['Stock', 'OBV_Value']) # Creates a new dataframe from the new_data list | |
df["Stocks_Ranked"] = df["OBV_Value"].rank(ascending = False) # Rank the stocks by their OBV_Values | |
df.sort_values("OBV_Value", inplace = True, ascending = False) # Sort the ranked stocks | |
df.to_csv("<Your Path>\\Daily_Stock_Report\\OBV_Ranked.csv", index = False) # Save the dataframe to a csv without the index column |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment