如何实现一个简单的股票监控策略
<h4><strong>简要说明</strong></h4>
<ul>
<li>
<p>股票监控策略需要在每次行情数据有更新时,实时的对监控条件进行判断是否满足。因此,可以首先订阅所需要监控的股票代码的分时行情数据(reg_quote),并在循环中等待每次的分时行情数据更新(wait_update),有更新后,将最新价格等数据代入到监控条件中进行判断,满足条件后执行委托,再执行后续程序;不满足时继续执行后续程序,直至监控条件满足。</p>
</li>
<li>在Python策略编辑器的“经典策略”一栏中,已内置了多个常见的监控策略程序可以参考,包含了条件策略(一个策略程序执行一次委托后终止,例如股价条件)和算法策略(一个程序可执行多笔委托,例如网格交易)。</li>
</ul>
<p>下面以股价条件策略程序为例进行说明:</p>
<h4><strong>示例代码</strong></h4>
<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@brief 股价条件-参照价格:设定一个触发价格,当股价上涨或下跌到此价格时触发
下面例子为:股价达到16.84,以最新价格卖掉000001 500股
'''
from ths_api import *
def order(codelist, trade_direction, amount):
mmlb = trade_direction
zqdm = codelist
wtjg = 'zxjg'
wtsl = amount
cmdstr = '%s %s %s %s' % (mmlb, zqdm, wtjg, wtsl)
xd.cmd(cmdstr)
def main():
codelist = '000001'
threshold = 16.84 #触发委托的阈值价格
trade_direction = 'sell'
amount = '500'
api = hq.ths_hq_api() #行情实例
quote = api.reg_quote(codelist) #订阅标的的分时行情,每次有行情更新时将最新的行情数据赋值给quote变量。
api.wait_update()
price = quote[codelist]['price']
if price >= threshold: #判断是股价下破阈值价格后委托还是上破阈值价格后委托
direction = 'down'
else:
direction = 'up'
while True:
api.wait_update() #等待行情数据更新,更新后程序执行下一行代码
price = quote[codelist]['price'] #获取最新价
if price <= threshold and direction == 'down':
order(codelist, trade_direction, amount)
break
elif price >= threshold and direction == 'up':
order(codelist, trade_direction, amount)
break
else:
pass
if __name__ == '__main__':
main()
</code></pre>
<h4><strong>运行结果</strong></h4>
<p>程序运行后,日志中不断输出如下的信息,说明程序已在实时监控标的的分时行情。直到行情达到触发条件后,出现委托确认弹窗,确认后完成委托单:以最新价格卖掉000001 500股。</p>
<pre><code>[2019-10-24 10:43:13.493] [24132][173216016]等待数据更新...
[2019-10-24 10:43:16.651] [20840]收到实时行情数据,Stockcode=000001
[2019-10-24 10:43:16.652] [20840][173216016]实时数据更新
[2019-10-24 10:43:16.652] [24132][173216016]更新的数据项 = {'quotes': {'000001': ['zd', 'zsz', 'volume', 'amount', 'amount_count', 'hs', 'syl_d', 'wpcjl', 'a3_v', 'syl_j', 'a1_v', 'b1_v', 'a4_v', 'a2_v', 'ltz']}, 'klines': {}}</code></pre>
<h4><strong>补充</strong></h4>
<p>以上的示例程序可以进一步改写如下,通过增加dict_params(用于对程序的各参数给出具体设置)、dict_descs(用于描述参数输入框的名称和输入方式),编辑器的右侧出现相应的参数输入框,如下图所示。这种方式可以使你在使用同一策略时,不必再到程序内修改参数。</p>
<p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/da78cd1143d62b77e030445b9a5a5b22?showdoc=.jpg" alt="" /></p>
<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@brief 股价条件-参照价格:设定一个触发价格,当股价上涨或下跌到此价格时触发
下面例子为:股价达到16.70,以最新价格卖掉000001 500股
'''
from ths_api import *
dict_params = {
"codelist" : "000001",
"threshold" : "16.70",
"trade_direction" : "sell",
"amount" : "500",
"wtjg" : "zxjg"
}
dict_descs = {
"codelist": {
"desc": "证券代码",
"type": "edit"
},
"threshold": {
"desc": "阈值价格",
"type": "edit"
},
"trade_direction": {
"desc": "买卖方向",
"type": "combo",
"values": "buy,sell"
},
"amount": {
"desc": "委托数量",
"type": "edit"
},
"wtjg": {
"desc": "委托价格",
"type": "edit"
}
} #"type": "edit"为可输入模式,"type": "combo"为选择模式。
def order(codelist, trade_direction, amount, wtjg):
mmlb = trade_direction
zqdm = codelist
wtjg = wtjg
wtsl = amount
cmdstr = '%s %s %s %s' % (mmlb, zqdm, wtjg, wtsl)
xd.cmd(cmdstr)
def main():
codelist = dict_params['codelist'] #读取参数,读取后统一为str格式。
threshold = float(dict_params['threshold'])
trade_direction = dict_params['trade_direction']
wtjg = dict_params['wtjg']
amount = dict_params['amount']
api = hq.ths_hq_api()
quote = api.reg_quote(codelist)
api.wait_update()
price = quote[codelist]['price']
if price >= threshold:
direction = 'down'
else:
direction = 'up'
while True:
api.wait_update()
price = quote[codelist]['price']
if price <= threshold and direction == 'down':
order(codelist, trade_direction, amount, wtjg)
break
elif price >= threshold and direction == 'up':
order(codelist, trade_direction, amount, wtjg)
break
else:
pass
if __name__ == '__main__':
main()</code></pre>