기본 콘텐츠로 건너뛰기

pandas_ta를 적용한 통계적 인덱스 지표

[stock] 변동성 돌파 전략

변동성 돌파 전략

시가에 매수한 주를 거래일의 현재가가 "시가 + 전일변동폭×0.5를 돌파하는 경우 매도하는 전략

전일변동폭: 전일 고가 - 전일 저가

import numpy as np 
import pandas as pd 
import yfinance as yf
st=pd.Timestamp(2024, 9,20)
et=pd.Timestamp(2024, 9,24)
d=yf.download("005930.KS", start=st, end=et)
d
Open High Low Close Adj Close Volume
Date
2024-09-20 63800.0 64700.0 63000.0 63000.0 63000.0 32746056
2024-09-23 62300.0 63500.0 62200.0 62300.0 62300.0 21242594
range_pre=d.High[0]-d.Low[0]
range_pre
1700.0
op_present=d.Open[1]
op_present
62300.0
sell=op_present+range_pre*0.5
sell
sell=op_present+range_pre*0.5
sell
profit=(sell-op_present)/op_present*100
profit.round(1)
1.4

위 전략에 의한 매매를 한달 수행한 과정을 기록해 보면 다음과 같습니다. 이를 위해 위 계산과정을 다음의 함수 volatilitStrategy()로 작성하였습니다.

def volatilitStrategy(da, base=0.5):
    rng=da.High[0]-da.Low[0]
    sell=da.Open[1]+rng*base
    if(sell >= da.Low[1]) and (sell <= da.High[1]):
        profit=(sell-da.Open[1])/da.Open[1]*100
    else:
        profit=np.nan
    return([sell, profit])
st=pd.Timestamp(2024, 8,24)
et=pd.Timestamp(2024, 9,24)
d=yf.download("005930.KS", start=st, end=et)
d
Open High Low Close Adj Close Volume
Date
2024-08-26 78100.0 78200.0 76000.0 76100.0 76100.0 15655938
2024-08-27 75700.0 76500.0 75600.0 75800.0 75800.0 11130145
2024-09-20 63800.0 64700.0 63000.0 63000.0 63000.0 32746056
2024-09-23 62300.0 63500.0 62200.0 62400.0 62400.0 22354828
n=d.shape[0]
re=pd.DataFrame()
for i in range(2, n+1):
    re1=volatilitStrategy(d.iloc[(i-2):i, :])
    re=pd.concat([re,pd.DataFrame(re1).T])
re.index=d.index[1:]
re.columns=["profit", "trade"]
re
profit trade
Date
2024-08-27 76800.0 NaN
2024-08-28 76250.0 0.593668
2024-08-29 74100.0 0.679348
2024-08-30 75000.0 0.806452
2024-09-02 74950.0 NaN
2024-09-03 74700.0 NaN
2024-09-04 70700.0 1.289398
2024-09-05 70750.0 0.927247
2024-09-06 70200.0 NaN
2024-09-09 67750.0 1.270553
2024-09-10 67800.0 NaN
2024-09-11 65750.0 NaN
2024-09-12 66650.0 NaN
2024-09-13 65700.0 NaN
2024-09-19 64600.0 NaN
2024-09-20 64900.0 NaN
2024-09-23 63150.0 1.364366

댓글