大家在选股票量化接口,自动交易接口时,会有一些以外部插件和软件的形式存在的工具,这些工具可能涉及窗口控件操作、DLL绑定甚至内存注入等技术,从安全角度来看必然存在风险,比如窗口操作类的插件,一旦出错,输入不正确的股票代码或金额,就可能导致资金损失,此外,还有账户被盗取的风险,被非法访问并操控,或者被券商封禁账户,最终,这些操作还可能涉及严重的法律风险,涉及刑事范围,因此,在选择和使用时,务必审慎考虑合法性合规性。
现在量化交易接口的门槛已经降低了,个人账户入金1万就可以开权限,头部券商,开户渠道选对,费率还会大大降低,如果有好的交易策略,自动化程序化交易就可以避免人工操作时的情绪化和操作失误
比如我在用的国金证券量化交易客户端,支持股票、基金、可转债、期权、期货等
自带了很多策略可以参考,包含多因子、日内回转、机器学习、双均线等不同类型的策略
很多操作只需要一行代码:
- #获取实时行情
- get_full_tick(['股票代码1','股票代码2'])
- #下载历史数据,支持tick级分笔数据,最早可以获取到中国股市开市以来的所有数据
- download_history_data('股票代码','k线类型','开始时间','结束时间')
模拟、实盘、回测均支持,根据你需要可以非常灵活开放,python、VBA无门槛上手
费率方面,大部分做量化交易的肯定交易频率会很高,找不到好的客户经理,拿着市面上常见的万2.5费率,像我的交易比较频繁,主要集中在可转债和场内ETF上,一天几个来回交易费用得差大几百几千的,费率低意味着交易机会多,拿可转债来说,我的是万0.4免5,以113050南银转债来说,做网格交易一天可以有几百次机会,挂买一成交后立即挂卖一出手,简直是捡钱一样,而如果按照市面上常见的万3或万2不免5,算上交易成本,加上是人工操作,就干脆不要想这种这交易方式,根本没机会出手,直接就被套住了
以下是一段网格交易策略参考,使用过程中有任何不懂的可以留言或私信与我交流
- import numpy as np
- import pandas as pd
- import time
- import datetime
- def init(ContextInfo):
- #设置图为标的
- ContextInfo.tradefuture = ContextInfo.stockcode+"."+ContextInfo.market
- ContextInfo.set_universe([ContextInfo.tradefuture])
- print(ContextInfo.get_universe())
- ContextInfo.timeseries = pd.DataFrame()
- ContextInfo.band = np.zeros(5)
- #print 'ContextInfo.band',ContextInfo.band
-
- # 设置网格的仓位
- ContextInfo.weight = [0.25, 0.15, 0.0, 0.15, 0.25]
- # 获取多仓仓位
- ContextInfo.position_long = 0
- # 获取孔仓仓位
- ContextInfo.position_short = 0
-
- #剩余资金
- ContextInfo.surpluscapital = ContextInfo.capital
- #保证金比率
- comdict = ContextInfo.get_commission()
- ContextInfo.marginratio = comdict['margin_ratio']
- #合约乘数
- ContextInfo.multiplier = ContextInfo.get_contract_multiplier(ContextInfo.tradefuture)
- #账号
- ContextInfo.accountid='testF'
- ContextInfo.now_timestamp = time.time()
- def handlebar(ContextInfo):
- index = ContextInfo.barpos
- realtimetag = ContextInfo.get_bar_timetag(index)
- lasttimetag = ContextInfo.get_bar_timetag(index - 1)
- print(timetag_to_datetime(realtimetag, '%Y-%m-%d %H:%M:%S'))
- if ContextInfo.period in ['1m','3m','5m','15m','30m'] and not ContextInfo.do_back_test:
- if (datetime.datetime.fromtimestamp(ContextInfo.now_timestamp) - datetime.datetime.fromtimestamp(realtimetag / 1000)).days > 7:
- return
- starttime = timetag_to_datetime(realtimetag-86400000 * 10, '%Y%m%d%H%M%S')
- endtime = timetag_to_datetime(realtimetag-86400000, '%Y%m%d%H%M%S')
- #print 'starttime,endtime',starttime,endtime
- 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')
- close_sort = Result['close'].sort_index(axis = 0,ascending = True)
- #print close_sort,starttime,endtime
- #过去300个价格数据的均值和标准差
- Result_mean = close_sort.tail(300).mean()
- Result_std = close_sort.tail(300).std()
- ContextInfo.band = Result_mean + np.array([-40, -3, -2, 2, 3, 40]) * Result_std
- #print 'ContextInfo.band',ContextInfo.band
- if np.isnan(ContextInfo.band).any() or Result_std==0:
- return
- if index > 0:
- lasttimetag = ContextInfo.get_bar_timetag(index - 1)
- #前一根bar收盘价
- close_lastbar = ContextInfo.get_market_data (['close'],stock_code=[ContextInfo.tradefuture],period=ContextInfo.period,dividend_type='front')
- #当前开盘价
- open_currentbar = ContextInfo.get_market_data (['open'],stock_code=[ContextInfo.tradefuture],period=ContextInfo.period,dividend_type='front')
- #划分网格
- #print close_lastbar,ContextInfo.band
- grid = pd.cut([close_lastbar], ContextInfo.band, labels=[0, 1, 2, 3, 4])[0]
- #print 'grid ',grid
- if not ContextInfo.do_back_test:
- ContextInfo.paint('grid',float(grid),-1,0)
- # 若无仓位且价格突破则按照设置好的区间开仓
- if ContextInfo.position_long == 0 and ContextInfo.position_short == 0 and grid != 2:
- # 大于3为在中间网格的上方,做多
- if grid >= 3 and ContextInfo.surpluscapital > 0 :
- long_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.marginratio*close_lastbar*ContextInfo.multiplier))
- ContextInfo.position_long = long_num
- buy_open(ContextInfo.tradefuture,long_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= long_num * ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier
- #print '开多'
- elif grid <= 1 and ContextInfo.surpluscapital > 0 :
- short_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.marginratio*close_lastbar*ContextInfo.multiplier))
- ContextInfo.position_short = short_num
- sell_open(ContextInfo.tradefuture,short_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= short_num * ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier
- #print '开空'
- # 持有多仓的处理
- elif ContextInfo.position_long > 0 :
- if grid >= 3 and ContextInfo.surpluscapital > 0 :
- targetlong_num = int(ContextInfo.weight[grid] * (ContextInfo.surpluscapital + ContextInfo.multiplier * close_lastbar * ContextInfo.position_long*ContextInfo.marginratio)/ (ContextInfo.marginratio*close_lastbar * ContextInfo.multiplier))
- if targetlong_num > ContextInfo.position_long :
- trade_num = targetlong_num - ContextInfo.position_long
- ContextInfo.position_long = targetlong_num
- buy_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- elif targetlong_num < ContextInfo.position_long:
- trade_num = ContextInfo.position_long - targetlong_num
- ContextInfo.position_long = targetlong_num
- sell_close_tdayfirst(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- #print '调多仓到仓位'
- # 等于2为在中间网格,平仓
- elif grid == 2:
- sell_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_long,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += ContextInfo.position_long * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- ContextInfo.position_long = 0
- #print '平多'
- # 小于1为在中间网格的下方,做空
- elif grid <= 1:
- sell_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_long,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += ContextInfo.position_long * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- ContextInfo.position_long = 0
- #print '全平多仓'
- if ContextInfo.surpluscapital > 0 :
- short_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital/(ContextInfo.multiplier * ContextInfo.marginratio * close_lastbar))
- ContextInfo.position_short = short_num
- sell_open(ContextInfo.tradefuture,short_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= short_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- #print '开空仓到仓位'
-
- # 持有空仓的处理
- elif ContextInfo.position_short> 0 :
- # 小于1为在中间网格的下方,做空
- if grid <= 1:
- targetlshort_num = int(ContextInfo.weight[grid]*(ContextInfo.surpluscapital + ContextInfo.multiplier*close_lastbar*ContextInfo.position_short*ContextInfo.marginratio)/(ContextInfo.multiplier * ContextInfo.marginratio * close_lastbar))
- if targetlshort_num > ContextInfo.position_short:
- trade_num = targetlshort_num - ContextInfo.position_short
- ContextInfo.position_short = targetlshort_num
- sell_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- #print '开空仓到仓位' ,targetlshort_num
- elif targetlshort_num < ContextInfo.position_short:
- trade_num = ContextInfo.position_short - targetlshort_num
- ContextInfo.position_short = targetlshort_num
- buy_close_tdayfirst(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- #print '平空仓到仓位' ,targetlshort_num
- # 等于2为在中间网格,平仓
- elif grid == 2:
- buy_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_short,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += ContextInfo.position_short * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- ContextInfo.position_short = 0
- #print '全平空仓'
- # 大于3为在中间网格的上方,做多
- elif grid >= 3:
- buy_close_tdayfirst(ContextInfo.tradefuture,ContextInfo.position_short,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital += ContextInfo.position_short * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- ContextInfo.position_short = 0
- #print '全平空仓'
- if ContextInfo.surpluscapital > 0 :
- trade_num = int(ContextInfo.weight[grid]*ContextInfo.surpluscapital / (ContextInfo.marginratio * close_lastbar * ContextInfo.multiplier))
- ContextInfo.position_long = trade_num
- buy_open(ContextInfo.tradefuture,trade_num,'fix',close_lastbar,ContextInfo,ContextInfo.accountid)
- ContextInfo.surpluscapital -= trade_num * close_lastbar * ContextInfo.marginratio * ContextInfo.multiplier
- #print ' 开多仓到仓位'
- # 获取多仓仓位
- #print 'ContextInfo.position_long',ContextInfo.position_long
- # 获取空仓仓位
- #print 'ContextInfo.position_short',ContextInfo.position_short
- # 获取剩余资金
- #print 'ContextInfo.surpluscapital',ContextInfo.surpluscapital
使用过程中有任何不懂的可以留言或私信与我交流
------