策略分享

非湘财证券如何实盘——利用BigQuant 平台API与同花顺实现策略实盘操作:

由gxlzlijing创建,最终由gxlzlijing 被浏览 275 用户

  BigQuant 平台目前支持的实盘为湘财证券, 如果我们是在别的券商开的帐户,同时想在盘中读取分钟级别的行情或指标进行择时买卖,而不是按策略的开盘买收盘卖,应该如何实现呢? 通过BQ平台的API 和同花顺交易终端的python 编辑器就可以实现了:

1、 BQ API 读取自己的策略交易信号 :

import requests import datetime import json

ids = 'ec915798-d8c5-11ec-bb48-361fbc3525fa' # 这是你策略的ID , 支持id和notebook_id,用;分开。不填则返回全部正在运行的自己和已订阅的计划交易信息

key = ''' tk_cca4750dd7bf0beeb334305fe493d181 ''' # 这是你 bigquant帐号的 KEY

def request_plan_order(): url = 'https://bigquant.com/bigwebapi/algo_info/planned_orders' headers = {'Authorization': 'Bearer {}'.format(key.strip().replace( \n ", ""))} data = {'id_list': ids}

r = requests.post(url=url, data=data, headers=headers)
res_dict = json.loads(r.text)
return res_dict

2、定义买入下单函数

def order_buy( stock , amount ) :

print("符合买入下单条件")

mmlb = 'buy' zqdm = stock wtjg = 'zxjg'

wtsl = str(amount)

cmdstr = '%s %s %s %s -notip' % (mmlb, zqdm, wtjg, wtsl) print('买入'+stock) xd.cmd(cmdstr)

3、定义卖出函数

def order_sell( stock , amount ) :

print("符卖出下单条件")

mmlb = 'sell'

zqdm = stock wtjg = 'zxjg'

wtsl = str(amount)cmdstr = '%s %s %s %s -notip' % (mmlb, zqdm, wtjg, wtsl) print('卖出'+stock) xd.cmd(cmdstr)

4、定义运行过程

def main():

buy_dic   =[]
sell_dic  =[]
res = request_plan_order()
df=(res["data"])  
orderlist =   (df[0]["planned_orders"]) 
print(orderlist)
for    order   in  orderlist  :    #取出策略信号买卖清单列表
    if  order["direction"]=="买": 
        buy_list     =  {} 
        buy_list["name"]      = order["sid"][0:6] 
        buy_list["amount"]    = order["amount_after_adjust"] 
        buy_dic.append(buy_list) 
    else  :
        sell_list     =  {} 
        sell_list["name"]      = order["sid"][0:6]  
        sell_list["amount"]    = order["amount_after_adjust"] 
        sell_dic.append(sell_list) 


now_time = time.strftime("%H:%M:%S", time.localtime())   

while True:
    
    print("当前时间" +  now_time+"-----------")          
    if  int(now_time.replace(':', '')) >= int('09:30:00'.replace(':', ''))   :    #时间到达 9:30  分下单买入 
        for    codestock     in      buy_dic  :  
            order_buy(  codestock["name"]  ,  codestock["amount"]  )           
            #print(codestock["name"])
            #print(codestock["amount"])  
        break          
        
while True:
    now_time = time.strftime("%H:%M:%S", time.localtime())   
    print("当前时间" +  now_time+"-----------")          
    if  int(now_time.replace(':', '')) >= int('14:50:00'.replace(':', ''))   :   #时间到达 14:50  分下单卖出  
        for    codestock     in      buy_dic  :  
            order_buy(  codestock["name"]  ,  codestock["amount"]  )           
            #print(codestock["name"])
            #print(codestock["amount"])  
        break        

if name == 'main': main()

执行效果

{w:100}{w:100}{w:100} {w:100}{w:100}如果想实现在盘中监控实时行情择时买卖 可以增加择时指标择时下单: 如下实现 股价上穿或下穿 5 分钟 5 天均线时再进行买入或卖出下单,而不是开盘买收盘卖

def   ma_order(code)   :
ma_lenth = 5    (定义均线参数: 5 天,10 天, 20 天)
direction_klinetoma = 'down'
api = hq.ths_hq_api()quote = api.reg_quote(code)   #读取同花顺股票实时行情
while True:
    api.wait_update()
    price = quote[code]['price']
    kline = api.get_kline(code, 5 , ma_lenth  )   #   读取 5 分钟行情   ;  如果是日线行情参数写为  kline = api.get_kline(code, 24*60 , ma_lenth  )
    valuelist_ma = api.MA(kline, code, ma_lenth, m=0)    #获取5分钟   5   天实时均线     
    valuetoday_ma = valuelist_ma.iloc[-1, 0]
    print(valuelist_ma)

    if price <= valuetoday_ma and direction_klinetoma == 'down':     #如果股价下穿 5 分钟 5 天均线卖出
        sell_order(code)
        print('股价:', price)
        print('均线价格:', valuetoday_ma)
        break
    elif price > valuetoday_ma and direction_klinetoma == 'up':         #如果股价上穿 5 分钟 5 天均线买入
        buy_order(code)
        print('股价:', price)
        print('均线价格:', valuetoday_ma)
        break
    else:
        pass

\

标签

API实盘

文档

如何结合欧奈尔的RPS指标,开发AI量化策略?
评论
  • 学习交流可 + V : gxlzlijing
  • 请问这里的xd包是什么作用,如何下载?