# [YouTube] 벡테스팅 - 변동성돌파전략 : Simple

Oct 24th, 2020 (edited)
1,571
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from pykrx import stock
2. from pandas import Series
3. import pandas as pd
4.
5. def 변동성돌파전략(df, k):
6.     df = df.loc[ df['시가'] != 0 ].copy()
7.
8.     df['변동폭'] = df['고가'] - df['저가']
9.     df['목표가'] = df['시가'] + df['변동폭'].shift(1) * k
10.     df['다음날시가'] = df['시가'].shift(-1)
11.     df = df.dropna()
12.
13.     cond = df['고가'] > df['목표가']
14.     수익률 = df.loc[cond, '다음날시가'] / df.loc[cond, '목표가']
15.
16.     if 수익률.empty :
17.         return 0
18.
19.     수익률 = 수익률 - 0.002
20.     기간수익률 = 수익률.cumprod().iloc[-1]
21.
22.     N = (df.index[-1] - df.index[0]).days / 365
23.     CAGR = (기간수익률 ** (1/N)) - 1
24.     return CAGR
25.
26. ticker_list = stock.get_market_ticker_list("20200831")
27.
28. result = [ ]
29. for ticker in ticker_list:
31.     CAGR = 변동성돌파전략(df, 0.4)
32.     result.append(CAGR)
33.
34. s = Series(result, ticker_list)
35. print(s.describe())
36.
37. s.sort_values().plot(kind='bar')
RAW Paste Data