Slicing a spreadsheet in Python terminal - python

So I'm making a program that asks a server a data and it send back. The problem is when I ask for some data it answer:
And I need just the value from the column "Close" . I can't figure out how to do it.
Code:
import yfinance as yf
import time
stock = yf.Ticker("MSFT")
data = stock.history(period="1")
oferta= stock.info["bid"]
print(data)

yfinance uses the pandas library to return data. You are receiving a pandas' DataFrame object in data, so to select only the "Close" column you can do this:
print(data["Close"].iloc[0])

Related

Is there a way to work around python to pull multiple tickers / symbols from binance and extract necessary information from them?

I would like to pull multiple tickers from Binance and have managed to do so and write them into a CSV file. However, I am having an issue pulling specific information from the columns to have the OHLCV data only and then work on wrapping ta-lib around this data.
For eg. I would like to keep the OHLCV data from each row for XRPBTC, NEOBTC which are in columns, and write them into a new file or just wrap ta-lib around the same data. It works fine for just one ticker but I'm having some troubles extracting this for multiple tickers.
I am given to understand that these are in the format of lists, can I split them to keep only OHLCV data and from each row and from each column and write them into a new file - is there an easier way of splitting a list?
screenshot of the data
Link to relevant binance documentation Klines candlestick data
import pandas as pd
import numpy as np
import csv
import talib as ta
from binance.client import Client
candlesticks = ['XRPBTC','NEOBTC'] # unable to split for each row in multiple columns
data = pd.DataFrame()
for candlestick in candlesticks:
data[candlestick] = client.get_historical_klines(candlestick, Client.KLINE_INTERVAL_15MINUTE, "1 Jul, 2021")
data.to_csv("XRPNEO15M.csv")
print(data)

Scanning Finviz Stocks

Hello here is a screener tool for Finviz but my stock_list result returns object of type Screener I am trying to put that into a data frame but I am having issues as the data is one long string divided by pipes. I tried to use str but method does not exists in the screener class. I am new to python this looks easy but I just dont know the proper syntax here can anyone help. Thank you!
import pandas as pd
import nest_asyncio
from finviz.screener import Screener
import csv
import sys
from datetime import datetime
nest_asyncio.apply()
filters = ['idx_sp500'] # Shows companies in NASDAQ which are in the S&P500
stock_list = Screener(filters=filters, order='price')
You could output the data to a csv file and read the file using pandas:
stock_list = Screener(filters=filters, order='price')
stock_list.to_csv(filename="stocks.csv")
df = pd.read_csv("stocks.csv")

Update Excel Spreadsheet with Real-Time Python Data

I'm quite new to Python and have mostly targeted learning the language exactly to automate some processes and update / populate excel spreadsheets with realtime data. Is there a way (e.g. through openpyxl) to update specific cells with data that's extracted through python packages such as pandas or web scraping through BeautifulSoup ?
I already have the necessary code to extract the data-series that I need for my project in Python but am stuck entirely on how to link this data to excel.
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
start = dt.datetime(2000,1,1)
end = dt.datetime.today()
tickers = [
"PYPL",
"BABA",
"SAP"
]
df = web.DataReader (tickers, 'yahoo', start, end)
print (df.tail(365)['Adj Close'])
Pandas has a method to export a Dataframe to Excel. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html
filename = "output.xlsx"
df.to_excel(filename)
One option is to run your python script run on a schedule and output to .csv or another format that Excel can link to. This option allows the data to be updated whenever the python script is executed.
Setup:
Output your dataframe to csv/database or other Excel readable format
Setup your python file to run on a schedule (either by scheduling, or a loop with a delay)
Create a data connection from Excel to your python outputted file/database
Build pivot tables based on table in Excel
Refresh data connection/pivot tables in Excel to get the new data
(Appreciate that this is an old question). Real time data in Excel is possible with xlOil. xlOil allows you to very easily define an Excel RTD (real time data) function in python. Excel's RTD functions operate outside the normal calc cycle and can push data onto a sheet.
Your example could be written as:
import xloil, datetime as dt, asyncio
import pandas_datareader.data as web
start = dt.datetime(2000,1,1)
#xloil.func
async def pyGetTickers(names:list, fetch_every_secs=10):
while True:
yield web.DataReader(
names, 'yahoo', start, dt.datetime.now())
await asyncio.sleep(fetch_every_secs)
Which would appear as a worksheet function pyGetTickers.
One easy solution is using xlwings library
import xlwings as xw
..
xw.Book(file_path).sheets['Sheet_name'].range('A1').value = df
this would print out your df to cell A1 of an excel file, via COM - which means it actually writes the values while file is open.
Hope this is helpful

