kzz.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. df = pd.DataFrame(columns=['time', 'open', 'close', 'high', 'low', 'volume', 'amount'])
  9. def avg_price(df):
  10. print('avg_price')
  11. daily_avg_price = []
  12. for i in range(1, len(df) + 1):
  13. daily_avg_price.append(round(sum(df['amount'][:i]) / sum(df['volume'][:i]) / 10, 2))
  14. df['daily_avg_price'] = daily_avg_price
  15. return df
  16. def over_avg(datas):
  17. global df
  18. for stock_code in datas:
  19. df_temp = pd.DataFrame(datas[stock_code])
  20. df_temp['time'] = df_temp['time'].apply(lambda x: dt.fromtimestamp(x / 1000.0))
  21. df = pd.concat([df, df_temp], axis=0, ignore_index=True)
  22. df = avg_price(df)
  23. df = df[['time', 'open', 'close', 'high', 'low', 'volume', 'amount', 'daily_avg_price']]
  24. print(stock_code, '\n', df)
  25. if __name__ == '__main__':
  26. print('start')
  27. kzz = xtdata.get_stock_list_in_sector('沪深转债')
  28. print(kzz, len(kzz))
  29. # exit()
  30. t = dt.now().strftime('%Y%m%d')
  31. # stocks = xtdata.get_stock_list_in_sector('沪深A股')
  32. # xtdata.subscribe_whole_quote(kzz, callback=over_avg)
  33. xtdata.subscribe_quote('128130.SZ', period='1m', start_time=t, count=-1, callback=over_avg)
  34. xtdata.run()