注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

且行且记录

点滴记录,行的更远!

 
 
 

日志

 
 

昨天大概把自动交易的py脚本写好了  

2016-03-28 15:28:38|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
# 所有要交易的股票列表
# 状态,策略,总量,可用,代码,名称,价格,涨幅,涨停价,跌停价,价格列表,数量列表
# 状态:0 停牌,1 可交易。初始化时自动设定
# 策略:0 不交易,1 挂单,2 只买,3 只卖,4 先买后卖,5 先卖后买,6 自由买卖, 7 按涨幅自由买卖,8 按上证指数自由买卖,9 半智能交易
stkset = []

'''
stkset = [
{'state':0 , 'policy':6 , 'count':800, 'tradecnt':100, 'code':'601010', 'name':'xxxx', 'price':1.00 , 'rate':0.01 , 'zt':2.00 , 'dt':0.01, 'p':[5.89, 5.6, 5.99,6.01,5.7], 'n':[100,-200,300,100,-100]},
{'state':0 , 'policy':0 , 'count':800, 'tradecnt':200, 'code':'002100', 'name':'xxxx', 'price':1.00 , 'rate':0.01 , 'zt':2.00 , 'dt':0.01, 'p':[3000, 2800, 3000], 'n':[100,-200,300]},
{'state':0 , 'policy':0 , 'count':800, 'tradecnt':800, 'code':'000782', 'name':'xxxx', 'price':1.00 , 'rate':0.01 , 'zt':2.00 , 'dt':0.01, 'p':[], 'n':[]}
]
'''

最主要的代码就是下面了,只粗测了一下。
# 自动交易,按股票的策略执行相应的预设买卖单
def AutoTrade(stk):
global money

cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

if stk['policy'] == 0:
# 不交易
return

elif stk['policy'] == 1:
# 挂单
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if pl[i] > 0 and pl[i] <= stk['zt'] and pl[i] >= stk['dt']:
if nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
#stk['count'] += nl[i] #买单不一定成交
money -= nl[i]*pl[i]*1.01 #保险起见
pl[i] = 0.0
nl[i] = 0
if nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
#stk['count'] -= -nl[i] #卖单不一定成交
#money += round(-nl[i]*pl[i], 2) #卖单不一定成交
stk['tradecnt'] -= -nl[i]
pl[i] = 0.0
nl[i] = 0
if nl[i] == 0 :
continue
# 挂单结束不再交易
stk['policy'] = 0

elif stk['policy'] == 2:
# 只买
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if p0 <= pl[i] and p0 > 0 and pl[i] <= stk['zt']:
if nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
stk['count'] += nl[i]
money -= round(nl[i]*pl[i]*1.01, 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] <= 0 :
continue
# 直到没有买单
for i in range( len(nl) ):
if nl[i] > 0:
return

# 买单结束不再交易
stk['policy'] = 0
elif stk['policy'] == 3:
# 只卖
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if p0 >= pl[i] and pl[i] > 0 and pl[i] >= stk['dt']:
if nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
stk['count'] -= -nl[i]
stk['tradecnt'] -= -nl[i]
money += round(-nl[i]*pl[i], 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] >= 0 :
continue
# 直到没有卖单
for i in range( len(nl) ):
if nl[i] < 0:
return

# 卖单结束不再交易
stk['policy'] = 0

elif stk['policy'] == 4:
# 先买后卖
if len(pl) == len(nl) and len(nl) > 0:
# 先买单
for i in range( len(nl) ):
if p0 <= pl[i] and p0 > 0 and pl[i] <= stk['zt']:
if nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
stk['count'] += nl[i]
money -= round(nl[i]*pl[i]*1.01, 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] <= 0 :
continue
# 直到没有买单
for i in range( len(nl) ):
if nl[i] > 0:
return
# 后卖单
for i in range( len(nl) ):
if p0 >= pl[i] and pl[i] > 0 and pl[i] >= stk['dt']:
if nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
stk['count'] -= -nl[i]
stk['tradecnt'] -= -nl[i]
money += round(-nl[i]*pl[i], 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] >= 0 :
continue
# 直到没有卖单
for i in range( len(nl) ):
if nl[i] < 0:
return

# 卖单结束不再交易
stk['policy'] = 0

