Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stock screening
#1
Hi all,

I´m pretty new to the programming at all but I want to programm my own stock screeners and let python create an excel with all the stocks which meet my criteria. I programmed the past 2 weeks a screener, where I choose an excel file on my pc with all US stock tickers and look for criteria like
- currrent price > 50 day moving average or
- 150 day movin average > 200 day MA and so on ...

Now I want to translate the criteria that can be seen in the screenshot into Python. But I absolutely do not know how. The code should again check a file I selected with the stocks, and finally create a new Excel with the stocks that match the criteria. I already know the 2 steps. But how the rest works I do not know.

Thank you.

Attached Files

Thumbnail(s)
   
Reply
#2
You could purchase the following book which could give you an excellent foundation to start on:
https://www.amazon.com/dp/1492024333

Used copies are available on Amazon.
Reply
#3
Hi Keelo61,

See the initial code below.

import yfinance as yf

ticker = "AAPL"
date1 = '2021-01-01'   # start and end date 
df=yf.download(ticker,date1)

def SMA(data, period=50):
    return data['Close'].rolling(window=period).mean()
#
df['SMA50']=SMA(df,50)
df['SMA150']=SMA(df,150)
df['SMA200']=SMA(df,200)

df2=df.drop(columns=['Open','High','Low','Volume'])
df3=df2.dropna()

#- currrent price > 50 day moving average or
#- 150 day movin average > 200 day MA and so on ...
df4=df3[[any([a, b]) for a, b in zip(df3.Close>df3.SMA50, df3.SMA150 >df3.SMA200)]]

df4.head()
Out[954]: 
                 Close   Adj Close       SMA50      SMA150     SMA200
Date                                                                 
2021-10-18  146.550003  145.935806  147.055599  137.554133  135.63210
2021-10-19  148.759995  148.136536  147.108999  137.714133  135.72885
2021-10-20  149.259995  148.634430  147.182199  137.905666  135.82010
2021-10-21  149.479996  148.853516  147.254599  138.102266  135.93450
2021-10-22  148.690002  148.066849  147.250599  138.270933  136.02335
Reply
#4
paulyan Wrote:This thread is
FYI: This thread is 7+ months old.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020