Cleaning up text from a URL as a CSV, then representing it as a Pandas Dataframe

import csv
import numpy as np
import pandas as pd
import urllib.request
import time
x = urllib.request.urlopen("https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,GBPJPY,USDJPY,AUDUSD,&api_key=KEY")
df = pd.read_csv(x,header=None, sep=',',
infer_datetime_format=True)
starttime=time.time()
while True:
print (df)
time.sleep(60.0 - ((time.time() - starttime) % 60.0))
I wrote this code with the intent of pulling data from the URL and place it within the Pandas Dataframe. Then, minute by minute, updating the information to the Dataframe from the URL with the index of time. Currently i'm able to pull the raw data to the dataFrame but when he information is being called by the timer I made, it repeats whats been called before and not updating. The data that i'm getting is also very convoluted and messy, so I have not been able to even index time to begin with.
If I could be pointed in the direction of where i can learn how to clean the information in the datframe and how to call the data thats updated when put to the dataframe, it would be much appreciated. Thanks for reading!
Looks like the data from the site is in JSON format.
Also your action of pulling the data was outside the while loop, so you only pulled once but printed every minute. Try this:
import pandas as pd
import time
while True:
df = pd.read_json("https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,GBPJPY,USDJPY,AUDUSD,&api_key=KEY")
print (df)
time.sleep(60)
Firstly, that looked like a real API key, which you should not share.
The code you shared does not request the URL repeatedly. Only the lines in the while True loop will repeatedly execute. In your code, these are the lines that make a request and establish a DataFrame from the response:
x = urllib.request.urlopen("https://forex.1forge.com/1.0.3/quotes?pairs=EURUSD,EURJPY,GBPUSD,USDCAD,GBPJPY,USDJPY,AUDUSD,&api_key=KEY")
df = pd.read_csv(x,header=None, sep=',',
infer_datetime_format=True)
Edit: as regards your question about how to begin cleaning data, the pandas official cheat sheet is not bad in my opinion.

pandas yahoo finance real time data

Is it possible to get real time stock data with pandas from yahoo finance?
For historical data i would do the following:
import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL')
Is there a way to get the latest aapl price?
EDIT:
Yahoo has ended their free finance API so this answer is no longer relevant. Below is my answer for pre-2019 purposes.
Archival:
There's plenty of libraries available for this. Pandas doesn't explicitly do this though.
Most simply, I would suggest you just use a web library to download yahoo data. I like using requests, but you could also use urllib. You can coerce the response into a data frame after you get it.
import requests
requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&e=.csv&f=nsl1op").text
The nsl1op format var is from the docs:
I will use here the tags of name(n), symbol(s), the latest value(l1), open(o) and the close value of the last trading day(p)
Your response data should look something like
u'"Apple Inc.","AAPL",114.63,113.50,115.07\r\n'
You can just split up the string using the csv library and throw it into a data frame from there
To answer your question about using Pandas specifically, you can pull stock data from yahoo using pandas like so:
from pandas.io.data import DataReader
from datetime import datetime
aapl = DataReader('AAPL', 'yahoo', datetime(2015,7,1), datetime(2015,7,1))
print(aapl['Adj Close'][0])
This code results in:
126.599998
The other keys you can use are Open, Close, High, Low, and Volume.
Keep in mind that the returned dataset is an array. You need to enumerate said array to get the data, either by specifying your index, or with a for loop.
You can use below code to get all info from yahoo finace api:
import pandas as pd
from pandas_datareader import data as wb
aapl=wb.DataReader('AAPL',start='2015-1-1',data_source='yahoo')
print(aapl)

Categories

Resources