Commit 1f0d9753 by Patryk Czarnik

jupyter koniec i analiza w Pycharm

parent 513fe57c
import pandas
# emps = pandas.read_csv('emps.csv', sep=';')
emps = pandas.read_csv('emps.csv', sep=';', index_col='employee_id', parse_dates=['hire_date'])
print('Dane wczytane')
print(emps)
print('-'*80)
print('Średnia pensja:', emps.salary.mean())
print('Średnia pensja programistów:', emps[emps.job_title == 'Programmer'].salary.mean())
print('='*80)
# bierzemy rekord o ID = 100
king = emps.loc[100]
print(king)
print()
print(king.first_name, king.last_name, 'zarabia', king.salary)
print(king["first_name"], king["last_name"], 'zarabia', king["salary"])
# Zapytaj o kod spółki, początkową i końcową datę
# (format dla dat: YYYYMMDD)
# *dla chętnych: gdy user nie poda daty końcowej, przyjmij datę dzisiejszą
# Gdy już pobierzesz dane:
# - zapisz do pliku csv (nazwa pliku niech zawiera kod oraz daty)
# - podziel dane na miesiące (resample) i np. oblicz średni kurs zamknięcia, ew. dodatkowe pola jak robiliśmy w Jupyterze
# - wygeneruj wykres liniowy i zapisz do pliku
# → dla chętnych dwie wersje: wykres czterech kursów (otwarcie, zamknięcie, min, max) oraz oddzielnie wstega Belingera
import pandas as pd
from datetime import date
ticker = input('Podaj symbol, np. pkn albo usdpln: ')
pocz = input('Podaj datę początkową w formacie YYYYMMDD: ')
konc = input('Podaj datę końcową w formacie YYYYMMDD: ')
# domyślny koniec: dzisiejsza data
if not konc:
konc = date.today().strftime('%Y%m%d')
# domyślny początek: 1 stycznia z tego samego roku
if not pocz:
pocz = konc[0:4] + '0101'
adres = f'https://stooq.pl/q/d/l/?s={ticker}&d1={pocz}&d2={konc}&i=d'
df = pd.read_csv(adres, index_col='Data', parse_dates=['Data'])
print(f'Pobrano dane: {len(df)} rekordów')
# nazwy kolumn: Otwarcie Najwyzszy Najnizszy Zamkniecie
print('Najniższy kurs:', df["Najnizszy"].min())
print('Najwyższy kurs:', df["Najwyzszy"].max())
print('Średni kurs zamknięcia:', df["Zamkniecie"].mean())
# Dodamy nowe kolumny
df["Zmiana"] = df["Zamkniecie"].diff()
df["Zmiana %"] = df["Zamkniecie"].pct_change() * 100
df["Średnia 5"] = df["Zamkniecie"].rolling(window=5).mean()
# To poprawiało format daty, ale jednocześnie Excel gubił informację, że ta kolumna jest typu data i stawała się zwykłym tekstem.
# df.index = df.index.strftime('%Y-%m-%d')
# Specyfikacja jak agregować w formie słownika - poniżej przekazuję to do agg
agregaty = {
'Otwarcie': ['min', 'mean', 'max'],
'Zamkniecie': ['min', 'mean', 'max'],
'Najnizszy': ['min'],
'Najwyzszy': ['max'],
}
# Wolumen agreguję tylko, jesli występuje w danych źródłowych
if 'Wolumen' in df:
agregaty['Wolumen'] = ['sum']
miesiace = df.resample('M').agg(agregaty)
plik_excel = f'kursy_{ticker}_{pocz}_{konc}.xlsx'
with pd.ExcelWriter(plik_excel) as xl_writer:
df.to_excel(excel_writer=xl_writer, sheet_name='Dane')
miesiace.to_excel(excel_writer=xl_writer, sheet_name='Miesiecznie')
print(f'Zapisano plik {plik_excel}')
wykres = df[["Otwarcie", "Zamkniecie", "Najwyzszy", "Najnizszy"]].plot(figsize=(20,10))
plik_wykres = f'kursy_{ticker}_{pocz}_{konc}.png'
wykres.get_figure().savefig(plik_wykres)
print(f'Zapisano wykres w pliku {plik_wykres}')
# Generowanie "wstęgi Boelingera"
WINDOW = 10
KORYTARZ = 2
srednia_kroczaca = df.Zamkniecie.rolling(window=WINDOW).mean()
odchylenie = df.Zamkniecie.rolling(window=WINDOW).std()
wstega_gorna = srednia_kroczaca + KORYTARZ * odchylenie
wstega_dolna = srednia_kroczaca - KORYTARZ * odchylenie
wstega = pd.DataFrame({
'Bieżące': df['Zamkniecie'],
'Trend': srednia_kroczaca,
'Górna': wstega_gorna,
'Dolna': wstega_dolna,
})
wykres = wstega.plot(figsize=(20, 10), grid=True)
wykres.fill_between(wstega.index, wstega["Górna"], wstega["Dolna"], color="#AAEEFF", alpha=0.25)
plik_bolinger = f'bolinger_{ticker}_{pocz}_{konc}.png'
wykres.get_figure().savefig(plik_bolinger)
print(f'Zapisano wstęgę Bolingera w pliku {plik_bolinger}')
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4899,7 +4899,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -9555,7 +9555,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -12441,7 +12441,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -3924,7 +3924,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
10 11 12 13 14 15 16 17 18 19
10 9 8 7 6 5 4 3 2 1
0 0 0 0 0 0 0 0 0 0
1.1 1.25 1.44 1.75 2.0 1.66 1.5 1.33 1.2 1.0
5 4 6 3 7 2 8 1 9 0
\ No newline at end of file
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica
4.850000000000000000e+01 5.350000000000000000e+01 5.850000000000000000e+01 6.350000000000000000e+01 6.850000000000000000e+01 7.350000000000000000e+01 7.850000000000000000e+01 8.350000000000000000e+01 8.850000000000000000e+01 9.350000000000000000e+01
4.850000000000000000e+01 4.350000000000000000e+01 3.850000000000000000e+01 3.350000000000000000e+01 2.850000000000000000e+01 2.350000000000000000e+01 1.850000000000000000e+01 1.350000000000000000e+01 8.500000000000000000e+00 3.500000000000000000e+00
-1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00 -1.500000000000000000e+00
4.000000000000000000e+00 4.750000000000000000e+00 5.699999999999999289e+00 7.250000000000000000e+00 8.500000000000000000e+00 6.799999999999998934e+00 6.000000000000000000e+00 5.150000000000000355e+00 4.500000000000000000e+00 3.500000000000000000e+00
2.350000000000000000e+01 1.850000000000000000e+01 2.850000000000000000e+01 1.350000000000000000e+01 3.350000000000000000e+01 8.500000000000000000e+00 3.850000000000000000e+01 3.500000000000000000e+00 4.350000000000000000e+01 -1.500000000000000000e+00
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment