001 moving average series

      Moving average is one of the most common indicators of technical analysis theory in the application, it is mainly used to confirm, track and determine trends, suggesting that buy and sell signals, unilateral market can better grasp market opportunities and avoid risks . However, the moving average is generally combined with other technical or fundamental indicators to use, especially when the market is in consolidation market, which buy and sell signals occur frequently, easily distorted.

General function name : MA

Code : ta.MA (close, timeperiod = 30 , matype = 0)

      Moving average of indicators comprising: SMA Simple Moving Average, EMA Exponential Moving Average, WMA weighted moving average, DEMA double moving average, TEMA triple exponential moving average, TRIMA triangular moving average, KAMA adaptive Kaufman moving average, MAMA adaptive moving average as MESA, T3 triple exponential moving average.

      Wherein, Close is the closing price, time series, TimePeriod short time, default 30 days index type matype respectively correspond to: 0 = SMA, 1 = EMA, 2 = WMA, 3 = DEMA, 4 = TEMA, 5 = TRIMA, 6 = KAMA, 7 = MAMA, 8 = T3 (Default = SMA)

      Different types of moving average has a respective function call:

  Simple Moving Average SMA: ta.SMA (Close, TimePeriod = 30)
  EMA Exponential Moving Average: ta.EMA (Close, TimePeriod = 30)
  WMA weighted moving average: ta.WMA (Close, TimePeriod = 30)
  the DEMA bis moving average: ta.DEMA (use Close, TimePeriod = 30)
  TEMA triple exponential moving average: ta.TEMA (use Close, TimePeriod = 30)
  TRIMA triangular moving average: ta.TRIMA (use Close, TimePeriod = 30)
  KAMA Kauf Man adaptive moving average: ta.KAMA (Close, TimePeriod = 30)
  MAMA adaptive moving average of MESA: ta.MAMA (Close, TimePeriod = 30)
  T3 triple exponential moving average. ta.T3 (close, timeperiod, vfactor = 0)

# Introduced first packet may be used later 
Import PANDAS AS PD
 Import numpy AS NP
 Import matplotlib.pyplot AS PLT
 Import Talib AS TA
 Import tushare AS TS 

plt.rcParams [ ' font.sans serif- ' ] = [ ' SimHei ' ] 
PLT .rcParams [ ' axes.unicode_minus ' ] = False 

# technical indicators and math functions see included 
# Print (ta.get_functions ()) 
# Print (ta.get_function_groups ()) 


ta_fun = ta.get_function_groups () 
ta_fun.keys ( ) 



"" "Use tushare obtain data on the card index "" " 
DF = ts.get_k_data ( ' SH ' , Start = ' 2000-01-01 ' )
 # At this index is [0,1,2, ...] it is necessary to adjust the date datetime format, and to index 
df.index = pd.to_datetime (df.date) 
DF = df.sort_index () 



"" " Various types of moving average Videos " "" 
types = [ ' the SMA ' , ' EMA ' , ' WMA ' , ' DEMA ' , ' TEMA ' ,' TRIMA ', 'KAMA', 'MAMA', 'T3']
df_ma = pd.DataFrame(df.close)
for i in range(len(types)):
    df_ma[types[i]] = ta.MA(df.close, timeperiod=5, matype=i)
df_ma.tail()

df_ma.loc['2018-08-01':].plot(figsize=(16,6))
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top' ] .Set_color ( ' none ' ) 
plt.title ( ' the Shanghai Composite Index moving averages of various types ' , fontSize = 15 ) 
plt.xlabel ( '' ) 
plt.show () 

# Why here have legend?
View Code

"""画5、30、120、250指数移动平均线"""
N = [5, 30, 120, 250]
for i in N:
    df['ma_'+str(i)] = ta.EMA(df.close, timeperiod=i)
df.tail()

df.loc['2014-01-01':, ['close', 'ma_5', 'ma_30', 'ma_120', 'ma_250']].plot(figsize=(16,6))
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.title('上证指数走势', fontsize=15)
plt.xlabel('')
plt.show()
View Code

 

Guess you like

Origin www.cnblogs.com/wintalau/p/11616277.html