Last active
June 13, 2020 08:23
-
-
Save sooch/80b8eb9473dce36d1845ce36f880952b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from pandas import DataFrame, Series | |
def sma(ohlc: DataFrame, period=9) -> Series: | |
return Series( | |
ohlc['close'].rolling(period).mean(), | |
name=f'SMA {period}', | |
) | |
def ema(ohlc: DataFrame, period=9) -> Series: | |
return Series( | |
ohlc['close'].ewm(span=period).mean(), | |
name=f'EMA {period}', | |
) | |
def wma(ohlc: DataFrame, period=9) -> Series: | |
# WMA = ( 価格 * n + 価格(1) * n-1 + ... 価格(n-1) * 1) / ( n * (n+1) / 2 ) | |
denominator = (period * (period + 1)) / 2 | |
weights = Series(np.arange(1, period + 1)).iloc[::-1] | |
return Series( | |
ohlc['close'].rolling(period, min_periods=period).apply(lambda x: np.sum(weights * x) / denominator, raw=True), | |
name=f'WMA {period}' | |
) | |
def sd(ohlc: DataFrame, period=10, ddof=0) -> float: | |
return ohlc['close'].tail(period).std(ddof=ddof) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment