SHARE
TWEET

Untitled

a guest May 26th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import baostock as bs
  2. import pandas as pd
  3. import talib
  4.  
  5.  
  6. def code_to_symbol(code):
  7.     return 'sh.%s' % code if code[:1] in ['5', '6', '9'] or code[:2] in ['11', '13'] else 'sz.%s' % code
  8.  
  9.  
  10. def query_k_data(code, start, end, frequency='d'):
  11.     bs.login()
  12.     codeSymbol = code_to_symbol(code)
  13.     rs = bs.query_history_k_data_plus(codeSymbol,
  14.                                       "date,open,high,low,close,preclose,volume,amount,turn,tradestatus,pctChg",
  15.                                       start_date=start, end_date=end,
  16.                                       frequency=frequency, adjustflag="2")
  17.     print(rs)
  18.     data_list = []
  19.     while (rs.error_code == '0') & rs.next():
  20.         data_list.append(rs.get_row_data())
  21.     df = pd.DataFrame(data_list, columns=rs.fields)
  22.     df = df.loc[df.tradestatus == '1']
  23.     df.open = df.open.astype(float)
  24.     df.high = df.high.astype(float)
  25.     df.low = df.low.astype(float)
  26.     df.close = df.close.astype(float)
  27.     bs.logout()
  28.     return df
  29.  
  30.  
  31. def fill_macd(df):
  32.     macd, macdsignal, macdhist = talib.MACD(df['close'].values)
  33.     df['macd'] = macd
  34.     return df
  35.  
  36.  
  37. if __name__ == '__main__':
  38.     df = query_k_data('000970', '2018-01-01', '2018-05-16')
  39.     print(fill_macd(df))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top