• 瀏覽: 962
  • 回覆: 2
[隱藏]
由於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-26 06:22 AM 編輯 ]



回覆 引用 TOP

引用:
原帖由 Ob1Kenobi 於 2020-6-25 10:23 PM 發表

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

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

#  returnOfHsi.py
#  to compute the return rate of HSI ...
師兄西利,我學左幾堂都無進展
而家唯有再係YouTube聽人講左基礎先再自學



回覆 引用 TOP

[按此隱藏 Google 建議的相符內容]