列表中数值,怎么放入函数中 一一判断呢?
由iquant创建,最终由iquant 被浏览 47 用户
问题
老师,麻烦您看下; 第一段,是选出一系列股票 xuangu=【…】 第二段,是对某个股票做出判断,最终正确,就输出 “true”,我把这个判断,做成了一个函数 第三段,我怎么把第一段选出的股票,一个个放入 instruments(a)里的a中,如果判断正确,就输出股票
l=DataSource(‘bar1d_CN_STOCK_A’).read(instruments=[],start_date=‘2021-06-10’,end_date=None,fields=[‘open’,‘close’,‘high’,‘low’,‘amount’,‘adjust_factor’])
l[‘open+’]=l[‘open’]/l[‘adjust_factor’]
l[‘close+’]=l[‘close’]/l[‘adjust_factor’]
l[‘high+’]=l[‘high’]/l[‘adjust_factor’]
l[‘low+’]=l[‘low’]/l[‘adjust_factor’]
c1 = l[‘date’]>=‘2021-06-08’
c2 = l[‘amount’]>100000000
c3 = l[‘close+’]>4.99
l1 = l.loc[c1&c2&c3,[‘instrument’,‘date’,‘open+’,‘high+’,‘close+’,‘low’,‘amount’]]
xuangu= l1[‘instrument’].values.tolist()
def instruments(a):
instruments=a
df = DataSource(“bar60m_CN_STOCK_A”).read(a,start_date=“2020-03-10”)
df = df[[‘open’,‘high’,‘close’,‘low’]]
data = df.values.tolist()
data.reverse()
a=data[0][1]
i=0
for i in range(len(data)):
if(data[i][1] > a):
break
b=i
m = data[0:b+1]
result=[]
for i in range(0,len(m)):
result=list(set(result).union(set(m[i])))
one = min(result)
n=data[b:]
for i in range(len(n)):
if(n[i][3] <=one):
break
c=i
o=n[0:c+1]
result=[]
for i in range(0,len(o)):
result=list(set(result).union(set(o[i])))
two = max(result)
p=n[c:]
for i in range(len(p)):
if(p[i][1] >=two):
break
d=i
q=p[0:i+1]
result=[]
for i in range(0,len(q)):
result=list(set(result).union(set(q[i])))three = min(result)
r=p[d:]
for i in range(len(r)):
if(r[i][3] <=three):
break
e=i
s=r[0:i+1]
result=[]
for i in range(0,len(s)):
result=list(set(result).union(set(s[i])))four = max(result)
five=min(result)
print(one,two,three,four,five)
if(four-three)*0.585<(two-one) and b>0 and c>0 and d>0 and e>0 and b+c+d+e<1000:
print("True")
else:print("Flase")
- 条件 c1 = l[‘date’]>='2021-06-08’是多余的,因为你的start_date已经是2021-06-10
- 不建议在循环里面调用DataSource,效率低,可以参照如下代码,先读取然后再通过股票来过滤
- 下次分享代码请点击策略编辑页面右上角的策略连接分享按钮,把链接发出来就可以了,不然直接这样拷贝代码格式会乱
def test(instr,df60):
df1 = df60[df60['instrument']==instr]
df = df1[["open","high","close","low"]]
data = df.values.tolist()
data.reverse()
a=data[0][1]
i=0
for i in range(len(data)):
if(data[i][1] > a):
break
b=i
m = data[0:b+1]
result=[]
for i in range(0,len(m)):
result=list(set(result).union(set(m[i])))
one = min(result)
n=data[b:]
for i in range(len(n)):
if(n[i][3] <=one):
break
c=i
o=n[0:c+1]
result=[]
for i in range(0,len(o)):
result=list(set(result).union(set(o[i])))
two = max(result)
p=n[c:]
for i in range(len(p)):
if(p[i][1] >=two):
break
d=i
q=p[0:i+1]
result=[]
for i in range(0,len(q)):
result=list(set(result).union(set(q[i])))
three = min(result)
r=p[d:]
for i in range(len(r)):
if(r[i][3] <=three):
break
e=i
s=r[0:i+1]
result=[]
for i in range(0,len(s)):
result=list(set(result).union(set(s[i])))
four = max(result)
five=min(result)
print(one,two,three,four,five)
if(four-three)*0.585<(two-one) and b>0 and c>0 and d>0 and e>0 and b+c+d+e<1000:
print("True")
else:
print("Flase")
l=DataSource('bar1d_CN_STOCK_A').read(instruments=[],start_date='2021-06-10',end_date=None,fields=['open','close','high','low','amount','adjust_factor'])
l['open+']=l['open']/l['adjust_factor']
l['close+']=l['close']/l['adjust_factor']
l['high+']=l['high']/l['adjust_factor']
l['low+']=l['low']/l['adjust_factor']
c1 = l['date']>='2021-06-08'
c2 = l['amount']>100000000
c3 = l['close+']>4.99
l1 = l.loc[c1&c2&c3,['instrument','date','open+','high+','close+','low','amount']]
xuangu= l1['instrument'].values.tolist()
df60 = DataSource("bar60m_CN_STOCK_A").read(instruments=xuangu,start_date="2021-03-10")
print(df60)
for instr in xuangu:
test(instr,df60)
\