3분 매매법

3분 양봉이 나오면 다음 1분 시작시에 매수하고 1분이 끝날 때 매도하는 방법

chart

Github

NoteBook

upbit_quotation.py

업비트 비트코인 1분 차트를 보던 중 3분동안 양봉이 나온다면 (양봉이 3개라면) 다음 1분동안 양봉이 나올 확률이 있지 않을까? 해서 해본 백테스트.

1
2
from upbit_quotation import *
FEE = 0.0005

from 시간부터 to(default=now) 시간까지의 unit 분봉 DataFrame을 얻는다.

1
df_2022 = get_minute_candle_from_to(1, 'KRW-BTC', '2022-01-01T00:00:00')
1
df_2022
market candle_date_time_utc candle_date_time_kst opening_price high_price low_price trade_price timestamp candle_acc_trade_price candle_acc_trade_volume unit
0 KRW-BTC 2022-02-02T19:05:00 2022-02-03T04:05:00 46457000 46482000 46451000 46451000 2022-02-02 19:05:28.113 1.384628e+07 0.297958 1
1 KRW-BTC 2022-02-02T19:04:00 2022-02-03T04:04:00 46451000 46462000 46451000 46462000 2022-02-02 19:04:58.756 2.057519e+07 0.442904 1
2 KRW-BTC 2022-02-02T19:03:00 2022-02-03T04:03:00 46439000 46452000 46421000 46451000 2022-02-02 19:03:59.589 1.549428e+07 0.333660 1
3 KRW-BTC 2022-02-02T19:02:00 2022-02-03T04:02:00 46439000 46439000 46408000 46439000 2022-02-02 19:02:57.967 1.083041e+08 2.333458 1
4 KRW-BTC 2022-02-02T19:01:00 2022-02-03T04:01:00 46415000 46447000 46415000 46439000 2022-02-02 19:01:56.361 3.910157e+07 0.842121 1
... ... ... ... ... ... ... ... ... ... ... ...
47524 KRW-BTC 2021-12-31T15:04:00 2022-01-01T00:04:00 58441000 58445000 58405000 58435000 2021-12-31 15:04:53.917 1.304826e+08 2.233001 1
47525 KRW-BTC 2021-12-31T15:03:00 2022-01-01T00:03:00 58423000 58442000 58422000 58441000 2021-12-31 15:03:59.557 1.007087e+08 1.723417 1
47526 KRW-BTC 2021-12-31T15:02:00 2022-01-01T00:02:00 58480000 58480000 58405000 58441000 2021-12-31 15:02:58.349 9.058170e+07 1.550462 1
47527 KRW-BTC 2021-12-31T15:01:00 2022-01-01T00:01:00 58462000 58485000 58421000 58480000 2021-12-31 15:02:00.101 3.546750e+08 6.065133 1
47528 KRW-BTC 2021-12-31T15:00:00 2022-01-01T00:00:00 58412000 58477000 58405000 58471000 2021-12-31 15:01:00.079 1.751670e+08 2.996578 1

47529 rows × 11 columns

상승이면 1, 하강이면 -1을 기입하는 Column을 새로 만든다

1
df_2022['move'] = np.where(df_2022['opening_price'] < df_2022['trade_price'], 1, -1)
1
df_2022
market candle_date_time_utc candle_date_time_kst opening_price high_price low_price trade_price timestamp candle_acc_trade_price candle_acc_trade_volume unit move
0 KRW-BTC 2022-02-02T19:05:00 2022-02-03T04:05:00 46457000 46482000 46451000 46451000 2022-02-02 19:05:28.113 1.384628e+07 0.297958 1 -1
1 KRW-BTC 2022-02-02T19:04:00 2022-02-03T04:04:00 46451000 46462000 46451000 46462000 2022-02-02 19:04:58.756 2.057519e+07 0.442904 1 1
2 KRW-BTC 2022-02-02T19:03:00 2022-02-03T04:03:00 46439000 46452000 46421000 46451000 2022-02-02 19:03:59.589 1.549428e+07 0.333660 1 1
3 KRW-BTC 2022-02-02T19:02:00 2022-02-03T04:02:00 46439000 46439000 46408000 46439000 2022-02-02 19:02:57.967 1.083041e+08 2.333458 1 -1
4 KRW-BTC 2022-02-02T19:01:00 2022-02-03T04:01:00 46415000 46447000 46415000 46439000 2022-02-02 19:01:56.361 3.910157e+07 0.842121 1 1
... ... ... ... ... ... ... ... ... ... ... ... ...
47524 KRW-BTC 2021-12-31T15:04:00 2022-01-01T00:04:00 58441000 58445000 58405000 58435000 2021-12-31 15:04:53.917 1.304826e+08 2.233001 1 -1
47525 KRW-BTC 2021-12-31T15:03:00 2022-01-01T00:03:00 58423000 58442000 58422000 58441000 2021-12-31 15:03:59.557 1.007087e+08 1.723417 1 1
47526 KRW-BTC 2021-12-31T15:02:00 2022-01-01T00:02:00 58480000 58480000 58405000 58441000 2021-12-31 15:02:58.349 9.058170e+07 1.550462 1 -1
47527 KRW-BTC 2021-12-31T15:01:00 2022-01-01T00:01:00 58462000 58485000 58421000 58480000 2021-12-31 15:02:00.101 3.546750e+08 6.065133 1 1
47528 KRW-BTC 2021-12-31T15:00:00 2022-01-01T00:00:00 58412000 58477000 58405000 58471000 2021-12-31 15:01:00.079 1.751670e+08 2.996578 1 1

