
Backtrader의 trader_stochf 함수는 스토치스트래잉을 계산하는 함수로, 스토치스트래잉의 %D와 %K를 계산하는 함수입니다.
fastk_period는 스토치스트래잉의 %K를 계산할 때 사용하는 기간입니다. 기본값은 5입니다. fastk_period를 9로 설정하면, 스토치스트래잉의 %K가 더 느리게 반응하게 됩니다. 예를 들어, 스토치스트래잉의 %K가 이전에 상승했는데, fastk_period를 9로 설정하면 %K가 더 오래 상승한 후에 하락할 가능성이 있습니다.
fastd_period는 스토치스트래잉의 %D를 계산할 때 사용하는 기간입니다. 기본값은 3입니다. fastd_period를 3으로 설정하면, 스토치스트래잉의 %D가 더 빠르게 반응하게 됩니다. 예를 들어, 스토치스트래잉의 %D가 이전에 상승했는데, fastd_period를 3으로 설정하면 %D가 더 빠르게 상승할 가능성이 있습니다.
fastk_period와 fastd_period의 값을 변경하면, 스토치스트래잉의 %K와 %D가 달라집니다. fastk_period를 9로 설정하고, fastd_period를 3으로 설정한 경우, 스토치스트래잉의 %K는 더 느리게 반응하고, %D는 더 빠르게 반응하게 됩니다.
예를 들어, 다음과 같은 코드를 작성할 수 있습니다.
#hostingforum.kr
python
import backtrader as bt
class StochF(bt.Strategy):
params = (('fastk_period', 9), ('fastd_period', 3),)
def __init__(self):
stochf = bt.ind.Stochastic(self.data.close,
self.p.fastk_period,
self.p.fastd_period,
self.data.close)
self.fastk = stochf.fastk
self.fastd = stochf.fastd
def next(self):
if self.fastk > 80:
print('Overbought!')
elif self.fastk < 20:
print('Oversold!')
cerebro = bt.Cerebro()
cerebro.addstrategy(StochF)
cerebro.run()
이 코드는 Stochastic Oscillator를 사용하여 Overbought와 Oversold를 판단합니다. fastk_period를 9로 설정하고, fastd_period를 3으로 설정하여 스토치스트래잉의 %K와 %D가 달라집니다.
2025-06-18 01:29