404.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from xtquant import xtdata, xtconstant
  2. from xtquant.xttype import StockAccount
  3. from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
  4. import pandas as pd
  5. import datetime
  6. from datetime import datetime as dt
  7. import sys
  8. pd.set_option('display.max_columns', None)
  9. class MyXtQuantTraderCallback(XtQuantTraderCallback):
  10. def on_disconnected(self):
  11. """
  12. 连接断开
  13. :return:
  14. """
  15. print(datetime.datetime.now(), '连接断开回调')
  16. def on_stock_order(self, order):
  17. """
  18. 委托回报推送
  19. :param order: XtOrder对象
  20. :return:
  21. """
  22. print(datetime.datetime.now(), '委托回调', order.order_remark)
  23. def on_stock_trade(self, trade):
  24. """
  25. 成交变动推送
  26. :param trade: XtTrade对象
  27. :return:
  28. """
  29. print(datetime.datetime.now(), '成交回调', trade.order_remark)
  30. def on_order_error(self, order_error):
  31. """
  32. 委托失败推送
  33. :param order_error:XtOrderError 对象
  34. :return:
  35. """
  36. # print("on order_error callback")
  37. # print(order_error.order_id, order_error.error_id, order_error.error_msg)
  38. print(f"委托报错回调 {order_error.order_remark} {order_error.error_msg}")
  39. def on_cancel_error(self, cancel_error):
  40. """
  41. 撤单失败推送
  42. :param cancel_error: XtCancelError 对象
  43. :return:
  44. """
  45. print(datetime.datetime.now(), sys._getframe().f_code.co_name)
  46. def on_order_stock_async_response(self, response):
  47. """
  48. 异步下单回报推送
  49. :param response: XtOrderResponse 对象
  50. :return:
  51. """
  52. print(f"异步委托回调 {response.order_remark}")
  53. def on_cancel_order_stock_async_response(self, response):
  54. """
  55. :param response: XtCancelOrderResponse 对象
  56. :return:
  57. """
  58. print(datetime.datetime.now(), sys._getframe().f_code.co_name)
  59. def on_account_status(self, status):
  60. """
  61. :param response: XtAccountStatus 对象
  62. :return:
  63. """
  64. print(datetime.datetime.now(), sys._getframe().f_code.co_name)
  65. def p(data):
  66. print(data)
  67. if __name__ == '__main__':
  68. hszs = xtdata.get_stock_list_in_sector('中金所')
  69. print(hszs)
  70. print(dt.now().strftime('%Y%m%d'))
  71. # print(xtdata.get_sector_list())
  72. field = ['time', 'open', 'close', 'high', 'low', 'volume', 'amount']
  73. stock_list = hszs
  74. stock = '000001.SH'
  75. xtdata.download_history_data2(stock_list=['000001.SZ'], period='1m', start_time='20230801', end_time='')
  76. df = xtdata.get_market_data(field, ['000001.SZ'], '1m', start_time='20230804', end_time='', count=-1,
  77. dividend_type='front')
  78. df = pd.concat([df[i].T for i in ['time', 'open', 'high', 'low', 'close', 'volume',
  79. 'amount']], axis=1)
  80. df.columns = ['time', 'open_front', 'high_front', 'low_front', 'close_front', 'volume_front',
  81. 'amount_front']
  82. df['time'] = df['time'].apply(lambda x: dt.fromtimestamp(x / 1000.0))
  83. daily_avg_price = []
  84. print(len(df))
  85. for i in range(1, len(df) + 1):
  86. daily_avg_price.append(round(sum(df['amount_front'][:i]) / sum(df['volume_front'][:i]) / 100, 2))
  87. df['daily_avg_price'] = daily_avg_price
  88. print(df)
  89. # xtdata.subscribe_whole_quote(['000001.SZ'], callback=p)