How to select the high value in a csv BUT not considering today high value

Hi there, I have a csv containing values of a stock (high, low, open, close, min, max). 12 rows (one per every market day).
I need to establish if the max of today in higher than the previous 11 (so excluding itself).

I’m only able to establish the max of ALL 12 days:

with open(‘/Users/fabry/upovermax12gg/BMPS.MI.csv’):
max12gg=df[‘High’].max()

Thank you in advance for help,
Fabry

Hi,

have you tried creating a running list of the previous 11 days and then looping through them as you currently appear to be doing for 12 days?

Do you mean to create a second file csv with only the 11 previous day?

To be clear, you’re using Pandas?

Please read the pinned thread to make sure you can format the code properly (so we can see what you have in mind properly), and make sure to show code that actually reflects what you have working so far.

yes, I’m using Pandas

Well, if you already have 12 days (including the present day) that you are looping, I take it the 12 days are provided in some kind of list or some derivate of that, if you create a secondary list:

# original list
some_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

# option 1 - create a secondary list of the previous 11 days through slicing
# then use that secondary list for looping to obtain your desired results
edited_list = some_list[0:-1]
print(edited_list)

You can also attempt it via the following method

# option 2
day_num = 0
for day in some_list:  # using your original list
    print(day)
    # here you place your function to run to process each day
    day_num += 1

    # once you enter the 12th day, you break and exit thereby only
    # processing the previous 11 days
    if day_num == 11:
        break

These are general simplified approaches to give you ideas on how you can potentially tackle this problem.

Isn’t comparing max value of the whole column to the last value an option?

Yes, definitely.

In the csv file there are all the high prices of the stock of the last 12 days. I must establish the highest between the high prices BUT only of the first 11 days. The 12th high (the today high price), must be compared with the highest of the previous 11 and if it’s higher the name of the stock will be written in a new csv file.

I do not have a list, I have a csv file containing the values of a stock of the last 12 days. I wish to avoid to create a second file just to calculate the today highest price.


When entering code, please use the following protocol:

This helps so that we can copy and paste your code for testing otherwise we have to manually retype your code (help us help you). Additionally, it also helps with formatting when you’re entering the code as it makes it appear as it does on your display.

From the following code snippet:

for stock in listatitoli:
    stock_data = yf.download(stock, period='12d')
    df = pd.DataFrame(stock_data)
    df.to_csv(stock + ".csv", index=False)

Prior to it being processed by panda (the last two lines shown here), you have to filter out the current day (since you only want the previous 11 days). Once having done so, then you can continue with the panda instructions. How does the stock_data appear when printed? Once you have determined this, then you can proceed to create code to remove the most current day prior to panda processing.

By the way, I am not familiar with the following instruction (particularly yf):

stock_data = yf.download(stock, period='12d')

For period='12d, is this a general string, or does it define an actual period? If it defines an actual period, why don’t you change it to period='11d? Does it allow you to define a start and a stop day?

Please provide additional information.

By the way, this code snippet only works as written because you are only processing one stock in listatitoli. If you had more than one stock, the previous stock processed in the for loop would be overwritten by the most recent one being processed.

Ok, thank you, I will use the back ticks, sorry.

About your question:
yf means for Yahoo Finance; I download from it the values inside the csv files.

period='12'

define the last 12 days. E.g. today is the 4th of July, so Python will download the last 12 days, therefore from the 19th of July (only working days).

" Prior to it being processed by panda (the last two lines shown here), you have to filter out the current day (since you only want the previous 11 days)."
This is my problem. I don’t know how to say to Python to compare the today High price to the highest of the previous 11 days.
At the same time it must establish which is the highest price of the last 11 days and if the today high price is higher or not than the higher price of the last 11 days.

" why don’t you change it to period='11d ?"
Because if I do it I will not able to have the today high price.
My goal is to not have two files to compare (one for the previous 11 days and one for today). I want only one csv file.

" By the way, this code snippet only works as written because you are only processing one stock in listatitoli . If you had more than one stock, the previous stock processed in the for loop would be overwritten by the most recent one being processed."
Yes, I know. This will be the next step. For now I need to solve the problem for one stock.

Thank you!
Fabrizio

Likewise, I don’t know how to approach this problem until I see how it is being downloaded / copied into the variable stock_data. Can you display the contents of this variable? Once I have this information, I can test it at my end to verify that the proposed method works.

Can you please paste it per formatting? So that I can just copy it.

You are only interested in comparing these values: The highest of the eleven values in the blue box with the one in the red box (the twelfth day).

Apparently, once saving it to the pandas dataframe, it becomes very easy for processing:

max_values_11_days = []

# Only append the first 11 values from column 1
for row in range(len(df) - 1):

    max_values_11_days.append(df.iat[row, 1])

max_eleven_days = max(max_values_11_days)
max_twelveth_day = df.iat[11, 1]

Here, I am assuming that the dates are not considered as part of the data. Thus, the column index 0 begins with the open column. The high column is column index 1. If the dates column is considered as part of the data, then change the 1 to a 2 in the statement inside the for loop.

Once you have these two values, you can proceed with your comparison code.

it works!!! Thank you VERY much!!!

Alternative solution:

+>>> df = pd.DataFrame(range(4), columns=['values'])
+>>> df
   values
0       0
1       1
2       2
3       3
+>>> df['values'].iloc[:-1]         # get column values except last row
0    0
1    1
2    2
Name: values, dtype: int64
+>>> df['values'].iloc[:-1].max()   # get max value from column except last value
2
+>>> df['values'].iloc[-1]          # get last value
3