思路其实很简单,就是找短线强势股:
买入条件:上上一日涨停,上一日未涨停,今日9:35分时涨5%以上
卖出条件:每日14:55检查下当前涨幅是否小于0,如果小于0就卖出
仓位管理:把资金等分5份,发现一只符合条件的股票就买入一份。
目前这个策略是没择时的,每个交易日都会无脑买入符合条件的股票。但做短线有经验的股民应该知道,做短线择时是非常重要的,如果给这个策略配上择时,我估计效果会更好,择时指标最好用市场情绪,涨停温度计这种。
虽然收益还是一般,但目前来看,这个已经是众多短线策略里,相对比较靠谱的了。
from jqdata import *
def filter_paused_stock(stock_list):
current_data = get_current_data()
return [stock for stock in stock_list if not current_data[stock].paused]
def delisted_filter(security_list):
current_data = get_current_data()
security_list = [stock for stock in security_list if not '退' in current_data[stock].name]
return security_list
def st_filter(security_list):
current_data = get_current_data()
security_list = [stock for stock in security_list if not current_data[stock].is_st]
return security_list
# 过滤新股
def delect_stop(stocks,begin_date,n=30*4):
stock_list = list()
begin_date = datetime.datetime.strptime(begin_date,"%Y-%m-%d")
for stock in stocks:
start_date = get_security_info(stock).start_date
if start_date < (begin_date - datetime.timedelta(days=n)).date():
stock_list.append(stock)
return stock_list
# 筛选涨停股票
def high_limit_filter(stocks):
ret = []
http://g.info = {}
cd = get_current_data()
for stock in stocks:
h = attribute_history(stock, 4, unit = &#39;1d&#39;, fields = (&#39;close&#39;, &#39;high_limit&#39;, &#39;high&#39;, &#39;open&#39;), skip_paused = True)
if h.close[-2] == h.high_limit[-2] and not (h.close[-1] - h.close[-2]) / h.close[-2] > 0.095:
ret.append(stock)
timerange = 240
item1history = attribute_history(stock,unit=&#39;1m&#39;,fields=[&#39;money&#39;,&#39;volume&#39;],count=timerange)
http://g.info[stock]=max(item1history[&#39;volume&#39;])
return ret
def get_fullist(context):
stocks = get_index_stocks(&#39;000002.XSHG&#39;) + get_index_stocks(&#39;399106.XSHE&#39;)
stocks = [stock for stock in stocks if not stock.startswith(&#39;300&#39;)]
return list(stocks)
# 初始化函数,设定基准等等
def initialize(context):
g.stocks = get_fullist(context)
g.stock = None
g.last_price = 0.00
# 设定沪深300作为基准
set_benchmark(&#39;000300.XSHG&#39;)
# 开启动态复权模式(真实价格)
set_option(&#39;use_real_price&#39;, True)
# 输出内容到日志 http://log.info()
http://log.info(&#39;初始函数开始运行且全局只运行一次&#39;)
# 过滤掉order系列API产生的比error级别低的log
# log.set_level(&#39;order&#39;, &#39;error&#39;)
### 股票相关设定 ###
# 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type=&#39;stock&#39;)
## 运行函数(reference_security为运行时间的参考标的;传入的标的只做种类区分,因此传入&#39;000300.XSHG&#39;或&#39;510300.XSHG&#39;是一样的)
# 开盘前运行
run_daily(before_market_open, time=&#39;before_open&#39;, reference_security=&#39;000300.XSHG&#39;)
# 开盘时运行
run_daily(market_sell, time=&#39;14:55&#39;, reference_security=&#39;000300.XSHG&#39;)
run_daily(market_buy, time=&#39;09:35&#39;, reference_security=&#39;000300.XSHG&#39;)
#run_daily(before_market_close, time=&#39;14:45&#39;, reference_security=&#39;000300.XSHG&#39;)
# 收盘后运行
run_daily(after_market_close, time=&#39;after_close&#39;, reference_security=&#39;000300.XSHG&#39;)
def handle_data(context, data):
return
## 开盘前运行函数
def before_market_open(context):
# 输出运行时间
http://log.info(&#39;函数运行时间(before_market_open):&#39;+str(context.current_dt.time()))
# 给微信发送消息(添加模拟交易,并绑定微信生效)
# send_message(&#39;美好的一天~&#39;)
g.stocks = get_fullist(context)
g.stocks = filter_paused_stock(g.stocks)
g.stocks = delisted_filter(g.stocks)
g.stocks = st_filter(g.stocks)
g.stocks = delect_stop(g.stocks,context.current_dt.strftime(&#34;%Y-%m-%d&#34;))
g.stocks = high_limit_filter(g.stocks)
print(g.stocks)
g.selllist = list(context.portfolio.positions)
def market_sell(context):
current_data = get_current_data()
for stock in context.portfolio.positions.keys():
h = attribute_history(stock, 1, unit = &#39;1d&#39;, fields = (&#39;close&#39;), skip_paused = True)
last_close = h.close[0]
current_price = current_data[stock].last_price
cost = context.portfolio.positions[stock].avg_cost
profit = (current_price - cost) / cost
change = (current_price - last_close) / last_close
init_time = context.portfolio.positions[stock].init_time
# 低开直接卖出
if change <= 0.00:
order_target(stock, 0)
## 开盘时运行函数
def market_buy(context):
current_data = get_current_data()
cash = context.portfolio.total_value
http://log.info(&#39;函数运行时间(market_open):&#39;+str(context.current_dt.time()))
temp = {}
print(g.stocks)
for stock in g.stocks:
h = attribute_history(stock, 1, unit = &#39;1d&#39;, fields = (&#39;close&#39;,&#39;open&#39;), skip_paused = True)
last_close = h.close[0]
current_price = current_data[stock].last_price
change = (current_price - last_close) / last_close
# if (current_data[stock].day_open <= last_close):
# print(&#34;底开过滤&#34; + stock)
# continue
if change >= 0.05 and current_price < current_data[stock].high_limit:
# temp[change] = stock
# m1history = attribute_history(stock,unit=&#39;1m&#39;,fields=[&#39;close&#39;, &#39;open&#39;, &#39;money&#39;,&#39;volume&#39;],count=5)
# # print(stock + &#34;:1分钟量能ok&#34;+ str(max(m1history[&#39;volume&#39;])) + &#34; maxmxaVol:&#34; + str(http://g.info[stock]) + &#34;:price&#34; + str(current_price))
# #5分钟涨幅大于2%
# if max((m1history[&#39;close&#39;] - m1history[&#39;open&#39;]) / m1history[&#39;open&#39;]) <= 0.018:
# # print(stock + &#34;量够了,但是涨幅不够&#34;+ str(m1history[&#39;open&#39;][0]))
# continue
# if (current_price - current_data[stock].day_open)/ current_data[stock].day_open < 0.01:
# # if current_price < current_data[stock].day_open:
# print(&#34;向下&#34; + stock)
# continue
temp[change] = stock
temp = [temp[change] for change in sorted(temp.keys(), reverse = True)]
for stock in temp:
order_value(stock, cash / 5)
def before_market_close(context):
current_data = get_current_data()
http://log.info(&#39;函数运行时间(market_open):&#39;+str(context.current_dt.time()))
## 收盘后运行函数
def after_market_close(context):
http://log.info(str(&#39;函数运行时间(after_market_close):&#39;+str(context.current_dt.time())))
#得到当天所有成交记录
trades = get_trades()
for _trade in trades.values():
http://log.info(&#39;成交记录:&#39;+str(_trade))
http://log.info(&#39;一天结束&#39;)
http://log.info(&#39;##############################################################&#39;) |
|