ttt.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. m = 0
  2. # def mm(m):
  3. # while True:
  4. # print('m=', m)
  5. # m = m + 1
  6. # if m == 10:
  7. # break
  8. # return
  9. # print(mm(m))
  10. import pandas as pd
  11. import pymysql
  12. from sqlalchemy import create_engine, text
  13. import threading
  14. from datetime import datetime as dt
  15. import datetime
  16. from jqdatasdk.technical_analysis import *
  17. from xtquant import xtdata, xtconstant
  18. from xtquant.xttype import StockAccount
  19. from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
  20. import time
  21. import math
  22. import multiprocessing as mp
  23. import os
  24. import psutil
  25. import traceback
  26. from apscheduler.schedulers.blocking import BlockingScheduler
  27. import sys
  28. # p = xtdata.get_instrument_detail('000001.sz')
  29. # print(p.get('UpStopPrice'))
  30. # stock = '000001.SZ'
  31. # engine_stock = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qmt_stocks_whole?charset=utf8',
  32. # pool_size=5000, pool_recycle=50, max_overflow=-1)
  33. # df = pd.read_sql_query(text(
  34. # 'select close_front, high_front from `%s_1d`' % stock), engine_stock.connect())
  35. #
  36. # print(df['close_front'].iloc[-1])
  37. # print(df['close_front'].iloc[-2])
  38. def sell_trader(data):
  39. stock = '002645.SZ'
  40. # print(data[stock][0]['close'])
  41. print(xtdata.get_instrument_detail(stock).get('UpStopPrice'))
  42. current_price = data[stock]['lastPrice']
  43. print('aaa', current_price, data[stock]['time'])
  44. print(data[stock])
  45. '''
  46. if stock in data and can_use_volume != 0:
  47. current_price = data[stock]['lastPrice']
  48. open_price = data[stock]['open']
  49. MA5 = ma(stock, 5, data)
  50. MA5_1 = ma_1(stock, 5)
  51. df = pd.read_sql_query(text(
  52. 'select close_front, high_front from `%s_1d`' % stock), engine_stock.connect())
  53. print(f'{stock},持仓量为{can_use_volume}当前价:{current_price},MA5:{MA5},昨日MA5:{MA5_1},开始判断:')
  54. if current_price == xtdata.get_instrument_detail(stock).get('UpStopPrice') \
  55. or (df['close_front'].iloc[-1] == df['high_front'].iloc[-1]
  56. and df['close_front'].iloc[-1] / df['close_front'].iloc[-2] > 1.08):
  57. continue
  58. elif current_price < MA5 or MA5 < MA5_1:
  59. print('卖出信号!!!!!!', stock, current_price)
  60. order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_SELL, can_use_volume,
  61. xtconstant.LATEST_PRICE, 0, 'MA5策略', '低于MA5趋势向下')
  62. print('价格:', current_price, open_price, MA5, MA5_1, '低于MA5趋势向下')
  63. print(order_id, stock, can_use_volume)
  64. elif current_price > MA5 * 1.07:
  65. print('盈利乖离率超7%!!!!!!', stock, current_price)
  66. order_id = xt_trader.order_stock(acc, stock, xtconstant.STOCK_SELL, can_use_volume,
  67. xtconstant.LATEST_PRICE, 0, 'MA5策略', '盈利乖离率超7%')
  68. print('价格:', current_price, open_price, MA5, MA5_1, '盈利乖离率超7%')
  69. print(order_id, stock, can_use_volume)
  70. else:
  71. print(f'本轮没有持仓股票信息!')
  72. '''
  73. # xtdata.subscribe_quote('301125.SZ',callback=sell_trader)
  74. stocks = xtdata.get_stock_list_in_sector('沪深A股')
  75. xtdata.download_history_data2(stock_list=stocks, period='30m', start_time='', end_time='')
  76. print(stocks)
  77. seq = xtdata.subscribe_whole_quote(stocks, callback=sell_trader)
  78. xtdata.run()