2014年10月5日 星期日 晴

10月3号,我有写MACD金叉和死叉的计算程序,比较简单,算是写好了。但 是写庄家抬轿指标的时候,没有找到WINNER函数是怎么实现的,进行不下去了。今天再实现一下二次金叉和二次死叉,这个文章系列就结束了。

我写的代码如下,觉得写的不咋样,如有出入请指正:

[code]def main(): closeList = [] dateList = [] amountList = [] sh1dayPath = ‘D:\Program Files\new_zszq\vipdoc\sh\lday\’ f601628 = ‘sh601628.day’ with open("%s%s" % (sh1dayPath ,f601628),‘rb’) as f: text = f.read() start = 0 total_length = len(text) while start < total_length: mydate,price_open,high,low,close,amount,vol,reservation = struct.unpack(“lllllfll”,text[start:start+32]) #print mydate,price_open,high,low,close,amount,vol,reservation closeList.append(close / 100.0) amountList.append(amount / 100.0) dateList.append(str(mydate)) start += 32

#12日EMA,快速移动平均线,即EMA1 ema12List = getEMAList(closeList,12) print ema12List #26日EMA,慢速移动平均线,即EMA2 ema26List = getEMAList(closeList,26) print ema26List #计算快慢速线间的离差值(DIF),DIF = 今日EMA(12)-今日EMA(26) difList = [x - y for x,y in zip(ema12List,ema26List)] print difList #DEA/DEM,计算DIF的9日EMA deaList = getEMAList(difList,9) print deaList #BAR柱状线,其公式为:BAR=2×(DIF-DEA) macdList = [2 * (x - y) for x,y in zip(difList,deaList)] print macdList

goldList = [] deadList = [] goldList2 = [] deadList2 = []

#短期内(8或13个交易日内)连续发生两次金叉或者死叉 p = 0 q = 0 n = 8 #13 for loop,dif in enumerate(difList): #计算二次金叉 if dif > deaList[loop]: if difList[loop - 1] <= deaList[loop -1]: if p: goldList2.append(dateList[loop - 1]) p = 0 else: p = 1 if p > 0: p +=1 if p > n: p = 0 #计算二次死叉 if dif < deaList[loop]: if difList[loop - 1] >= deaList[loop -1]: if p: deadList2.append(dateList[loop - 1]) q = 0 else: q = 1 if q > 0: q +=1 if q > n: q = 0 continue #DIF、DEA均为正,DIF向上突破DEA,买入信号参考 if dif > 0.0 and deaList[loop] > 0.0: #DIF向上突破DEA if dif > deaList[loop]: if difList[loop - 1] <= deaList[loop -1]: goldList.append(dateList[loop - 1]) #DIF、DEA均为负,DIF向下跌破DEA,卖出信号参考 elif dif < 0.0 and deaList[loop] < 0.0: if dif < deaList[loop]: if difList[loop - 1] >= deaList[loop -1]: deadList.append(dateList[loop - 1]) print ‘,’.join(goldList) print ‘,’.join(deadList) print ‘,’.join(goldList2) print ‘,’.join(deadList2)[/code]