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

且行且记录

点滴记录,行的更远!

 
 
 

日志

 
 

化整为零把策略函数拆成独立的小函数,便于扩展  

2016-05-13 08:45:12|  分类: 方法技巧 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本想用类的方法拆分大函数封装成对象,后来想到Python的函数也可以赋值给变量,
就在Python命令行里验证了一下。结果是可以赋值,可以用索引引用他们。
就把AutoTrade.py小改了下。
# hcper@2016.5.13 v0.8.4 拆策略函数化整为零,便于扩展

# 不交易
def Policy0(stk):
return

# 挂单
def Policy1(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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
# 只买
def Policy2(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 只卖
def Policy3(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 先买后卖
def Policy4(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 先卖后买
def Policy5(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 自由买卖,与挂单区别是盯着价格买卖,这样不用占用资金,占用可交易数量
def Policy6(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 按涨幅自由买卖
def Policy7(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 按上证指数自由买卖
def Policy8(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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

# 半智能交易,就是交易完成后能够自动设置买卖单继续交易
def Policy9(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

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] = round(pl[i]*1.03, 2) #高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] = round(pl[i]*0.97, 2) #低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

# 追买,适用于突破压力时快速买入
def Policy10(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if p0 > pl[i] and pl[i] >= stk['dt']:
if nl[i] > 0 and money > nl[i]*pl[i]*1.01 :
trade(cd, pl[i]*1.01, nl[i]) # 用高1%价追高买入
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

# 追卖,适用于跌破支撑快速卖出
def Policy11(stk):
global money
cd = stk['code'][2:]
pl = stk['p']
nl = stk['n']
p0 = stk['price']
r0 = stk['rate']

if len(pl) == len(nl) and len(nl) > 0:
for i in range( len(nl) ):
if p0 < pl[i] and pl[i] <= stk['zt']:
if nl[i] < 0 and stk['tradecnt'] >= -nl[i] :
trade(cd, pl[i]*0.99, nl[i]) # 低1%快速卖出
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

# 按股票的策略执行相应的策略函数
def AutoTrade(stk):
idx = stk['policy']
if idx >= 0 and idx < len(Policy):
Policy[idx](stk)
else:
# print('[-] 无效策略!')
return

# 策略函数列表,扩展的时候把新策略添加进去
Policy = [
Policy0, Policy1, Policy2, Policy3, Policy4, Policy5,
Policy6, Policy7, Policy8, Policy9, Policy10, Policy11
]
  评论这张
 
阅读(14)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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