返回列表 发布新帖

量化实盘---大qmt实现国债逆回购,提供源代码

1709 0
发表于 2024-7-25 22:21:01 | 显示全部楼层 阅读模式
量化实盘---大qmt实现国债逆回购,提供源代码 https://mp.weixin.qq.com/s/kVfqMFmJl04Km9BmkdW97A
源代码
  1. #encoding:gbk
  2. '''
  3. 小果国债逆回购
  4. 网页 https://gitee.com/li-xingguo11111/qmt_trader
  5. '''
  6. import pandas as pd
  7. import numpy as np
  8. import talib
  9. import math
  10. def adjust_stock(stock='600031.SH'):
  11.         '''
  12.         调整代码
  13.         '''
  14.         if stock[-2:]=='SH' or stock[-2:]=='SZ' or stock[-2:]=='sh' or stock[-2:]=='sz':
  15.                 stock=stock.upper()
  16.         else:
  17.                 if stock[:3] in ['600','601','603','688','510','511',
  18.                         '512','513','515','113','110','118','501'] or stock[:2] in ['11']:
  19.                         stock=stock+'.SH'
  20.                 else:
  21.                         stock=stock+'.SZ'
  22.                 return stock
  23. def adjust_amount(stock='',amount=''):
  24.         '''
  25.         调整数量
  26.         '''           
  27.         if stock[:3] in ['110','113','123','127','128','111'] or stock[:2] in ['11','12']:
  28.                 amount=math.floor(amount/10)*10
  29.         else:
  30.                 amount=math.floor(amount/100)*100
  31.                 return amount
  32. def balance(c,accounts=''):
  33.                 '''
  34.                 对接同花顺
  35.                 '''
  36.                 try:
  37.                         accounts = get_trade_detail_data(c.account, c.account_type, 'account')
  38.                 except:
  39.                         accounts=accounts
  40.                 df=pd.DataFrame()
  41.                 for dt in accounts:
  42.                         df['账号类型']=[dt.m_nBrokerType]
  43.                         df['资金账户']=[dt.m_strAccountID]
  44.                         df['可用金额']=[dt.m_dAvailable]
  45.                         #df['冻结金额']=[dt.frozen_cash]
  46.                         df['持仓市值']=[dt.m_dInstrumentValue]
  47.                         df['总资产']=[dt.m_dBalance]
  48.                 return df
  49. def position(c,positions=''):
  50.                 '''
  51.                 对接同花顺
  52.                 持股
  53.                 '''
  54.                 try:
  55.                         positions = get_trade_detail_data(c.account, c.account_type, 'position')
  56.                 except :
  57.                         positions=positions
  58.                 data=pd.DataFrame()
  59.                 if len(positions)>0:
  60.                         print('持股数量{}'.format(len(positions)))
  61.                         for dt in positions:
  62.                                 df=pd.DataFrame()
  63.                                   #df['账号类型']=[positions[i].account_type]
  64.                                 df['资金账号']=[dt.m_strAccountID]
  65.                                 df['证券代码']=[dt.m_strInstrumentID]
  66.                                 df['证券代码']=df['证券代码'].apply(lambda x:adjust_stock(x))
  67.                                 #df['证券代码']=[dt.m_strProductID]
  68.                                 #df['证券代码']=df['证券代码'].apply(lambda x:str(x)[:6])
  69.                                 df['股票余额']=[dt.m_nVolume]
  70.                                 df['可用余额']=[dt.m_nCanUseVolume]
  71.                                 df['成本价']=[dt.m_dPositionCost]
  72.                                 df['参考成本价']=[dt.m_dOpenPrice]
  73.                                 df['市值']=[dt.m_dInstrumentValue]
  74.                                 data=pd.concat([data,df],ignore_index=True)
  75.                         return data
  76.                 else:
  77.                         return data
  78. def get_spot_data(c,stock='600031.SH'):
  79.         '''
  80.         获取最新价格
  81.         '''
  82.         df=c.get_full_tick([stock])
  83.         price=df[stock]['lastPrice']
  84.         return price
  85. def buy(c,stock='159985.SZ',amount=100,price=2.045):
  86.         '''
  87.         自定义买入函数
  88.         stock,股票
  89.         amount,数量
  90.         price价格
  91.         '''
  92.         account=c.account
  93.         passorder(23,1101,account,stock,11,price,amount,1,c)
  94.         print('买入股票{} 数量{} 价格{}##############'.format(stock,amount,price))
  95. def sell(c,stock='159985.SZ',amount=100,price=2.045):
  96.         '''
  97.         自定义卖出函数
  98.         stock,股票
  99.         amount,数量
  100.         price价格
  101.         '''
  102.         account=c.account
  103.         passorder(24,1101,account,stock,11,price,amount,1,c)
  104.         print('卖出股票{} 数量{} 价格{}##############'.format(stock,amount,price))
  105. def reverse_repurchase_of_treasury_bonds_1(c,buy_ratio=1):
  106.         '''
  107.         国债逆回购1,新的函数
  108.         购买比例buy_ratio
  109.         '''
  110.         # 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
  111.         account=balance(c)
  112.         print(account)
  113.         av_cash=account['可用金额'].tolist()[-1]
  114.         av_cash=float(av_cash)
  115.         av_cash=av_cash*buy_ratio
  116.         #stock_code_sh = '204001.SH'
  117.         #统一用深圳
  118.         stock_code_sh = '131810.SZ'
  119.         stock_code_sz = '131810.SZ'
  120.         price_sh = get_spot_data(c,stock_code_sh)
  121.         price_sz = get_spot_data(c,stock_code_sz)
  122.         bidPrice1 = max(price_sh,price_sz)
  123.         if price_sh >= price_sz:
  124.                 stock_code = stock_code_sh
  125.         else:
  126.                 stock_code = stock_code_sz
  127.         print(stock_code,bidPrice1)
  128.         price=bidPrice1
  129.         stock=stock_code
  130.         #下单的数量要是1000
  131.         amount = int(av_cash/1000)
  132.         #想下取整1000的倍数
  133.         amount=math.floor(amount/10)*100
  134.         #借出钱sell
  135.         print('开始逆回购***********')
  136.         if amount>0:
  137.                 sell(c,stock=stock,amount=amount,price=price)
  138.                 text='国债逆回购交易类型 代码{} 价格{} 数量{} 订单编号{}'.format(stock,price,amount,fix_result_order_id)
  139.                 return '交易成功',text
  140.         else:
  141.                 text='国债逆回购卖出 标的{} 价格{} 委托数量{}小于0有问题'.format(stock,price,amount)
  142.                 print('账户没有可以的钱@@@@@@@@@@@@@@@@@@@')
  143.                 return '交易失败',text
  144. def init(c):
  145.         '''
  146.         小果国债逆回购
  147.         网页 https://gitee.com/li-xingguo11111/qmt_trader
  148.         '''
  149.         c.account=''
  150.         c.account_type='STOCK'
  151.         #测试代码
  152.         #######################测试代码
  153.         stats,text=reverse_repurchase_of_treasury_bonds_1(c)
  154.         print(text)
  155.         ###################测试代码
  156.         c.run_time("reverse_repurchase_of_treasury_bonds_1","1nDay","2024-07-25 14:57:00")  #每日14:47分触发
  157.        
  158.        
复制代码



回复

您需要登录后才可以回帖 登录 | 立即注册

客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2025 迅投QMT社区 版权所有 All Rights Reserved. 京ICP备2025122616号-3
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表