Signal Moving Average는 트레이딩뷰 오픈 소스로 ‘LuxAlgo‘라는 유저가 공개하였습니다.
[유저 설명 번역]개요
이 스크립트는 이동 평균 크로스오버 시스템에서 ‘시그널’ 라인 으로 사용될 수 있는 이동 평균을 제공합니다. 이 이동 평균은 횡보장에서는 가격과 차이를 보이다가, 추세가 형성되면 일반적인 이동 평균과 비슷한 값으로 수렴합니다.설정
Length: 이동 평균의 기간
Src: 인디케이터의 기준이 되는 입력 값사용 방법
이동 평균 크로스오버 전략에서는 일반적으로 ‘시그널’ 라인을 사용합니다. 이는 비교적 느린 이동 평균으로 전체적인 추세를 파악하는 데 활용됩니다. 이 신호선과 더 빠른 이동 평균을 함께 사용하여, 가격과 신호선의 교차로 인해 발생할 수 있는 거짓 신호(whipsaw trades) 를 걸러낼 수 있습니다.
트레이딩뷰: https://www.tradingview.com/script/L1NqQsyw-Signal-Moving-Average-LuxAlgo/
Signal Moving Average 구현
Signal Moving Average 값과 color를 저장하겠습니다.
import pandas as pd
import ccxt
import pinetopy as pp
import numpy as np
# Binance Futures, BTCUSDT, 1h
bnb = ccxt.binance({'options': { 'defaultType': 'future' }})
ohlcv = bnb.fetch_ohlcv(symbol="BTC/USDT", timeframe="1h", limit=500)
df = pd.DataFrame(ohlcv, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
df['time'] = pp.kst(df['time'])
# TradingView Default Settings
def main(df, length=50):
df_len = len(df)
target = pp.sma(df['close'], length=length)
abs_diff = target.diff().abs()
r2 = df['close'].rolling(length).corr(pd.Series(np.arange(df_len), index=df.index)) ** 2
ma, os = np.zeros(df_len), np.zeros(df_len)
for i in range(1, df_len):
if r2.iloc[i] > 0.5:
os[i] = np.sign(df['close'].iloc[i-1] - target.iloc[i-1])
prev_ma = ma[i-1] if not np.isnan(ma[i-1]) else target.iloc[i]
ma[i] = r2.iloc[i] * target.iloc[i] + (1 - r2.iloc[i]) * prev_ma
else:
os[i], ma[i] = os[i-1], ma[i-1] - abs_diff.iloc[i] * os[i-1]
df['ma'] = ma.round(1)
df['ma_color'] = np.where(os == 1, 'green', 'red')
return df[['time', 'ma', 'ma_color']]
print(main(df))
Check
트레이딩뷰 차트와 비교