elif stk['policy'] == 5:
# 先卖后买
if len(pl) == len(nl) and len(nl) > 0:
# 先卖单
for i in range( len(nl) ):
if p0 >= pl[i] and pl[i] > 0 and pl[i] >= stk['dt']:
if nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
stk['count'] -= -nl[i]
stk['tradecnt'] -= -nl[i]
money += round(-nl[i]*pl[i], 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] >= 0 :
continue
# 直到没有卖单
for i in range( len(nl) ):
if nl[i] < 0:
return
# 后买单
for i in range( len(nl) ):
if p0 <= pl[i] and p0 > 0 and pl[i] <= stk['zt']:
if nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
stk['count'] += nl[i]
money -= round(nl[i]*pl[i]*1.01, 2)
pl[i] = 0.0
nl[i] = 0
if nl[i] <= 0 :
continue
# 直到没有买单
for i in range( len(nl) ):
if nl[i] > 0:
return

# 买单结束不再交易
stk['policy'] = 0

elif stk['policy'] == 6:
# 自由买卖,与挂单区别是盯着价格买卖,这样不用占用资金,占用可交易数量
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if pl[i] > 0 and pl[i] <= stk['zt'] and pl[i] >= stk['dt']:
if p0 <= pl[i] and nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
stk['count'] += nl[i]
money -= nl[i]*pl[i]*1.01
pl[i] = 0.0
nl[i] = 0
if p0 >= pl[i] and nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
stk['count'] -= -nl[i]
money += round(-nl[i]*pl[i], 2)
stk['tradecnt'] -= -nl[i]
pl[i] = 0.0
nl[i] = 0
if nl[i] == 0 :
continue

# 直到没有买卖单
for i in range( len(nl) ):
if nl[i] != 0:
return

# 自由交易结束不再交易
stk['policy'] = 0

elif stk['policy'] == 7:
# 按涨幅自由买卖
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if pl[i] <= 10.00 and pl[i] >= -10.00: #涨幅单位为%
if r0 <= pl[i] and nl[i] > 0 and money > nl[i]*p0*1.01 :
trade(cd, p0, nl[i])
stk['count'] += nl[i]
money -= nl[i]*p0*1.01
pl[i] = 0.0
nl[i] = 0
if r0 >= pl[i] and nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, p0, nl[i])
stk['count'] -= -nl[i]
money += round(-nl[i]*p0, 2)
stk['tradecnt'] -= -nl[i]
pl[i] = 0.0
nl[i] = 0
if nl[i] == 0 :
continue

# 直到没有买卖单
for i in range( len(nl) ):
if nl[i] != 0:
return

# 自由交易结束不再交易
stk['policy'] = 0

elif stk['policy'] == 8:
# 按上证指数自由买卖
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if pl[i] > 1000.00: #大于一千的价格认为是指数
if a0 <= pl[i] and nl[i] > 0 and money > nl[i]*p0*1.01 :
trade(cd, p0, nl[i])
stk['count'] += nl[i]
money -= nl[i]*p0*1.01
pl[i] = 0.0
nl[i] = 0
if a0 >= pl[i] and nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, p0, nl[i])
stk['count'] -= -nl[i]
money += round(-nl[i]*p0, 2)
stk['tradecnt'] -= -nl[i]
pl[i] = 0.0
nl[i] = 0
if nl[i] == 0 :
continue

# 直到没有买卖单
for i in range( len(nl) ):
if nl[i] != 0:
return

# 自由交易结束不再交易
stk['policy'] = 0

elif stk['policy'] == 9:
# 半智能交易,就是交易完成后能够自动设置买卖单继续交易
if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if pl[i] > 0 and pl[i] <= stk['zt'] and pl[i] >= stk['dt']:
if p0 <= pl[i] and nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i], nl[i])
stk['count'] += nl[i]
money -= nl[i]*pl[i]*1.01
pl[i] *= 1.03 #高3个点卖出
nl[i] = -nl[i] #设卖单
continue
if p0 >= pl[i] and nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i], nl[i])
stk['count'] -= -nl[i]
money += round(-nl[i]*pl[i], 2)
stk['tradecnt'] -= -nl[i]
pl[i] *= 0.97 #低3个点再买入
nl[i] = -nl[i] #设买单
continue
if nl[i] == 0 :
continue

# 直到没有买卖单
for i in range( len(nl) ):
if nl[i] != 0:
return

# 自由交易结束不再交易
stk['policy'] = 0

else:
# print('[-] 无效策略!')
return



  评论这张
 
阅读(13)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017