indicators.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:Quant-StockPredictor 作者: echapuis 项目源码 文件源码
def get_indicators (symbols, start_date, end_date, lookback=14):

  # Construct an appropriate DatetimeIndex object.
  dates = pd.date_range(start_date, end_date)

  # Read all the relevant price data (plus SPY) into a DataFrame.
  price = util.get_data(symbols, dates)

  # Add SPY to the symbol list for convenience.
  symbols.append('SPY')


  #Simple Moving Average
  sma = price.rolling(window=lookback,min_periods=lookback).mean()

  smaRatio = sma.copy()
  smaRatio = price / sma


  # Exponentially Moving Average
  ewma = price.ewm(ignore_na=False, span=lookback, min_periods=0, adjust=True).mean()
  ewmaRatio = ewma.copy()
  ewmaRatio = price / ewma

  #Momentum
  momentum = price.copy()
  momentum /= momentum.shift(lookback)
  momentum -= 1
  momentum = momentum.fillna(0)


  #Bollinger Bands
  rolling_std = price.rolling(window=lookback,min_periods=lookback).std()
  top_band = sma + (2 * rolling_std)
  bottom_band = sma - (2 * rolling_std)

  bbp = (price - bottom_band) / (top_band - bottom_band)



  data = pd.concat([price, sma, smaRatio, bbp, ewma, ewmaRatio, momentum], keys=['Price', 'SMA', 'SMA Ratio', 'BBP', 'EMA', 'EMA Ratio', 'Momentum'], axis=1)
  return data
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号