Skip to content

Commit 69f9b46

Browse files
committedFeb 7, 2021
add day16 homework
1 parent c156046 commit 69f9b46

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed
 

‎D16_timePeriod/homework.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pandas as pd
2+
import numpy as np
3+
4+
# 題目:運下列時間序列資料做運算
5+
index = pd.date_range("1/1/2021", periods=20, freq='D')
6+
series = pd.Series(range(20), index=index)
7+
# print(f'index= {index}')
8+
print(series)
9+
# 將所有轉為周資料
10+
print( series.resample('W',convention='start').asfreq('D') )
11+
# 將周資料的值取平均
12+
print( series.resample('W').mean() )

‎Stock/LPPLS_Bubble.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pandas as pd
2+
import numpy as np
3+
import urllib3
4+
# from bs4 import BeautifulSoup
5+
import yfinance as yf
6+
import matplotlib.pyplot as plt
7+
import lppls
8+
9+
# 參考網站 https://colab.research.google.com/drive/1oEMk8-yvhaWHP9DwE6bO_uzazPcvEvAY?usp=sharing
10+
# 中文 https://www.finlab.tw/bitcoin-stock-bubble-analysis-lppl/
11+
# Log Periodic Power Law Singularity (LPPLS) Model
12+
13+
14+

‎Stock/Single.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
# Yahoo! module
1010
#https://pypi.org/project/yfinance/
1111
# Get the data of the stock AAPL
12-
print( yf.download('QS','2020-01-01','2021-03-01') )
13-
print( yf.download('ISR','2020-01-01','2021-03-01') )
14-
print( yf.download('SENS','2020-01-01','2021-03-01') )
15-
print( yf.download('NVIV','2020-01-01','2021-03-01') )
12+
print( yf.download('TLRY','2020-12-01','2021-03-01') )
13+
print( yf.download('APHA','2020-12-01','2021-03-01') )
1614
# # Plot the close price of the AAPL
1715
# data.Close.plot()
1816
# plt.show()

‎Stock/TaiwanStock.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 參考網站 https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae%e5%8f%b0%e8%82%a1%e6%af%8f%e6%97%a5%e7%88%ac%e8%9f%b2%e6%95%99%e5%ad%b8/
2+
3+
import requests
4+
from io import StringIO
5+
import pandas as pd
6+
import numpy as np
7+
8+
datestr = '20200131'
9+
10+
# 下載股價
11+
r = requests.post('https://www.twse.com.tw/exchangeReport/MI_INDEX?response=csv&date=' + datestr + '&type=ALL')
12+
13+
# 整理資料,變成表格
14+
df = pd.read_csv(StringIO(r.text.replace("=", "")),
15+
header=["證券代號" in l for l in r.text.split("\n")].index(True)-1)
16+
17+
# 整理一些字串:
18+
df = df.apply(lambda s: pd.to_numeric(s.astype(str).str.replace(",", "").replace("+", "1").replace("-", "-1"), errors='coerce'))
19+
20+
# 顯示出來
21+
df.head()

‎Stock/top100.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# from bs4 import BeautifulSoup
55
import yfinance as yf
66
import matplotlib.pyplot as plt
7+
import time
8+
timestr = time.strftime("%Y%m%d-%H%M%S")
79

810
# 參考網站
911
# https://medium.com/ai%E8%82%A1%E4%BB%94/%E7%94%A8-python-%E6%89%93%E9%80%A0%E8%87%AA%E5%B7%B1%E7%9A%84%E8%82%A1%E5%B8%82%E8%B3%87%E6%96%99%E5%BA%AB-%E7%BE%8E%E8%82%A1%E7%AF%87-e3e896659fd6
@@ -26,6 +28,13 @@ def Money2int(x):
2628
return float(x)
2729
return (float(x[:-1]) * 10 ** m[x[-1]] )
2830

31+
def over3(x):
32+
A = Money2int(x[5])
33+
B = Money2int(x[6])
34+
P = p2f(x[4])
35+
L = ( A>B and P>=0.03 )
36+
return L
37+
2938
def sort3to10(x):
3039
A = Money2int(x[5])
3140
B = Money2int(x[6])
@@ -50,5 +59,8 @@ def setFunc( func ):
5059
print(setFunc(big10))
5160
print(setFunc(sort3to10))
5261

62+
Over3 = setFunc(over3)
63+
print(timestr)
64+
Over3.to_excel('over3.xlsx', sheet_name='A'+timestr)
5365
# data=data[np.argsort(data[:,0])]
5466
# stk_list = data.Symbol

0 commit comments

Comments
 (0)
Please sign in to comment.