量化交易 | 涨停回调短线策略

论坛 期权论坛 期权     
期权匿名问答   2022-10-17 21:40   6222   0
思路其实很简单,就是找短线强势股:
买入条件:上上一日涨停,上一日未涨停,今日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 = '1d', fields = ('close', 'high_limit', 'high', 'open'), 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='1m',fields=['money','volume'],count=timerange)
            http://g.info[stock]=max(item1history['volume'])
return ret
def get_fullist(context):
    stocks = get_index_stocks('000002.XSHG') + get_index_stocks('399106.XSHE')
    stocks = [stock for stock in stocks if not stock.startswith('300')]
return list(stocks)
# 初始化函数,设定基准等等
def initialize(context):
    g.stocks = get_fullist(context)
    g.stock = None
    g.last_price = 0.00
    # 设定沪深300作为基准
set_benchmark('000300.XSHG')
    # 开启动态复权模式(真实价格)
set_option('use_real_price', True)
    # 输出内容到日志 http://log.info()
    http://log.info('初始函数开始运行且全局只运行一次')
    # 过滤掉order系列API产生的比error级别低的log
    # log.set_level('order', 'error')
    ### 股票相关设定 ###
    # 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
    ## 运行函数(reference_security为运行时间的参考标的;传入的标的只做种类区分,因此传入'000300.XSHG'或'510300.XSHG'是一样的)
      # 开盘前运行
run_daily(before_market_open, time='before_open', reference_security='000300.XSHG')
      # 开盘时运行
run_daily(market_sell, time='14:55', reference_security='000300.XSHG')
run_daily(market_buy, time='09:35', reference_security='000300.XSHG')
    #run_daily(before_market_close, time='14:45', reference_security='000300.XSHG')
      # 收盘后运行
run_daily(after_market_close, time='after_close', reference_security='000300.XSHG')
def handle_data(context, data):
return
## 开盘前运行函数
def before_market_open(context):
    # 输出运行时间
    http://log.info('函数运行时间(before_market_open):'+str(context.current_dt.time()))
    # 给微信发送消息(添加模拟交易,并绑定微信生效)
    # send_message('美好的一天~')
    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("%Y-%m-%d"))
    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 = '1d', fields = ('close'), 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('函数运行时间(market_open):'+str(context.current_dt.time()))
    temp = {}
print(g.stocks)
for stock in g.stocks:
        h = attribute_history(stock, 1, unit = '1d', fields = ('close','open'), 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("底开过滤" + stock)
        #     continue
if change >= 0.05 and current_price < current_data[stock].high_limit:
            # temp[change] = stock
            # m1history = attribute_history(stock,unit='1m',fields=['close', 'open', 'money','volume'],count=5)
            # # print(stock + ":1分钟量能ok"+ str(max(m1history['volume'])) + " maxmxaVol:" + str(http://g.info[stock]) + ":price" + str(current_price))
            # #5分钟涨幅大于2%
            # if max((m1history['close'] - m1history['open']) / m1history['open']) <= 0.018:
            #     # print(stock + "量够了,但是涨幅不够"+ str(m1history['open'][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("向下" + 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('函数运行时间(market_open):'+str(context.current_dt.time()))
## 收盘后运行函数
def after_market_close(context):
    http://log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
    #得到当天所有成交记录
    trades = get_trades()
for _trade in trades.values():
        http://log.info('成交记录:'+str(_trade))
    http://log.info('一天结束')
    http://log.info('##############################################################')
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:393137
帖子:78628
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP