his_money_flow.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from jqdatasdk import *
  2. from datetime import datetime as dt
  3. import pandas as pd
  4. import pymysql
  5. from sqlalchemy import create_engine
  6. auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
  7. stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
  8. engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
  9. engine_data = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks_data?charset=utf8')
  10. # stocks_List = ','.join(set(stocks))
  11. # db_stocks_list = pymysql.connect(host='localhost',
  12. # user='root',
  13. # port=3307,
  14. # password='r6kEwqWU9!v3',
  15. # database='hlfx_pool')
  16. # cursor_stock_list = db_stocks_list.cursor()
  17. # sql = "INSERT INTO stocks_list (date,securities) VALUES('%s','%s')" % (dt.today().strftime('%Y-%m-%d'), stocks_List)
  18. # cursor_stock_list.execute(sql)
  19. # db_stocks_list.commit()
  20. # db_stocks_list.close()
  21. fre = '1d'
  22. print('ready to write to mysql %s' % fre)
  23. for stock in stocks:
  24. print(stock, fre)
  25. starttime ='2010-01-04'
  26. # endtime = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine).iloc[-1, 1]
  27. df_stock = get_price(stock, start_date=starttime, end_date=dt.today().strftime('%Y-%m-%d %H:%M:%S'),
  28. frequency=fre, fields=['open', 'close', 'high', 'low', 'volume', 'money'],
  29. skip_paused=False,
  30. fq='pre', count=None, panel=False)
  31. df_stock.index.name = 'date'
  32. # print(df_stock)
  33. # print(starttime,endtime)
  34. df_money = get_money_flow(stock, start_date=starttime, end_date=dt.today().strftime('%Y-%m-%d %H:%M:%S'),
  35. fields=None, count=None)
  36. df_money = df_money.drop(columns=['sec_code'])
  37. # df_money.to_csv('/Users/daniel/Downloads/000002.csv')
  38. # print(df_money)
  39. df_stock = pd.merge(df_stock, df_money, how='outer', left_index=False , on='date')
  40. # df_stock.to_csv('D:\001_QuantTrade\Result.csv')
  41. df_stock = df_stock.dropna(axis=0)
  42. df_stock.reset_index(inplace=True)
  43. df_stock.rename(columns={'index': 'date'}, inplace=True)
  44. df_stock.to_sql('stk%s_%s' % (stock, fre), con=engine_data, index=True, if_exists='replace')
  45. # with engine.connect() as con:
  46. # con.execute("ALTER TABLE `stk%s_%s` ADD PRIMARY KEY (`date`);" % (stock, fre))
  47. print(df_stock)