量化交易2

本文详细介绍了使用程序化交易接口进行买入和卖出操作的步骤,包括参数设置、URL拼接和请求执行。通过示例展示了如何以特定价格买入和卖出指定数量的股票,并通过查看持仓来确认交易成功。最后,验证了交易记录以确保操作无误。
摘要由CSDN通过智能技术生成

前篇程序化交易的续集,在搞定接口之后自然是进行实际的交易了,因为我的账户只有300块,交易一次就要5元,本人目前失业在家,实在伤不起,多谢网友支持所以才有了这个续篇。

使用接口进行买卖

准备:

所使用的接口在上篇文章中都已提到
 

因为实际测试了几次,以下是接口各参数的意义
&request_id=buystock_302&stock_account=沪深账号 
&exchange_type=1 #沪市1 深市2 
&entrust_prop=0 #委托方式,因为我使用的都是默认的普通委托,所以为0不变
&entrust_bs=1 #买入1 卖出2 不变
&stock_code=股票ID&entrust_price=价格&entrust_amount=数量
&elig_riskmatch_flag=1 #这个是用户风险等级,不变

 

先拼接url生成对应的接口格式:

以下命令表示我们以SZ账户在 SZ市场 以0.58的价格买入162411(华宝油气)100股

account->沪深账户 | market->市场ID | stockid->股票代码 | price->价格 | amount->数量

url = api['buy'].format(account=SZACCOUNT, market=SZ, stockid='162411', price='0.58', amount='100')

生成的url:
'https://jy.yongjinbao.com.cn/winner_gj/gjzq/stock/exchange.action?CSRF_Token=undefined&timestamp=0.11111111111111111&request_id=buystock_302
&stock_account=股票账户 # account = SZACCOUNT
&exchange_type=2 # SZ = 2
&entrust_prop=0
&entrust_bs=1
&stock_code=162411 # stockid = '162411'
&entrust_price=0.58 # price = '0.58'
&entrust_amount=100 # amount = '100'
&elig_riskmatch_flag=1'

执行命令:

r = requests.get(url, cookies=ck)

返回的数据格式

{
entrust_no: '委托编号',
init_date: '发生日期',
batch_no: '委托批号',
report_no: '申报号',
seat_no: '席位编号',
entrust_time: '委托时间',
entrust_price: '委托价格',
entrust_amount: '委托数量',
stock_code: '证券代码',
entrust_bs: '买卖方向',
entrust_type: '委托类别',
entrust_status: '委托状态',
fund_account: '资金帐号',
error_no: '错误号',
error_info: '错误原因'
}

错误号在正常执行时值为0,由此可判断命令是否出错
 

查看持仓:

r = requests.get(api['chican'], cookies=ck)

返回的数据:

[{'cost_price': '0.681', # 成本价
'current_amount': '100', # 当前数量
'enable_amount': '100', # 可用数量
'income_balance': '-11.100', # 浮动盈亏
'keep_cost_price': '0.681',
'last_price': '0.570',
'market_value': '57.000', # 证券市值
'position_str': '定位用的字符串,无意义',
'stock_code': '162411',
'stock_name': '华宝油气'}]

观察证券数量可见已经成功执行

卖出

接口参数

&service_type=stock&request_id=sellstock_302
&stock_account=股票账户
&exchange_type=2 # 沪市1 深市2
&entrust_prop=0 # 同上
&entrust_bs=2 # 买入1 卖出2
&stock_code=162411 # 股票代码
&entrust_price=0.56 # 价格
&entrust_amount=100' # 数量

拼接url

以下命令表示我们以SZ账户在 SZ市场 以 0.56的价格卖出162411(华宝油气)100股

account->沪深账户 | market->市场ID | stockid->股票代码 | price->价格 | amount->数量

url = api['sell'].format(account=SZACCOUNT, market=SZ, stockid='162411', price='0.56', amount='100')


生成的url:
'https://jy.yongjinbao.com.cn/winner_gj/gjzq/stock/exchange.action?CSRF_Token=undefined&timestamp=0.11111111111111111&service_type=stock&request_id=sellstock_302&stock_account=深市账号&exchange_type=2&entrust_prop=0&entrust_bs=2&stock_code=162411&entrust_price=0.56&entrust_amount=100'

执行

r = requests.get(url, cookies=ck)

查看持仓

r = requests.get(api['chican'], cookies=ck)


返回的数据

[{'cost_price': '0.000',
'current_amount': '0', # 当前数量
'enable_amount': '0', # 可用数量
'income_balance': '-11.100',
'keep_cost_price': '0.000',
'last_price': '0.571',
'market_value': '0.000', # 证券市值
'position_str': '定位字符串,无意义',
'stock_code': '162411',
'stock_name': '华宝油气'}]


观察证券数量发现已经归0可见卖出成功
 

证实

我们去web客户端截取一下我们委托记录看看是否 正常执行
 

结尾

还有很多可以优化的地方,因为不知道股票代码跟沪深的关系,所以写死了沪深ID和账号。
其实在发现接口之后剩下的功能不过是对接口进行组合拼接以实现我们想要功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值