๐Ÿ“Š ๋ณผ๋ฆฐ์ €๋ฐด๋“œ์Šคํ€ด์ฆˆ CODE


๋น„ํŠธ์ฝ”์ธ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ๋ณผ๋ฆฐ์ € ์Šคํ€ด์ฆˆ ์ „๋žต์„ ์ ์šฉํ•˜๊ณ , ์„ฑ๊ณผ๋ฅผ ๋ถ„์„


โœ… ๊ธฐ๋Šฅ:

  • ๋ณผ๋ฆฐ์ € ๋ฐด๋“œ(20, 2) ๊ณ„์‚ฐ
  • ์Šคํ€ด์ฆˆ ๋ฐœ์ƒ ํƒ์ง€ (๋ฐด๋“œ ํญ์ด ์ผ์ • ์ž„๊ณ„๊ฐ’ ์ดํ•˜)
  • ๊ฐ€๊ฒฉ์ด ์ƒ๋‹จ/ํ•˜๋‹จ ๋ŒํŒŒ ์‹œ ๋งค๋งค
  • ์„ฑ๊ณผ ๋ถ„์„ (์ˆ˜์ต๋ฅ , ์Šน๋ฅ , ์ตœ๋Œ€ ์†์‹ค ๋“ฑ)

๐Ÿ”น ์ฝ”๋“œ ์‹คํ–‰ ์ˆœ์„œ

  1. ๋น„ํŠธ์ฝ”์ธ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ (yfinance ์‚ฌ์šฉ)
  2. ๋ณผ๋ฆฐ์ € ๋ฐด๋“œ ๊ณ„์‚ฐ
  3. ์Šคํ€ด์ฆˆ ๋ฐœ์ƒ ํ™•์ธ ๋ฐ ์ง„์ž… ์กฐ๊ฑด ์„ค์ •
  4. ์ „๋žต ๋ฐฑํ…Œ์ŠคํŠธ
  5. ์„ฑ๊ณผ ๋ถ„์„ ๋ฐ ์‹œ๊ฐํ™”

๐Ÿ“Œ Python ์ฝ”๋“œ

import pandas as pd
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt

# 1๏ธโƒฃ ๋น„ํŠธ์ฝ”์ธ ๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ
symbol = "BTC-USD"
data = yf.download(symbol, start="2022-01-01", end="2025-01-01")

# 2๏ธโƒฃ ๋ณผ๋ฆฐ์ € ๋ฐด๋“œ ๊ณ„์‚ฐ (20์ผ ์ด๋™ํ‰๊ท  + ํ‘œ์ค€ํŽธ์ฐจ)
window = 20
std_dev = 2
data['SMA'] = data['Close'].rolling(window).mean()
data['STD'] = data['Close'].rolling(window).std()
data['Upper'] = data['SMA'] + (data['STD'] * std_dev)
data['Lower'] = data['SMA'] - (data['STD'] * std_dev)
data['Band_Width'] = data['Upper'] - data['Lower']

# 3๏ธโƒฃ ์Šคํ€ด์ฆˆ ๋ฐœ์ƒ ํƒ์ง€ (๋ฐด๋“œ ํญ์ด ์ผ์ • ์ž„๊ณ„๊ฐ’ ์ดํ•˜)
squeeze_threshold = data['Band_Width'].rolling(window).mean() * 0.8
data['Squeeze'] = data['Band_Width'] < squeeze_threshold

# 4๏ธโƒฃ ๋งค๋งค ์‹ ํ˜ธ ์ƒ์„ฑ (์Šคํ€ด์ฆˆ ํ›„ ๋ŒํŒŒ ์—ฌ๋ถ€ ํ™•์ธ)
data['Long_Entry'] = (data['Squeeze']) & (data['Close'] > data['Upper'])
data['Short_Entry'] = (data['Squeeze']) & (data['Close'] < data['Lower'])

data['Position'] = 0
data.loc[data['Long_Entry'], 'Position'] = 1  # ๋กฑ ํฌ์ง€์…˜
\data.loc[data['Short_Entry'], 'Position'] = -1  # ์ˆ ํฌ์ง€์…˜

data['Position'] = data['Position'].shift(1).fillna(0)  # ์‹ ํ˜ธ ๋‹ค์Œ๋‚  ์ง„์ž…

# 5๏ธโƒฃ ๋ฐฑํ…Œ์ŠคํŠธ: ์ผ๋ณ„ ์ˆ˜์ต๋ฅ  ๊ณ„์‚ฐ
data['Returns'] = data['Close'].pct_change()
data['Strategy'] = data['Position'].shift(1) * data['Returns']

cumulative_strategy = (1 + data['Strategy']).cumprod()
cumulative_market = (1 + data['Returns']).cumprod()

# 6๏ธโƒฃ ์„ฑ๊ณผ ์ง€ํ‘œ ๊ณ„์‚ฐ
final_return = cumulative_strategy.iloc[-1] - 1
win_rate = (data['Strategy'] > 0).sum() / (data['Strategy'] != 0).sum()
max_drawdown = (cumulative_strategy / cumulative_strategy.cummax() - 1).min()

print(f"๐Ÿ“ˆ ์ตœ์ข… ์ˆ˜์ต๋ฅ : {final_return:.2%}")
print(f"โœ… ์Šน๋ฅ : {win_rate:.2%}")
print(f"โš ๏ธ ์ตœ๋Œ€ ์†์‹ค(Drawdown): {max_drawdown:.2%}")

# 7๏ธโƒฃ ์„ฑ๊ณผ ์‹œ๊ฐํ™”
plt.figure(figsize=(12, 6))
plt.plot(cumulative_strategy, label='Bollinger Squeeze Strategy', color='blue')
plt.plot(cumulative_market, label='Market (BTC)', color='gray', linestyle='dashed')
plt.legend()
plt.title('Bollinger Squeeze Strategy Performance')
plt.xlabel('Date')
plt.ylabel('Cumulative Returns')
plt.show()

๐Ÿ“Š ๊ฒฐ๊ณผ ๋ถ„์„

  • ์ตœ์ข… ์ˆ˜์ต๋ฅ : ๋ฐฑํ…Œ์ŠคํŠธ ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ์ „๋žต ์„ฑ๊ณผ
  • ์Šน๋ฅ : ๋กฑ/์ˆ ์ง„์ž… ์‹œ ์„ฑ๊ณตํ•œ ๋น„์œจ
  • ์ตœ๋Œ€ ์†์‹ค: ์ „๋žต์˜ ์ตœ๋Œ€ ํ•˜๋ฝํญ (Drawdown)
  • ์‹œ๊ฐํ™”: ์ „๋žต ์ˆ˜์ต๋ฅ  vs. ์‹œ์žฅ ์ˆ˜์ต๋ฅ  ๋น„๊ต

results matching ""

    No results matching ""