import pandas as pd
import numpy as np
import talib
import yfinance as yf # For fetching historical price data
# Fetch historical price data for KOSPI 200 futures
ticker = "^KS200" # KOSPI 200 futures symbol
start_date = "2020-01-01"
end_date = "2023-01-01"
data = yf.download(ticker, start=start_date, end=end_date)
# Calculate MACD values
data['macd'], data['signal'], _ = talib.MACD(data['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
# Identify bullish and bearish divergence
data['bullish_divergence'] = np.where((data['Close'] < data['Low']) & (data['macd'] > data['macd'].shift(1)), 1, 0)
data['bearish_divergence'] = np.where((data['Close'] > data['High']) & (data['macd'] < data['macd'].shift(1)), 1, 0)
# Entry and exit signals based on divergence
data['buy_signal'] = np.where(data['bullish_divergence'] == 1, 1, 0)
data['sell_signal'] = np.where(data['bearish_divergence'] == 1, -1, 0)
# Simulate trading
data['position'] = data['buy_signal'] + data['sell_signal']
data['position'] = data['position'].fillna(0).cumsum()
# Print the trading signals and positions
print(data[['Close', 'macd', 'bullish_divergence', 'bearish_divergence', 'buy_signal', 'sell_signal', 'position']])
안녕하세요?
Chat GPT에 의해 파이썬으로 작성된 샘플입니다.
YT에서 사용할 수 있도록 변환 부탁드립니다.
답변 1
예스스탁
예스스탁 답변
2023-08-16 18:54:56
안녕하세요
예스스탁입니다.
올려주신 부분은 내용을 파악할 수 없습니다.
₩구현하고자 하시는 내용을 글로 올리시길 바랍니다.
즐거운 하루되세요
> 흰둥이아빠 님이 쓴 글입니다.
> 제목 : 함수요청
> import pandas as pd
import numpy as np
import talib
import yfinance as yf # For fetching historical price data
# Fetch historical price data for KOSPI 200 futures
ticker = "^KS200" # KOSPI 200 futures symbol
start_date = "2020-01-01"
end_date = "2023-01-01"
data = yf.download(ticker, start=start_date, end=end_date)
# Calculate MACD values
data['macd'], data['signal'], _ = talib.MACD(data['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
# Identify bullish and bearish divergence
data['bullish_divergence'] = np.where((data['Close'] < data['Low']) & (data['macd'] > data['macd'].shift(1)), 1, 0)
data['bearish_divergence'] = np.where((data['Close'] > data['High']) & (data['macd'] < data['macd'].shift(1)), 1, 0)
# Entry and exit signals based on divergence
data['buy_signal'] = np.where(data['bullish_divergence'] == 1, 1, 0)
data['sell_signal'] = np.where(data['bearish_divergence'] == 1, -1, 0)
# Simulate trading
data['position'] = data['buy_signal'] + data['sell_signal']
data['position'] = data['position'].fillna(0).cumsum()
# Print the trading signals and positions
print(data[['Close', 'macd', 'bullish_divergence', 'bearish_divergence', 'buy_signal', 'sell_signal', 'position']])
안녕하세요?
Chat GPT에 의해 파이썬으로 작성된 샘플입니다.
YT에서 사용할 수 있도록 변환 부탁드립니다.