代码拉取完成,页面将自动刷新
同步操作将从 macroan/traderStock-gui 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#coding=utf-8
import public
import volume
import math
class Ma:
__instance = None
def __init__(self):
pass
@classmethod
def getInstance(cls):
if(cls.__instance == None):
cls.__instance = Ma()
return cls.__instance
#计算平均成交量
def set_ma_data(self, df, type=1):
if df is not None and df.shape[0] > 0:
# ========== 计算移动平均线
# 分别计算5日、10日、20日、30日、60日、120、250的移动平均线
ma_list = [5, 10, 20, 30, 60, 120, 250]
for ma in ma_list:
df['MA_' + str(ma)] = df['close'].rolling(window=ma, center=False).mean()
# 计算指数平滑移动平均线EMA
for ma in ma_list:
df['EMA_' + str(ma)] = df['close'].ewm(ignore_na=False,span=ma,min_periods=0,adjust=True).mean()
df['ma_cross10'] = ''
ma_position = df['MA_5'] > df['MA_10']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross10'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross10'] = 'dc'
df['ma_cross20'] = ''
ma_position = df['MA_10'] > df['MA_20']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross20'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross20'] = 'dc'
df['ma_cross30'] = ''
ma_position = df['MA_20'] > df['MA_30']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross30'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross30'] = 'dc'
df['ma_cross60'] = ''
ma_position = df['MA_30'] > df['MA_60']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross60'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross60'] = 'dc'
df['ma_cross120'] = ''
ma_position = df['MA_60'] > df['MA_120']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross120'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross120'] = 'dc'
df['ma_cross250'] = ''
ma_position = df['MA_120'] > df['MA_250']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross250'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross250'] = 'dc'
#df.dropna(how='any', inplace=True)#删除所有空行值的数据
return df
def set_ma_data2(self, df):
df = df.sort_values('date', ascending=True)
df['ma_cross_5_20'] = ''
ma_position = df['MA_5'] > df['MA_20']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross_5_20'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross_5_20'] = 'dc'
df['ma_cross_5_30'] = ''
ma_position = df['MA_5'] > df['MA_30']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross_5_30'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross_5_30'] = 'dc'
df['ma_cross_10_30'] = ''
ma_position = df['MA_10'] > df['MA_30']
df.loc[ma_position[(ma_position == True) & (ma_position.shift() == False)].index, 'ma_cross_10_30'] = 'gc'
df.loc[ma_position[(ma_position == False) & (ma_position.shift() == True)].index, 'ma_cross_10_30'] = 'dc'
df = df.sort_values('date', ascending=False)
return df
#是否是叉 type: D日线 W周线 cycle:10,20,30,60,120,250
def is_cross(self, df, index, cycle=10, crosstype='gc'):
if index < 0 or index > df.shape[0]-1:
return False
cross_list = {10:"ma_cross10", 20:"ma_cross20", 30:"ma_cross30",
60:"ma_cross60", 120:"ma_cross120", 250:"ma_cross250"}
cross_ret = {'gc':df.iloc[index][cross_list[cycle]]=='gc', 'dc':df.iloc[index][cross_list[cycle]]=='dc'}
return cross_ret[crosstype]
def ma_xielv(self, df, target, typestr, direction='down', day=None):
def xielv_data_handle(x, y):
xl = math.atan((x/y-1.0)*100)*180/3.1416
if x == y or x > 0 and y > 0:
return xl
elif x < 0 and y < 0:
return -xl
elif x > 0 and y < 0:
return -xl
else:
return xl
if df is None:
return False
dflen = df.shape[0]
if day != None:
for i in xrange(day):
if target+i+1 < dflen:
if typestr not in ['dif', 'macd', 'dea']:
xl = math.atan((df.iloc[target][typestr]/df.iloc[target+i+1][typestr]-1.0)*100)*180/3.1416
if (direction == 'down' and xl > 0) or (direction == 'up' and xl < 0):
return False
else:
xl = xielv_data_handle(df.iloc[target][typestr], df.iloc[target+i+1][typestr])
if (direction == 'down' and xl > 0) or (direction == 'up' and xl < 0):
return False
else:
if target+1 < dflen:
if type not in ['dif', 'macd', 'dea']:
xl = math.atan((df.iloc[target][typestr]/df.iloc[target+1][typestr]-1)*100*(180/3.1416))
if direction == 'down':
return xl < 0
else:
return xl > 0
else:
xl = xielv_data_handle(df.iloc[target][typestr], df.iloc[target+1][typestr])
if direction == 'down':
return xl < 0
else:
return xl > 0
return True
#ma平均涨幅,成功率
def ma_jinsanjiao_succ(df):
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['ma_cross20'] == 'gc' and i-1 > 0 and \
df.iloc[i-1]['MA_5'] > df.iloc[i-1]['MA_10'] and df.iloc[i-1]['MA_10'] > df.iloc[i-1]['MA_20']: #当前MA10与MA20金叉
target = i
start = 1
count = 0
continue
if start == 1:
if i-target >= 10 or df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_20'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_20'] == 'gc' or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA10金叉
count += 1
if count == 2 and i+2 < dflen:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_20']:
flag = False
if flag:
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#ma平均涨幅,成功率
def ma_jinsanjiao_succ_volume(df):
v = volume.Volume.getInstance()
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['ma_cross20'] == 'gc' and i-1 > 0 and \
df.iloc[i-1]['MA_5'] > df.iloc[i-1]['MA_10'] and df.iloc[i-1]['MA_10'] > df.iloc[i-1]['MA_20'] \
and v.is_fangliang(df, i):
target = i
start = 1
count = 0
continue
if start == 1:
if i-target >= 10 or df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_20'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_20'] == 'gc' or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA10金叉
count += 1
if count == 2 and i+2 < dflen:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_20']:
flag = False
if flag:
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#ma平均涨幅,成功率
def ma_jinsanjiao_succ2(df):
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['ma_cross_10_30'] == 'gc' and i-1 > 0 and \
df.iloc[i-1]['MA_5'] > df.iloc[i-1]['MA_10'] and df.iloc[i-1]['MA_10'] > df.iloc[i-1]['MA_30']: #当前MA10与MA20金叉
target = i
start = 1
count = 0
continue
if start == 1:
if i-target >= 10 or df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_30'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_30'] == 'gc'or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA30金叉
count += 1
if count == 2 and i+2 < dflen:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_30']:
flag = False
if flag:
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#ma平均涨幅,成功率
def ma_jinsanjiao_succ_volume2(df):
v = volume.Volume.getInstance()
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['ma_cross_10_30'] == 'gc' and i-1 > 0 and \
df.iloc[i-1]['MA_5'] > df.iloc[i-1]['MA_10'] and df.iloc[i-1]['MA_10'] > df.iloc[i-1]['MA_30'] \
and v.is_fangliang(df, i): #当前MA10与MA20金叉
target = i
start = 1
count = 0
continue
if start == 1:
if i-target >= 10 or df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_30'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_30'] == 'gc'or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA30金叉
count += 1
if count == 2 and i+2 < dflen:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_30']:
flag = False
if flag:
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#======================================ma选股====================================
def ma_jinsanjiao(df, code):
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
retcode = 0
start = 0
count = 0
for i in xrange(0, 10):
if i == 0 and start == 0 and df.iloc[i]['ma_cross20'] == 'gc': #当前MA10与MA20金叉
start = 1
count = 0
continue
if start == 1:
if df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_20'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_20'] == 'gc'or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA30金叉
count += 1
if count == 2:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_20']:
flag = False
if flag:
retcode = code
return retcode
def ma_jinsanjiao2(df, code):
m_a = Ma.getInstance()
df = m_a.set_ma_data2(df)
retcode = 0
start = 0
count = 0
for i in xrange(0, 10):
if i == 0 and start == 0 and df.iloc[i]['ma_cross_10_30'] == 'gc': #当前MA10与MA20金叉
start = 1
count = 0
continue
if start == 1:
if df.iloc[i]['MA_5'] < df.iloc[i]['MA_10'] or df.iloc[i]['MA_30'] < df.iloc[i]['MA_10']:
start = 0
count = 0
continue
if df.iloc[i]['ma_cross_5_30'] == 'gc'or df.iloc[i]['ma_cross10'] == 'gc': #当前MA5与MA30金叉
count += 1
if count == 2:
flag = True
for j in xrange(2):
if df.iloc[i+j]['MA_5'] > df.iloc[i+j]['MA_10'] or df.iloc[i+j]['MA_10'] > df.iloc[i+j]['MA_30']:
flag = False
if flag:
retcode = code
return retcode
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。