查看完整版本 : Python問題

蕩調筋 2018-12-7 01:42 AM

Python問題

我新手黎 上網左抄右抄 自己改下 寫左下面段code

[quote]import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as wb

stock1='TSLA'
stock2='3988.HK'

stock1_df = wb.DataReader(stock1,data_source='yahoo',start='2015-01-01')

rsi_period = 14

chg = stock1_df['Close'].diff(1)

gain = chg.mask(chg<0,0)
stock1_df['Gain'] = gain

loss = chg.mask(chg>0,0)
stock1_df['Loss'] = loss

avg_gain = gain.ewm(com = rsi_period-1,min_periods=rsi_period).mean()
avg_loss = loss.ewm(com = rsi_period-1,min_periods=rsi_period).mean()

stock1_df['Avg Gain'] = avg_gain
stock1_df['Avg Loss'] = avg_loss

rs = abs(avg_gain/avg_loss)

rsi = 100-(100/(1+rs))

stock1_df['RSI'] = rsi

#stock2

stock2_df = wb.DataReader(stock2,data_source='yahoo',start='2015-01-01')

rsi_period = 14

chg = stock2_df['Close'].diff(1)

gain = chg.mask(chg<0,0)
stock2_df['Gain'] = gain

loss = chg.mask(chg>0,0)
stock2_df['Loss'] = loss

avg_gain = gain.ewm(com = rsi_period-1,min_periods=rsi_period).mean()
avg_loss = loss.ewm(com = rsi_period-1,min_periods=rsi_period).mean()

stock2_df['Avg Gain'] = avg_gain
stock2_df['Avg Loss'] = avg_loss

rs = abs(avg_gain/avg_loss)

rsi = 100-(100/(1+rs))

stock2_df['RSI'] = rsi

new_df = pd.DataFrame({
"RSI": [stock1_df.iloc[-1]['RSI'],stock2_df.iloc[-1]['RSI']],
"ASTOCK":[stock1,stock2]
})

new_df.sort_values(by=['RSI'], ascending=False)[/quote]


問題在於我想寫一段code可以方便以後加唔同既股票
依家要加一隻股票就要copy上面個code一次
我就諗既然全部野都一樣 應該有更好方法做
例如 ticker果類?
咁以後要加股票 只要將個股票編號加入ticker就可以了

謝謝指教

[[i] 本帖最後由 蕩調筋 於 2018-12-7 01:44 AM 編輯 [/i]]

form5 2018-12-7 11:20 PM

my practicing for python 3.7

form5 2018-12-7 11:21 PM

StockService class

form5 2018-12-7 11:22 PM

plot screen

獵頭先生 2018-12-18 04:02 PM

[quote]原帖由 [i]form5[/i] 於 2018-12-7 11:22 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=491662906&ptid=27896209][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
plot screen [/quote]
form5 識唔識Cryptocurrency D 野?
low-latency, algo-trade 果D,哩排好hit wor.
頁: [1]
查看完整版本: Python問題