• 同花顺自动化交易股票下单接口API量化系统,别信那些外挂插件和软件,头部券商有现成的接口


    同花顺自动化交易股票下单接口API量化系统,别信那些外挂插件和软件,头部券商有现成的接口

    大家在选股票量化接口,自动交易接口时,会有一些以外部插件和软件的形式存在的工具,这些工具可能涉及窗口控件操作、DLL绑定甚至内存注入等技术,从安全角度来看必然存在风险,比如窗口操作类的插件,一旦出错,输入不正确的股票代码或金额,就可能导致资金损失,此外,还有账户被盗取的风险,被非法访问并操控,或者被券商封禁账户,最终,这些操作还可能涉及严重的法律风险,涉及刑事范围,因此,在选择和使用时,务必审慎考虑合法性合规性。

    现在量化交易接口的门槛已经降低了,个人账户入金1万就可以开权限,头部券商,开户渠道选对,费率还会大大降低,如果有好的交易策略,自动化程序化交易就可以避免人工操作时的情绪化和操作失误

    比如我在用的国金证券量化交易客户端,支持股票、基金、可转债、期权、期货等

    自带了很多策略可以参考,包含多因子、日内回转、机器学习、双均线等不同类型的策略

    很多操作只需要一行代码:

    1. #获取实时行情
    2. get_full_tick(['股票代码1','股票代码2'])
    3. #下载历史数据,支持tick级分笔数据,最早可以获取到中国股市开市以来的所有数据
    4. download_history_data('股票代码','k线类型','开始时间','结束时间')

    模拟、实盘、回测均支持,根据你需要可以非常灵活开放,python、VBA无门槛上手

    费率方面,大部分做量化交易的肯定交易频率会很高,找不到好的客户经理,拿着市面上常见的万2.5费率,像我的交易比较频繁,主要集中在可转债和场内ETF上,一天几个来回交易费用得差大几百几千的,费率低意味着交易机会多,拿可转债来说,我的是万0.4免5,以113050南银转债来说,做网格交易一天可以有几百次机会,挂买一成交后立即挂卖一出手,简直是捡钱一样,而如果按照市面上常见的万3或万2不免5,算上交易成本,加上是人工操作,就干脆不要想这种这交易方式,根本没机会出手,直接就被套住了

    以下是一段网格交易策略参考,使用过程中有任何不懂的可以留言或私信与我交流

    1. import numpy as np
    2. import pandas as pd
    3. import time
    4. import datetime
    5. def init(ContextInfo):
    6. #设置图为标的
    7. ContextInfo.tradefuture = ContextInfo.stockcode+"."+ContextInfo.market
    8. ContextInfo.set_universe([ContextInfo.tradefuture])
    9. print(ContextInfo.get_universe())
    10. ContextInfo.timeseries = pd.DataFrame()
    11. ContextInfo.band = np.zeros(5)
    12. #print 'ContextInfo.band',ContextInfo.band
    13. # 设置网格的仓位
    14. ContextInfo.weight = [0.25, 0.15, 0.0, 0.15, 0.25]
    15. # 获取多仓仓位
    16. ContextInfo.position_long = 0
    17. # 获取孔仓仓位
    18. ContextInfo.position_short = 0
    19. #剩余资金
    20. ContextInfo.surpluscapital = ContextInfo.capital
    21. #保证金比率
    22. comdict = ContextInfo.get_commission()
    23. ContextInfo.marginratio = comdict['margin_ratio']
    24. #合约乘数
    25. ContextInfo.multiplier = ContextInfo.get_contract_multiplier(ContextInfo.tradefuture)
    26. #账号
    27. ContextInfo.accountid='testF'
    28. ContextInfo.now_timestamp = time.time()
    29. def handlebar(ContextInfo):
    30. index = ContextInfo.barpos
    31. realtimetag = ContextInfo.get_bar_timetag(index)
    32. lasttimetag = ContextInfo.get_bar_timetag(index - 1)
    33. print(timetag_to_datetime(realtimetag, '%Y-%m-%d %H:%M:%S'))
    34. if ContextInfo.period in ['1m','3m','5m','15m','30m'] and not ContextInfo.do_back_test:
    35. if (datetime.datetime.fromtimestamp(ContextInfo.now_timestamp) - datetime.datetime.fromtimestamp(realtimetag / 1000)).days > 7:
    36. return
    37. starttime = timetag_to_datetime(realtimetag-86400000 * 10, '%Y%m%d%H%M%S')
    38. endtime = timetag_to_datetime(realtimetag-86400000, '%Y%m%d%H%M%S')
    39. #print 'starttime,endtime',starttime,endtime
    40. Result=ContextInfo.get_market_data(['close'],stock_code=[ContextInfo.tradefuture],start_time=starttime,end_time=endtime,skip_paused=False,period=ContextInfo.period,dividend_type='front')
    41. close_sort = Result['close'].sort_index(axis = 0,ascending = True)
    42. #print close_sort,starttime,endtime
    43. #过去300个价格数据的均值和标准差
    44. Result_mean = close_sort.tail(300).mean()
    45. Result_std = close_sort.tail(300).std()
    46. ContextInfo.band = Result_mean + np.array([-40, -3, -2, 2, 3, 40]) * Result_std
    47. #print 'ContextInfo.band',ContextInfo.band
    48. if np.isnan(ContextInfo.band).any() or Result_std==0:
    49. return
    50. if index > 0:
    51. lasttimetag = ContextInfo.get_bar_timetag(index - 1)
    52. #前一根bar收盘价
    53. close_lastbar = ContextInfo.get_market_data (['close'],stock_code=[ContextInfo.tradefuture],period=ContextInfo.period,dividend_type='front')
    54. #当前开盘价
    55. open_currentbar = ContextInfo.get_market_data (['open'],stock_code=[ContextInfo.tradefuture],period=ContextInfo.period,dividend_type='front')
    56. #划分网格
    57. #print close_lastbar,ContextInfo.band
    58. grid = pd.cut([close_lastbar], ContextInfo.band, labels=[0, 1, 2, 3, 4])[0]
    59. #print 'grid ',grid
    60. if not ContextInfo.do_back_test:
    61. ContextInfo.paint('grid',float(grid),-1,0)
    62. # 若无仓位且价格突破则按照设置好的区间开仓
    63. if ContextInfo.position_long == 0 and ContextInfo.position_short == 0 and grid != 2:
    64. # 大于3为在中间网格的上方,做多
    65. if grid >= 3 and ContextInfo.surpluscapital > 0 :
    66. long_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.marginratio*close_lastbar*ContextInfo.multiplier))
    67. ContextInfo.position_long = long_num
    68. buy_open(ContextInfo.tradefuture,long_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    69. ContextInfo.surpluscapital -= long_num * ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier
    70. #print '开多'
    71. elif grid <= 1 and ContextInfo.surpluscapital > 0 :
    72. short_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.marginratio*close_lastbar*ContextInfo.multiplier))
    73. ContextInfo.position_short = short_num
    74. sell_open(ContextInfo.tradefuture,short_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    75. ContextInfo.surpluscapital -= short_num * ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier
    76. #print '开空'
    77. # 持有多仓的处理
    78. elif ContextInfo.position_long > 0 :
    79. if grid >= 3 and ContextInfo.surpluscapital > 0 :
    80. targetlong_num = int(ContextInfo.weight[grid] * (ContextInfo.surpluscapital + ContextInfo.multiplier * close_lastbar * ContextInfo.position_long*ContextInfo.marginratio)/ (ContextInfo.marginratio*close_lastbar * ContextInfo.multiplier))
    81. if targetlong_num > ContextInfo.position_long :
    82. trade_num = targetlong_num - ContextInfo.position_long
    83. ContextInfo.position_long = targetlong_num
    84. buy_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    85. ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    86. elif targetlong_num < ContextInfo.position_long:
    87. trade_num = ContextInfo.position_long - targetlong_num
    88. ContextInfo.position_long = targetlong_num
    89. sell_close_tdayfirst(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    90. ContextInfo.surpluscapital += trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    91. #print '调多仓到仓位'
    92. # 等于2为在中间网格,平仓
    93. elif grid == 2:
    94. sell_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_long,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    95. ContextInfo.surpluscapital += ContextInfo.position_long * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    96. ContextInfo.position_long = 0
    97. #print '平多'
    98. # 小于1为在中间网格的下方,做空
    99. elif grid <= 1:
    100. sell_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_long,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    101. ContextInfo.surpluscapital += ContextInfo.position_long * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    102. ContextInfo.position_long = 0
    103. #print '全平多仓'
    104. if ContextInfo.surpluscapital > 0 :
    105. short_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.multiplier * ContextInfo.marginratio * close_lastbar))
    106. ContextInfo.position_short = short_num
    107. sell_open(ContextInfo.tradefuture,short_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    108. ContextInfo.surpluscapital -= short_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    109. #print '开空仓到仓位'
    110. # 持有空仓的处理
    111. elif ContextInfo.position_short> 0 :
    112. # 小于1为在中间网格的下方,做空
    113. if grid <= 1:
    114. targetlshort_num = int(ContextInfo.weight[grid]*(ContextInfo.surpluscapital + ContextInfo.multiplier*close_lastbar*ContextInfo.position_short*ContextInfo.marginratio)/(ContextInfo.multiplier * ContextInfo.marginratio * close_lastbar))
    115. if targetlshort_num > ContextInfo.position_short:
    116. trade_num = targetlshort_num - ContextInfo.position_short
    117. ContextInfo.position_short = targetlshort_num
    118. sell_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    119. ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    120. #print '开空仓到仓位' ,targetlshort_num
    121. elif targetlshort_num < ContextInfo.position_short:
    122. trade_num = ContextInfo.position_short - targetlshort_num
    123. ContextInfo.position_short = targetlshort_num
    124. buy_close_tdayfirst(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    125. ContextInfo.surpluscapital += trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    126. #print '平空仓到仓位' ,targetlshort_num
    127. # 等于2为在中间网格,平仓
    128. elif grid == 2:
    129. buy_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_short,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    130. ContextInfo.surpluscapital += ContextInfo.position_short * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    131. ContextInfo.position_short = 0
    132. #print '全平空仓'
    133. # 大于3为在中间网格的上方,做多
    134. elif grid >= 3:
    135. buy_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_short,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    136. ContextInfo.surpluscapital += ContextInfo.position_short * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    137. ContextInfo.position_short = 0
    138. #print '全平空仓'
    139. if ContextInfo.surpluscapital > 0 :
    140. trade_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital / (ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier))
    141. ContextInfo.position_long = trade_num
    142. buy_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
    143. ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
    144. #print ' 开多仓到仓位'
    145. # 获取多仓仓位
    146. #print 'ContextInfo.position_long',ContextInfo.position_long
    147. # 获取空仓仓位
    148. #print 'ContextInfo.position_short',ContextInfo.position_short
    149. # 获取剩余资金
    150. #print 'ContextInfo.surpluscapital',ContextInfo.surpluscapital

    使用过程中有任何不懂的可以留言或私信与我交流

    ------

  • 相关阅读:
    AutoCAD DWG,DXF文件导出高清图片、PDF
    身份认证的尽头竟然是无密码 ?
    AR/VR难改歌尔股份代工命
    java-php-python-ssm医院患者管理系统计算机毕业设计
    VSCode + RemoteSSH实现虚拟机Linux下开发
    python画图
    【ARM AMBA5 CHI 入门 12 -- CHI 总线学习 】
    ROS noetic
    Java算法 每日一题(八) 编号206:反转链表
    【Python数据结构与算法】--- 递归算法应用-五行代码速解汉诺塔问题.
  • 原文地址:https://blog.csdn.net/sohoqq/article/details/132620906