1m_real_time.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # coding:utf-8
  2. from datetime import datetime as dt
  3. import os
  4. import pandas as pd
  5. from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
  6. from xtquant.xttype import StockAccount
  7. from xtquant import xtdata, xtconstant
  8. import time
  9. from sqlalchemy import create_engine, text
  10. from jqdatasdk import *
  11. import pymysql
  12. import multiprocessing as mp
  13. import math
  14. import psutil
  15. import datetime
  16. from apscheduler.schedulers.blocking import BlockingScheduler
  17. import sys
  18. df = pd.DataFrame(columns=['time', 'open', 'close', 'high', 'low', 'volume', 'amount'])
  19. def avg_price(df):
  20. print('avg_price')
  21. daily_avg_price = []
  22. for i in range(1, len(df) + 1):
  23. daily_avg_price.append(round(sum(df['amount'][:i]) / sum(df['volume'][:i]) / 100, 2))
  24. df['daily_avg_price'] = daily_avg_price
  25. return df
  26. def over_avg(datas):
  27. global df
  28. for stock_code in datas:
  29. df_temp = pd.DataFrame(datas[stock_code])
  30. df_temp['time'] = df_temp['time'].apply(lambda x: dt.fromtimestamp(x / 1000.0))
  31. df = pd.concat([df, df_temp], axis=0, ignore_index=True)
  32. # df = avg_price(df)
  33. # df = df[['time', 'open', 'close', 'high', 'low', 'volume', 'amount', 'daily_avg_price']]
  34. print(df)
  35. # print(stock_code, datas[stock_code])
  36. if __name__ == '__main__':
  37. print('start')
  38. t = dt.now().strftime('%Y%m%d')
  39. # stocks = xtdata.get_stock_list_in_sector('沪深A股')
  40. xtdata.subscribe_quote('000001.SH', period='1m', start_time=t, callback=over_avg)
  41. xtdata.run()