기업 가치 분석/금융데이터 분석과 자동화(파이썬)

[Pandas] 볼린저밴드(Bollinger Band) 매매 부수기 ( with python )

보보트레인 2023. 11. 13. 22:55

사용툴 및 세팅은 이전글 참고

[Pandas] 이동평균선 매매기법 뿌수기(with python) (tistory.com)

1. 코드

def Bollinger(df) :
    index = df.index.astype('str')
    ma20 = df['Close'].rolling(window = 20).mean()
    bol_upper = ma20 + 2 * df['Close'].rolling(window = 20).std()
    bol_down = ma20 - 2 * df['Close'].rolling(window = 20).std()

    fig = plt.figure(figsize = (18,8))
    ax_main = fig.add_subplot(1,1,1)
    def x_date(x,pos):
        try :
            return index[int(x-0.5)][:7]
        except IndexError :
            return ''
    ax_main.xaxis.set_major_locator(ticker.MaxNLocator(8))
    ax_main.set_xlabel('Date', fontsize = 17)
    plt.ylabel('Close Price', fontsize = 17)
    ax_main.plot(index,ma20,label = 'MA20')
    ax_main.plot(index,bol_upper,label = 'bol_upper')
    ax_main.plot(index,bol_down,label = 'bol_down')
    ax_main.set_title('Bollinger Band', fontsize = 23, position = (0.5,1.05))
    plt.legend( loc='upper left', fontsize = 13)
    candlestick2_ohlc(ax_main,df['Open'],df['High'],df['Low'],df['Close'], width = 0.5, colorup = 'orangered',colordown = 'deepskyblue')
    plt.grid(b=True, color='DarkTurquoise', alpha=0.3, linestyle=':', linewidth=2)
    plt.show()
Bollinger(df)

 

<핵심 코드>

    index = df.index.astype('str')
    ma20 = df['Close'].rolling(window = 20).mean()
    bol_upper = ma20 + 2 * df['Close'].rolling(window = 20).std()
    bol_down = ma20 - 2 * df['Close'].rolling(window = 20).std()
 

20 이동평균선을 기준으로 (+ 2*표준편차) & (- 2*표준편차) 의 값을 더하고 뺀 추이를 보여주는 그래프를 그림

 

<매매전략>

-> 볼린저 밴드 하단선을 깨고 하방을 가르킬때 -> 매수

볼린저밴드 상단선을 돌파하고 상방에 위치할때 -> 매도

 

2. 결과

HES

HES 불린저밴드
1차 매수매도 수익률 : 18%
2차매수 매도 수익률 : 17%

 

MPC

MPC 불린저밴드
1차 매수매도 수익률 : 36%
2차매수 매도 수익률 : 22%

 

EOG

EOG 불린저밴드
1차 매수매도 수익률 : 43%

반응형