47529 rows × 12 columns

iterrow()는 위에서부터 진행하기 때문에 DataFrame을 뒤집는다.

1
df_2022 = df_2022[::-1]
1
df_2022
market candle_date_time_utc candle_date_time_kst opening_price high_price low_price trade_price timestamp candle_acc_trade_price candle_acc_trade_volume unit move
47528 KRW-BTC 2021-12-31T15:00:00 2022-01-01T00:00:00 58412000 58477000 58405000 58471000 2021-12-31 15:01:00.079 1.751670e+08 2.996578 1 1
47527 KRW-BTC 2021-12-31T15:01:00 2022-01-01T00:01:00 58462000 58485000 58421000 58480000 2021-12-31 15:02:00.101 3.546750e+08 6.065133 1 1
47526 KRW-BTC 2021-12-31T15:02:00 2022-01-01T00:02:00 58480000 58480000 58405000 58441000 2021-12-31 15:02:58.349 9.058170e+07 1.550462 1 -1
47525 KRW-BTC 2021-12-31T15:03:00 2022-01-01T00:03:00 58423000 58442000 58422000 58441000 2021-12-31 15:03:59.557 1.007087e+08 1.723417 1 1
47524 KRW-BTC 2021-12-31T15:04:00 2022-01-01T00:04:00 58441000 58445000 58405000 58435000 2021-12-31 15:04:53.917 1.304826e+08 2.233001 1 -1
... ... ... ... ... ... ... ... ... ... ... ... ...
4 KRW-BTC 2022-02-02T19:01:00 2022-02-03T04:01:00 46415000 46447000 46415000 46439000 2022-02-02 19:01:56.361 3.910157e+07 0.842121 1 1
3 KRW-BTC 2022-02-02T19:02:00 2022-02-03T04:02:00 46439000 46439000 46408000 46439000 2022-02-02 19:02:57.967 1.083041e+08 2.333458 1 -1
2 KRW-BTC 2022-02-02T19:03:00 2022-02-03T04:03:00 46439000 46452000 46421000 46451000 2022-02-02 19:03:59.589 1.549428e+07 0.333660 1 1
1 KRW-BTC 2022-02-02T19:04:00 2022-02-03T04:04:00 46451000 46462000 46451000 46462000 2022-02-02 19:04:58.756 2.057519e+07 0.442904 1 1
0 KRW-BTC 2022-02-02T19:05:00 2022-02-03T04:05:00 46457000 46482000 46451000 46451000 2022-02-02 19:05:28.113 1.384628e+07 0.297958 1 -1

47529 rows × 12 columns

예산은 10000으로 설정하고 count3이 되면 다음 row는 무조건 시가에 매수하고 종가에 매도한다, 그리고 count는 0으로 만든 후 continue 한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
budget = 10000
count = 0
rise_count = 0
fall_count = 0
result_budget = budget

for _, member in df_2022.iterrows():
    if count == 3:
        result_budget *= 1 - FEE
        result_budget *= (member['trade_price'] / member['opening_price'])
        result_budget *= 1 - FEE
        if member['move'] == 1:
            rise_count += 1
        else:
            fall_count += 1
        count = 0
        continue
    if member['move'] == 1:
        count += 1
    if member['move'] == -1:
        count = 0

수익률 -93.14%, 무조건 망하니 생각도 하지 말자

1
pd.DataFrame({'year': [2022], 'budget': [result_budget], 'rise_count': [rise_count], 'fall_count': [fall_count], 'earnings rate': [(result_budget / budget) - 1]}).set_index('year')
budget rise_count fall_count earnings rate
year
2022 682.448528 1373 1418 -0.931755

위의 과정을 함수화 하여 다른 연도에도 적용해보자

1
2
df_2021 = get_minute_candle_from_to(1, 'KRW-BTC', start='2021-01-01T00:00:00', end='2022-01-01T00:00:00')
df_2020 = get_minute_candle_from_to(1, 'KRW-BTC', start='2020-01-01T00:00:00', end='2021-01-01T00:00:00')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def m3_strategy_info(df, budget, year):
    df['move'] = np.where(df['opening_price'] < df['trade_price'], 1, -1)
    df = df[::-1]
    count = 0
    rise_count = 0
    fall_count = 0
    result_budget = budget

    for _, member in df.iterrows():
        if count == 3:
            result_budget *= 1 - FEE
            result_budget *= (member['trade_price'] / member['opening_price'])
            result_budget *= 1 - FEE
            if member['move'] == 1:
                rise_count += 1
            else:
                fall_count += 1
            count = 0
            continue
        if member['move'] == 1:
            count += 1
        if member['move'] == -1:
            count = 0
    return pd.DataFrame({'year': [year], 'budget': [result_budget], 'rise_count': [rise_count], 'fall_count': [fall_count], 'earnings rate': [(result_budget / budget) - 1]}).set_index('year')
1
m3_strategy_info(df_2021, 10000, 2021)
budget rise_count fall_count earnings rate
year
2021 3.421880e-10 17191 15832 -1.0
1
m3_strategy_info(df_2020, 10000, 2020)
budget rise_count fall_count earnings rate
year
2020 0.000017 9754 11558 -1.0

각각 수익률 -100% 를 달성하였다. 말도 안되는 전략이었던 것 같다.