查看完整版本 : 學人學寫python

Ob1Kenobi 2020-6-25 10:23 PM

學人學寫python

由於python 有matplotlib、pandas等套件。其他語言,唔係冇就係唔齊。用上手真係搣唔甩。在下試寫了一段,從網上拿了十年的「恒生指數」,計出回報率,然後plot出來。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#  returnOfHsi.py
#  to compute the return rate of HSI of past 10 years

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf;

def first_day_of_year(offset=0, d=np.datetime64('today')):
    dt = '{}-01-01'.format(str(d.astype(object).year + offset))
    dt = np.datetime64(dt)

    #  turn it into string
    dt = np.datetime_as_string(dt, unit='D')
    return dt

# timestamp right now
today = np.datetime64('now')

yearList = []
retList = []

thisYear = today.astype(object).year
for index in range(-10, 0):
    yearList.append(thisYear + index)

#  程罪員的測試碼
#  print(yearList)

yearArr = np.array(yearList)

#  程罪員的測試碼
#  print(yearArr)
#  print(yearArr.dtype)

retList = []

stock = yf.Ticker("%5EHSI")

for index in range(-10, 0):
    #  程罪員的測試碼
    #  print(first_day_of_year(index))
    #  print(first_day_of_year(index+1))

    hist = stock.history(start=first_day_of_year(index), end=first_day_of_year(index+1), interval="1d")

    #  程罪員的測試碼
    #  print(hist.columns)
    #  print(hist.index.max())
    #  x = hist.index.array
    #  print(x.size)

    print(hist.iloc[0]['Open'], end=', ')
    print(hist.iloc[hist.index.array.size-1]['Close'])

    #  open value at the begin of year
    x = hist.iloc[0]['Open']
    #  closing value at the end of year
    y = hist.iloc[hist.index.array.size-1]['Close']

    retList.append(y/x)

    #  程罪員的測試碼
    print(hist)

retArr = np.array(retList)

#  to create a new dataframe
d = {'year':yearArr, 'return(%)':np.log(retArr) * 100}
df = pd.DataFrame(data=d)

#  程罪員的測試碼
print(df)

plt.figure(figsize=(10, 6))
plt.plot(df['year'], df['return(%)'], '-k', label='return of HSI')
plt.legend()
plt.xlabel('Year')
plt.ylabel('%')
plt.grid(True)
plt.show()

鄉貢仁 2020-6-25 11:36 PM

:P

[[i] 本帖最後由 鄉貢仁 於 2020-6-26 06:22 AM 編輯 [/i]]

Nikki000 2020-7-4 11:12 PM

[quote]原帖由 [i]Ob1Kenobi[/i] 於 2020-6-25 10:23 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=520716942&ptid=29172405][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
由於python 有matplotlib、pandas等套件。其他語言,唔係冇就係唔齊。用上手真係搣唔甩。在下試寫了一段,從網上拿了十年的「恒生指數」,計出回報率,然後plot出來。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#  returnOfHsi.py
#  to compute the return rate of HSI ... [/quote]


師兄西利,我學左幾堂都無進展
而家唯有再係YouTube聽人講左基礎先再自學
頁: [1]
查看完整版本: 學人學寫python