def show_macd(): """ MACD交易策略 :return: """ CBClose = get_CBClose() DIF = ma.ewma_cal(CBClose, 12, 2 / (1 + 12)) - ma.ewma_cal(CBClose, 26, 2 / (1 + 26)) print(DIF.tail(n=3)) DEA = ma.ewma_cal(DIF, 9, 2 / (1 + 9)) print(DEA.tail()) MACD = DIF - DEA print("MACD tail") print(MACD.tail(n=3)) # plt.subplot(211) # plt.plot(DIF['2015'], label='DIF', color='k') # plt.plot(DEA['2015'], label='DEA', color='b', linestyle='dashed') # plt.title("信号线DIF与DEA") # plt.legend() # plt.subplot(212) # plt.bar(left=MACD['2015'].index, height=MACD['2015'], label='MACD', color='r') # plt.legend() macddata = pd.DataFrame() macddata['DIF'] = DIF['2015'] macddata['DEA'] = DEA['2015'] macddata['MACD'] = MACD['2015'] import candle ChinaBank = pd.read_csv(r"./data/ChinaBank.csv") candle.candleLinePlots( ChinaBank['2015'], candleTitle='中国银行2015年日K线图', splitFigures=True, Data=macddata, ylabel='MACD' )
def show_ewma(): Close = get_tsingtao_close() Ema5 = ma.ewma_cal(Close, 5, 0.2) print(Ema5.head()) print(Ema5.tail()) plt.plot(Close[4:], label="Close", color="k") plt.plot(Ema5[4:], label="Ema5", color="g", linestyle="-.") plt.title("青岛啤酒收盘价指数移动平均线") plt.ylim(35, 50) plt.legend()
def trade_macd(): """ 根据macd产生信号交易 :return: """ CBClose = get_CBClose() DIF = ma.ewma_cal(CBClose, 12, 2 / (1 + 12)) - ma.ewma_cal(CBClose, 26, 2 / (1 + 26)) DEA = ma.ewma_cal(DIF, 9, 2 / (1 + 9)) macdSignal = pd.Series(0, index=DIF.index[1:]) for i in range(1, len(DIF)): if all([DIF[i] > DEA[i] > 0.0, DIF[i - 1] < DEA[i - 1]]): macdSignal[i] = 1 elif all([DIF[i] < DEA[i] < 0.0, DIF[i - 1] > DEA[i - 1]]): macdSignal[i] = -1 print(macdSignal.tail()) macdTrade = macdSignal.shift(1) CBRet = CBClose / CBClose.shift(1) - 1 macdRet = (CBRet * macdTrade).dropna() macdWinRate = len(macdRet[macdRet > 0]) / len(macdRet[macdRet != 0]) print(macdWinRate)
def show_chinabank(): CBClose = get_CBClose() print(CBClose.describe()) Close15 = CBClose["2015"] Sma10 = ma.sma_cal(Close15, 10) print(Sma10.tail()) weight = np.array(range(1, 11)) / sum(range(1, 11)) Wma10 = ma.wma_cal(Close15, weight) print(Wma10.tail(n=3)) expo = 2 / (len(Close15) + 1) Ema10 = ma.ewma_cal(Close15, 10, expo) print(Ema10.tail(n=3)) plt.rcParams["font.sans-serif"] = ["SimHei"] plt.plot(Close15[10:], label="Close", color="k") plt.plot(Sma10[10:], label="Sma10", color="r", linestyle="dashed") plt.plot(Wma10[10:], label="Wma10", color="b", linestyle=":") plt.plot(Ema10[10:], label="Ema10", color="G", linestyle="-.") plt.title("中国银行价格均线") plt.ylim(3.5, 5.5) plt.legend() plt.show()
def trade_multi(): cb_close = get_CBClose() cb_sma10 = ma.sma_cal(cb_close, 10) SmaSignal = pd.Series(0, cb_close.index) for i in range(10, len(cb_close)): if all([cb_close[i] > cb_sma10[i], cb_close[i - 1] < cb_sma10[i - 1]]): SmaSignal[i] = 1 elif all([cb_close[i] < cb_sma10[i], cb_close[i - 1] > cb_sma10[i - 1]]): SmaSignal[i] = -1 Ssma5 = ma.sma_cal(cb_close, 5) Lsma30 = ma.sma_cal(cb_close, 30) SLSignal = pd.Series(0, index=Lsma30.index) for i in range(1, len(Lsma30)): if all([Ssma5[i] > Lsma30[i], Ssma5[i - 1] < Lsma30[i - 1]]): SLSignal[i] = 1 elif all([Ssma5[i] < Lsma30[i], Ssma5[i - 1] > Lsma30[i - 1]]): SLSignal[i] = -1 DIF = ma.ewma_cal(cb_close, 12, 2 / (1 + 12)) - ma.ewma_cal(cb_close, 26, 2 / (1 + 26)) DEA = ma.ewma_cal(DIF, 9, 2 / (1 + 9)) macdSignal = pd.Series(0, index=DIF.index[1:]) for i in range(1, len(DIF)): if all([DIF[i] > DEA[i] > 0.0, DIF[i - 1] < DEA[i - 1]]): macdSignal[i] = 1 elif all([DIF[i] < DEA[i] < 0.0, DIF[i - 1] > DEA[i - 1]]): macdSignal[i] = -1 AllSignal = SmaSignal + SLSignal + macdSignal for i in AllSignal.index: if AllSignal[i] > 1: AllSignal[i] = 1 elif AllSignal[i] < -1: AllSignal[i] = -1 else: AllSignal[i] = 0 print(AllSignal[AllSignal == 1]) print(AllSignal[AllSignal == -1]) tradSig = AllSignal.shift(1).dropna() cb_close = cb_close[-len(tradSig):] asset = pd.Series(0.0, index=cb_close.index) cash = pd.Series(0.0, index=cb_close.index) share = pd.Series(0, index=cb_close.index) entry = 3 cash[:entry] = 20000 while entry < len(cb_close): cash[entry] = cash[entry - 1] if all([cb_close[entry - 1] >= cb_close[entry - 2], cb_close[entry - 2] >= cb_close[entry - 3], AllSignal[entry - 1] != -1]): share[entry] = 1000 cash[entry] = cash[entry] - 1000 * cb_close[entry] break entry += 1 i = entry + 1 while i < len(tradSig): cash[i] = cash[i - 1] share[i] = share[i - 1] flag = 1 if tradSig[i] == 1: share[i] = share[i] + 3000 cash[i] = cash[i] - 3000 * cb_close[i] if all([tradSig[i] == -1, share[i] > 0]): share[i] = share[i] - 1000 cash[i] = cash[i] + 1000 * cb_close[i] i += 1 plt.subplot(411) plt.title("2014-2015年上:中国银行均线交易账户") plt.plot(cb_close, color='b') plt.ylabel("Pricce") plt.subplot(412) plt.plot(share, color='b') plt.ylabel("Share") plt.ylim(0, max(share) + 1000) plt.subplot(413) plt.plot(asset, label="asset", color='r') plt.ylabel("Asset") plt.ylim(min(asset) - 5000, max(asset) + 5000) plt.subplot(414) plt.plot(cash, label="cash", color='g') plt.ylabel("Cash") plt.ylim(0, max(cash) + 5000) TradeReturn = (asset[-1] - 20000) / 20000 print(TradeReturn) plt.show()