
```
from jqdata import *
import pandas as pd
import datetime
import math
from pandas import Series,DataFrame
#uuhasoo2019
def initialize(context):
'''
#g.index_list = ['000005.XSHG','000028.XSHG','000069.XSHG','000001.XSHG',\
'000016.XSHG','000925.XSHG','399649.XSHE','000913.XSHG',\
'399373.XSHE','399377.XSHE','000092.XSHG','000006.XSHG',\
'399326.XSHE','000300.XSHG','399006.XSHE','399106.XSHE',\
'000968.XSHG','000969.XSHG','000914.XSHG','000912.XSHG',\
'000915.XSHG','399234.XSHE','399673.XSHE','399997.XSHE',\
'000994.XSHG','000987.XSHG','000826.XSHG']
'''
#把所有需要的指数或概念指数都写这个列表里,多了速度会慢,我取3个指数为例
g.index_list = ['000005.XSHG','000028.XSHG','000069.XSHG']
run_daily(check_list, time='every_bar')
run_daily(buy_list,time='every_bar')
run_daily(after_market_close, time='after_close', reference_security='000300.XSHG')
run_daily(before_market_open, time='before_open', reference_security='000300.XSHG')
run_daily(sell_list, time='every_bar')
run_daily(sell_all, time='every_bar')
def sell_all(context):
hour = context.current_dt.hour
minute = context.current_dt.minute
#if (hour == 14 and minute == 48):
# for code in context.portfolio.positions.keys():
# order_target(code,0)
def sell_list(context):
for code in g.sell_list:
df = get_bars(code,2,unit='1d', fields=('date','open', 'high', 'low', \
'close','volume', 'money'),include_now=True,end_dt=None)
_open = df['open']
_close = df['close']
_high = df['high']
_low = df['low']
_volume = df['volume']
_money = df['money']
df_1 = get_bars(code,2,unit='1m', fields=('date','open', 'high', 'low', \
'close','volume', 'money'),include_now=True,end_dt=None)
_close_1 = df_1['close']
_low_1 = df_1['low']
_high_1 = df_1['high']
if (_low_1[-2] < _low[-2] * 0.995) or \
(((_high[-1]-_close[-2])/_close[-2] > 0.05) and ((_close_1[-1]-_high[-1])/_high[-1] < -0.03)):
order_target(code,0)
log.info('卖出股票:',code,get_security_info(code).display_name)
g.sell_list.remove(code)
return
def before_market_open(context):
g.list_end = []
g.list_end2 = {}
g.list_end3=[]
g.sell_list = list(context.portfolio.positions.keys())
log.info('################今天交易开始################')
if len(g.sell_list):
log.info('目前有持仓:%s'%(g.sell_list))
def after_market_close(context):
log.info('################今天交易结束################')
# 得到当天所有成交记录
trades = get_trades()
for _trade in trades.values():
log.info('今天的成交记录:'+str(_trade))
def buy_list(context):
hour = context.current_dt.hour
minute = context.current_dt.minute
if (hour == 14 and minute == 52):
cash = context.portfolio.available_cash * 0.05 #现金5%买入
for code in g.list_end3:
order_value(code,cash)
log.info('买入:',code,get_security_info(code).display_name)
def check_list(context):
hour = context.current_dt.hour
minute = context.current_dt.minute
if (hour == 14 and minute == 50):
check_tech(g.index_list)
def check_tech(index_list):
all_code= []
for list1 in index_list:
code = get_index_stocks(list1,date=None)
all_code += code
log.info('列表总数:',len(all_code))
dic={}
g.list_end=dic.fromkeys(all_code).keys()
log.info('去重后数:',len(g.list_end))
curr_data = get_current_data()
g.list_end = [stock for stock in g.list_end if \
(not curr_data[stock].paused)
and (not curr_data[stock].is_st)
and ('ST' not in curr_data[stock].name)
and ('*' not in curr_data[stock].name)
and (not curr_data[stock].day_open >= curr_data[stock].high_limit)
]
log.info('去st后数:',len(g.list_end))
g.list_end2={}
g.list_end3=[]
for code in g.list_end:
df = get_bars(code,3,unit='1d', fields=('date','open', 'high', 'low', \
'close','volume', 'money'),include_now=True,end_dt=None)
_open = df['open']
_close = df['close']
_high = df['high']
_low = df['low']
_volume = df['volume']
_money = df['money']
df_1 = get_bars(code,1,unit='1m', fields=('date','open', 'high', 'low', \
'close','volume', 'money'),include_now=True,end_dt=None)
_close_1 = df_1['close']
#所有判断条件(真阴,真阳,成交量,价格1.015,成交额1亿,涨幅小于5%)
if (_close[-2]< _close[-3] and _close[-2]< _open[-2]) \
and (_close_1[-1]>_close[-2] and _close_1[-1]>_open[-1]) \
and (_volume[-1]>_volume[-2]) and (_high[-1]/_close_1[-1]< 1.015) \
and ((_close_1[-1]-_open[-1])/_open[-1]< 0.05) \
and (_money[-1]>100000000):
log.info('所有条件符合:',code,get_security_info(code).display_name,'成交金额:',_money[-1])
g.list_end2[code]=_money[-1]
if g.list_end2:
g.list_end2=sorted(g.list_end2.items(),key=lambda asd:asd[1],reverse=True)
#log.info(g.list_end2)
for i in g.list_end2:
g.list_end3.append(i[0])
g.list_end3 = g.list_end3[:20] #取排序后前20名
log.info('排序后最终列表为:',g.list_end3)
'''###############################################'''
def REF(Series, N):
Series = pd.Series(Series)
var = Series.diff(N)
var = Series - var
return var.values
code
```
2019-11-04