| 39 | |
| 40 | #计算修正K线数据 |
| 41 | def Heikin_Ashi(df): |
| 42 | df=df.copy() |
| 43 | #计算修正版K线 |
| 44 | df['ha_close']=(df.close+df.open+df.high+df.low)/4.0 |
| 45 | ha_open=np.zeros(df.shape[0]) |
| 46 | ha_open[0]=df.open[0] |
| 47 | for i in range(1,df.shape[0]): |
| 48 | ha_open[i]=(ha_open[i-1]+df['ha_close'][i-1])/2 |
| 49 | df.insert(1,'ha_open',ha_open) |
| 50 | df['ha_high']=df[['high','ha_open','ha_close']].max(axis=1) |
| 51 | df['ha_low']=df[['low','ha_open','ha_close']].min(axis=1) |
| 52 | old_cols=['ha_open','ha_high','ha_low','ha_close','volume'] |
| 53 | df=df[old_cols] |
| 54 | new_cols=['open','high','low','close','volume'] |
| 55 | df=df.rename(columns=dict(zip(old_cols,new_cols))) |
| 56 | return df |
| 57 | |
| 58 | #修正K线图 |
| 59 | #平均K线图(Heikin-Ashi) |