Python Mini Projects for Everyone-2

Is it time to sell or buy ?

Kaan ÇUKUR
3 min readNov 17, 2022
Image took from https://www.marketbeat.com/

In the second part of “Python Mini Projects for Everyone” I would like to do some kind of stock price alarm with python.

First of all we need live stock price values.In this project I used Yahoo Finance API . Let’s start with install and explore our API.

!pip install yFinance # İlk önce Yahoo finance API mizi indiriyoruz.
import yfinance as yf

We need to choose stock which will we want to notify. I want to follow USD/TRY parity so need to check special code of parity from website.

Symbol of USD/TRY parity

I need live data so values of today enough for me. In here we will get today’s data with 1 minute intervals. Data table will be Pandas DataFrame.

#Günlük periyotta 1 dakikalık aralıklarla veriyi çekiyorum.
df= yf.download(tickers='TRY=X', period='1d', interval='1m')
type(data)
#Verim Pandas Dataframe olarak geliyor.

Let’s look at our data table. Last row of df is my live data.

So column name don’t important, i can take any column’s data for use. If you want you can change decimal in here.

close_value=(df['Close'].iloc[len(df['Close'])-1])
#Close sutünunun son verisini kapanış değeri olarak atıyorum.
print(close_value)

Before write a function just i need one more thing, time of the data request. One more simple code for system date.

!!! Need to pay attention,if you are writing on Google Colab.It’s possible to local time cant be same.

from datetime import datetime

#datetime tüm zaman birimlerini kapsamaktadır.
now = datetime.now()
#Veri saniyeye kadar indiği için bende buraya kadar alıyorum.
sys_date = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", sys_date)

My conditions will be basic, i have a buy and sell points. If price will be equal or greater which i want, it’s time to sell and take a profit time. If price equal or less than which i want, it means stock price is cheap and it’s time to buy. If my conditions not okey, i want to see price and time.

def set_alarm(min_price,max_price):
import yfinance as yf
from datetime import datetime #Yahoo finance ve datetime tekrardan import edilmesi
usd_try = yf.Ticker("TRY=X") # İstenilen birim veya hisse kodunun yerleştirilmesi
while True: # Fonksiyonun sonsuz döngüye sokulması
data = yf.download(tickers='TRY=X', period='1d', interval='1m')
close_value=(data['Close'].iloc[len(data['Close'])-1]) #anlık kapanış değerinin alınması
now = datetime.now()
sys_date = now.strftime("%d/%m/%Y %H:%M:%S") # Güncel sistem zamanının alınması
if close_value>=max_price: # max fiyat için koşul
print("Price is " + str(close_value) + " . Time to sell. " +str(sys_date))
break
elif close_value<=min_price: # min fiyat için koşul
print("Price is " + str(close_value) + " . Time to buy. " +str(sys_date))
break
else:
print("Value of TRY is : " + str(close_value) +" at " +str(sys_date)) # koşullar sağlanmadıkça anlık değeri ve zamanı çıktı olarak görmek istiyorum.





Let’s see how is output look like .

set_alarm(18.58,18.6)

For the third project i was thinking to do send mail on Python and merge with this project. If you see some mistake or have any suggestion, please notify me.

You can see the full code on my github profile.

--

--