MA_Close_futures.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from jqdatasdk import *
  2. from datetime import datetime as dt
  3. import pandas as pd
  4. from jqdatasdk.technical_analysis import *
  5. from sqlalchemy import create_engine
  6. import threading
  7. auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
  8. # futures = ['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX', 'FU9999.XSGE', 'RB9999.XSGE', 'MA9999.XZCE', 'TA9999.XZCE', 'SA9999.XZCE', 'M9999.XDCE', 'LH9999.XDCE']
  9. # futures = ['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX']
  10. futures = ['FU9999.XSGE', 'RB9999.XSGE', 'MA9999.XZCE', 'TA9999.XZCE', 'SA9999.XZCE', 'M9999.XDCE', 'LH9999.XDCE']
  11. # futures = ['FU9999.XSGE']
  12. engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/futures?charset=utf8')
  13. # 获取各stock的去包含dataframe
  14. fut = locals()
  15. print(dt.now(), '开始赋值!')
  16. for fre in ['30m']:
  17. for future in futures:
  18. try:
  19. fut[future] = pd.read_sql_query('select date,open,close,high,low,volume,money from `%s_%s`' % (future, fre),
  20. engine)
  21. except BaseException:
  22. continue
  23. print(dt.now(), '数据库数据已赋值!')
  24. # print(fut['IF9999.CCFX'])
  25. start = dt.now()
  26. print(start)
  27. for f in futures:
  28. fut[f] = pd.concat([fut[f], pd.DataFrame(columns=['MA5', 'derta'])],axis=1)
  29. print(fut[f])
  30. for i in range(len(fut[f])):
  31. MA1 = MA(f, check_date=fut[f].loc[i, 'date'], timeperiod=5)
  32. MA1 = MA1[f]
  33. fut[f].loc[i, 'MA5'] = MA1
  34. # print(fut[f].loc[i, ['close','high','low']])
  35. if fut[f].loc[i, 'close'] > MA1:
  36. derta = fut[f].loc[i, 'high']/MA1 - 1
  37. else:
  38. derta = (fut[f].loc[i, 'low']/MA1 -1)
  39. fut[f].loc[i, 'derta'] = derta
  40. print('___________________________________')
  41. print(fut[f])
  42. fut[f].to_excel('/Users/daniel/Downloads/MA_derta/30m_%s.xlsx'% f)
  43. end= dt.now()
  44. print('总时长:', (end - start).seconds)
  45. # MA1 = MA('MA9999.XZCE', check_date='2022-09-25', timeperiod=5)
  46. # print(MA1['MA9999.XZCE'])