包含MACD日线,盘中量比选优,定时运行的实盘策略,可在其基础上继续优化
import pandas as pd
import numpy as np
import talib ## 使用talib计算MACD的参数
import time
import sys
import json
from jqlib.technical_analysis import *
#this version to develop yellowline and every time focus time context#
def initialize(context):
g.security = ['000030.XSHE', '000036.XSHE', '000069.XSHE', '000488.XSHE',
'000501.XSHE', '000537.XSHE', '000581.XSHE', '000591.XSHE',
'000636.XSHE', '000651.XSHE', '000717.XSHE', '000720.XSHE',
'000825.XSHE', '000830.XSHE', '000877.XSHE', '000885.XSHE',
'000898.XSHE', '000900.XSHE', '000921.XSHE', '000926.XSHE',
'000932.XSHE', '000983.XSHE', '002068.XSHE', '002078.XSHE',
'002092.XSHE', '002110.XSHE', '002128.XSHE', '002195.XSHE',
'002462.XSHE', '002589.XSHE', '300145.XSHE']
set_backtest()
run_daily(begin,time='09:30')
run_daily(trade,time='09:33')
#run_daily(trade,time='09:34')
#run_daily(trade,time='09:35')
run_daily(trade,time='09:36')
#run_daily(trade,time='09:37')
#run_daily(trade,time='09:39')
run_daily(trade,time='09:40')
run_daily(trade,time='09:45')
run_daily(trade,time='09:50')
run_daily(trade,time='09:55')
run_time(30)#每30分钟运行一次
#run_daily(trade1,time='10:00')
def run_time(x):
datas = get_price('000300.XSHG',count=200,frequency='1m').index.tolist()[:-1]#剔除15:00
times = [str(t)[-8:] for t in datas]
#print(times)
#times.insert(0,'09:30:00')
for t in times[::x]:
run_daily(trade1, t)
def set_backtest():
set_benchmark('000300.XSHG')
set_option('use_real_price', True)
log.set_level('order', 'error')
def order_dict(dicts, n):
result = []
result1 = []
p = sorted([(k, v) for k, v in dicts.items()], reverse=True)
s = set()
for i in p:
s.add(i[1])
for i in sorted(s, reverse=True)[:n]:
for j in p:
if j[1] == i:
result.append(j)
for r in result:
result1.append(r[0])
#print (result1)
return result1
def get_v_ratio(security_list,time):
#'''计算某个时间点股票的量比,
#输入:股票池/股票,时间(datetime)
#返回:截至当天某一时间点的量比'''
if isinstance(security_list, str):
security_list = [security_list]
d = {}
time2= time-datetime.timedelta(days=1)
start_time = datetime.datetime(time.year,time.month,time.day,9,30)
for security in security_list:
ma5 = get_price(security,end_date=time2,count=5,skip_paused=True,fields='volume').volume.sum()/1200
n_ma = get_price(security,end_date=time,start_date =start_time ,skip_paused=True,fields='volume',
frequency='1m').volume.mean()
d[security]=n_ma/ma5
return d
#分时黄金线实际是当日平均价#
def get_yellowline(security_list,time):
if isinstance(security_list, str):
security_list = [security_list]
d = {}
time2= time-datetime.timedelta(days=1)
start_time = datetime.datetime(time.year,time.month,time.day,9,30)
for security in security_list:
df1=get_price(security,end_date=time, start_date=start_time,frequency='1m', fields='avg', skip_paused=False, fq='none')
series1=df1['avg'].mean()
d[security]=series1
return d
####1.盘前只留MACD day good####
def begin(context):
stockpoolbegin = g.security
long_list = []
g.short_list = []
result = []
g.stockpool = []
g.stockpoollist= []
g.hold = []
g.stockset = list(context.portfolio.positions.keys())
#print('portfolio_value',context.portfolio.portfolio_value)
for stock in stockpoolbegin:
#prices = attribute_history(stock,300, '1d',['close'])
prices = get_bars(stock, 50, '1d',include_now=True)
price = array(prices['close'])
#print(price)
macd_tmp = talib.MACD(price, fastperiod=12, slowperiod=26, signalperiod=20)
#print(macd_tmp)
DIF = macd_tmp[0]
DEA = macd_tmp[1]
MACD = np.round(macd_tmp[2],3)
#print(MACD)
# 判断MACD走向
if MACD[-1] > 0:
#if MACD[-1] < 0 and MACD[-1] > MACD[-2] and MACD[-2] <MACD[-3]:
print(MACD[-2],MACD[-1],'choose it',stock)
r = (stock,MACD[-1])
result.append(r)
#g.stockpool.append(stock)
elif MACD[-1] < 0:
#elif MACD[-1] > 0 and MACD[-1] < MACD[-2] and MACD[-2] >MACD[-3]:
print(MACD[-2],MACD[-1],'exclude it',stock)
g.short_list.append(stock)
#elif MACD[-1] > 0 and MACD[-1] > MACD[-2]*1.3:
#long_list.append(stock)
#elif MACD[-1] < MACD[-2] *0.8:
#short_list.append(stock)
result = np.array(result)
columns = ['stock','macd-1']
num_columns =['macd-1']
g.stockpoollist=pd.DataFrame(data = result, columns=columns)
#g.stockpoollist[num_columns] = g.stockpoollist[num_columns].apply(pd.to_numeric, errors='ingnore')
g.stockpoollist=g.stockpoollist.sort_index(axis = 0,ascending =False,by = 'macd-1')
g.stockpool=g.stockpoollist.head(50)['stock']
#print(g.stockpoollist)
print('short_list by day MACD',g.short_list)
print('choosed by day MACD',g.stockpool)
for stock in g.stockset:
if stock in g.short_list:
print('sell it right now with bad day MACD', stock)
order_target_value(stock, 0)
#order_value(stock, -10000)
else:
g.hold.append(stock)#如果不在卖出列表里则持有
print('temp hold for today following test',g.hold)
##2.Qucik handle the valume ratio good' stock to buy or not##
def trade(context):
#按分钟回测/模拟, 在每分钟的第一秒运行, 每天执行240次,
security_list =g.stockpool.tolist()
#time = datetime.datetime(2019,3,14,15,00)
time=datetime.datetime.now()
#回测中:
time = context.current_dt
volume_ratio=get_v_ratio(security_list,time)
print ('volume_ratio',volume_ratio)
stockpool=order_dict(volume_ratio, 3)
print ('after filter',stockpool)
g.stockset = list(context.portfolio.positions.keys())
result = []
long_list = []
hold = []
g.short_list = []
for stock in stockpool:
prices = get_bars(stock, 5, '1m',include_now=True)
#print(prices)
price = array(prices['close'])
#print(price)
#macd_tmp = talib.MACD(price, fastperiod=12, slowperiod=26, signalperiod=20)
#DIF = macd_tmp[0]
#DEA = macd_tmp[1]
#MACD = np.round(macd_tmp[2],3)
#print(stock,MACD[-1],MACD[-2],MACD[-3])
time2= time-datetime.timedelta(days=1)
start_time = datetime.datetime(time.year,time.month,time.day,9,30)
df1=get_price(stock,end_date=time, start_date=start_time,frequency='1m', fields='avg', skip_paused=False, fq='none')
yellowline=df1['avg'].mean()
# 判断MACD走向??need to judge the 1 minutes rate
if price[-1] > yellowline:
#if MACD[-1] > 0: #and MACD[-2] <0:
#if MACD[-1] < 0 and MACD[-1] > MACD[-2]*1.002 and MACD[-2] <MACD[-3]*1.005:
print(price[-1],yellowline,'buy it at',price[-1],stock)
#print(MACD[-3],MACD[-2],MACD[-1],'buy it at',price[-1],stock)
long_list.append(stock)
elif price[-1] < yellowline:
#elif MACD[-1] > 0 and MACD[-1] < MACD[-2]*1.005 and MACD[-2] >MACD[-3]*1.002:
print(price[-1],yellowline,'sell it at',price[-1],stock)
#print(MACD[-3],MACD[-2],MACD[-1],'sell it at',price[-1],stock)
g.short_list.append(stock)
#elif MACD[-1] > 0 and MACD[-1] > MACD[-2]*1.3:
#long_list.append(stock)
#elif MACD[-1] < MACD[-2] *0.8:
#short_list.append(stock)
print('long_list',long_list)
print('short_list',g.short_list)
for stock in g.stockset:
if stock in g.short_list:
print('sell it right now with bad 1 minute data and high volume', stock)
#order_target_value(stock, 0)
order_value(stock, -10000)
else:
hold.append(stock)#如果不在卖出列表里则持有
#print(hold)
buy_list = []
for stock in long_list:
#if stock not in hold:
buy_list.append(stock)#新增的买入股票
if len(buy_list)==0:
Cash = context.portfolio.available_cash
#print(Cash)
else:
#Cash = context.portfolio.available_cash/10#len(buy_list)
for stock in buy_list:
#order_target_value(stock, 30000)
# 买入这么多现金的股票
order_value(stock, 10000)
print('buy right now with good 1 minute data and high volume',stock)
def trade1(context):
#5分钟for mid of day#
#print(g.stockpoollist)
security_list =g.stockpool.tolist()
#time = datetime.datetime(2019,3,14,15,00)
time=datetime.datetime.now()
#回测中:
time = context.current_dt
volume_ratio=get_v_ratio(security_list,time)
print ('volume_ratio',volume_ratio)
stockpool=order_dict(volume_ratio, 3)
print ('after filter',stockpool)
g.stockset = list(context.portfolio.positions.keys())
stockpool=list(set(stockpool+g.stockset))
#两个list比较去除重复元素,然后合并#
print('filtered combined hold for total focus',stockpool)
result = []
long_list = []
hold = []
g.short_list = []
for stock in stockpool:
pricesd = get_bars(stock, 3, '1d',include_now=True)
priced=array(pricesd['open'])
print('open price',priced[-1])
prices = get_bars(stock, 50, '5m',include_now=True)
price = array(prices['close'])
print('current price',price[-1])
#macd_tmp = talib.MACD(price, fastperiod=12, slowperiod=26, signalperiod=20)
#DIF = macd_tmp[0]
#DEA = macd_tmp[1]
#MACD = np.round(macd_tmp[2],3)
#print(stock,MACD[-1],MACD[-2],MACD[-3])
time2= time-datetime.timedelta(days=1)
start_time = datetime.datetime(time.year,time.month,time.day,9,30)
df1=get_price(stock,end_date=time, start_date=start_time,frequency='1m', fields='avg', skip_paused=False, fq='none')
yellowline=df1['avg'].mean()
# 判断MACD走向and if current price not lower than beginning price
#if MACD[-1] > 0: #and MACD[-2] <0:
if price[-1] > yellowline and price[-1]>priced[-1]:
#if MACD[-1] > 0 and MACD[-1] > MACD[-2]*1 and MACD[-1] <MACD[-2]*1.05 and price[-1]>priced[-1]:
#if MACD[-1] < 0 and MACD[-1] > MACD[-2]*1.002 and MACD[-2] <MACD[-3]*1.005 and price[-1]>priced[-1]:
print(price[-1],yellowline,'buy it at',price[-1],stock)
#?if add ratio to avoid catch too high
long_list.append(stock)
#elif MACD[-1] < 0: #and MACD[-2] >0:
#elif MACD[-1] > 0 and MACD[-1] < MACD[-2]*1.05 and MACD[-2] >MACD[-3]*1.02:
#print(MACD[-1],MACD[-2],MACD[-3],'sell it at',price[-1],stock)
else:
g.short_list.append(stock)
print('long_list',long_list)
print('short_list',g.short_list)
g.stockset = list(context.portfolio.positions.keys())
for stock in g.stockset:
if stock not in long_list: #g.short_list:
print('sell', stock)
#order_target_value(stock, 0)
order_value(stock, -5000*2)
else:
hold.append(stock)#如果不在卖出列表里则持有
#print(hold)
buy_list = []
for stock in long_list:
#if stock not in hold:
buy_list.append(stock)#新增的买入股票
if len(buy_list)==0:
Cash = context.portfolio.available_cash
#print(Cash)
else:
Cash = context.portfolio.available_cash/len(buy_list)
for stock in buy_list:
#调整股票仓位到10000元价值
#order_target_value(stock, 30000)
# 买入这么多现金的股票
order_value(stock, 5000)
print('buy')