[关闭]
@Channelchan 2017-03-22T19:16:16.000000Z 字数 3761 阅读 21240

择时买卖

未分类


  1. Second Order Breakout(Confirmation)
    突破20天最高,同时是在40天内第一次破20天新高。
  1. import numpy as np
  2. import talib as ta
  3. from fxdayu_data.data import MongoHandler, StockData
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. mh = MongoHandler(db='Global_index')
  7. hs300 = mh.read('hs300.D')
  1. x = np.array(range(10, 81, 10))
  2. def Second_Breakout(s):
  3. high = ta.MAX(hs300.high.values, s)
  4. low = ta.MIN(hs300.low.values, s)
  5. return {s: high, -s: low}
  6. def day_range(data):
  7. low = 0
  8. high = 0
  9. is_high = 1
  10. is_low = 1
  11. for i in x:
  12. if is_low:
  13. if data['low'] <= data[-i]:
  14. low = -i
  15. else:
  16. is_low = 0
  17. if is_high == 0:
  18. return low
  19. if is_high:
  20. if data['high'] >= data[i]:
  21. high = i
  22. else:
  23. is_high = 0
  24. if is_low == 0:
  25. return high
  26. if is_high:
  27. return high
  28. elif is_low:
  29. return low
  30. if __name__ == '__main__':
  31. dct = {'high': hs300.high, 'low': hs300.low}
  32. for i in x:
  33. dct.update(Second_Breakout(i))
  34. df = pd.DataFrame(dct).dropna()
  35. BreakDay = []
  36. for row in df.iterrows():
  37. BreakDay.append(day_range(row[1]))
  38. df['BreakDay'] = BreakDay
  39. fig,(ax,ax1) = plt.subplots(2,1, sharex=True)
  40. ax.plot(hs300.close)
  41. ax1.bar(df.index, df.BreakDay)
  42. plt.show()

1

  1. MACD Histogram Replacement(Accelarating)
    加速度短期均线在长期上金叉确认时进场,加速度短期均线在长期下方死叉确认时出场。
  1. macd = ta.abstract.MACD(hs)
  2. macd = macd.dropna()
  3. macd['sma_hist'] = ta.abstract.MA(macd, 40, price='macdhist').dropna()
  4. macd['lma_hist'] = ta.abstract.MA(macd, 60, price='macdhist').dropna()
  5. hist = macd.iterrows()
  6. t0,d0 = next(hist)
  7. x_b = []
  8. y_b = []
  9. x_s = []
  10. y_s = []
  11. pos = 0
  12. for t1,d1 in hist:
  13. if pos == 0 and d1.sma_hist > d1.lma_hist and d0.sma_hist < d0.lma_hist:
  14. x_b.append(t1)
  15. y_b.append(d1.sma_hist)
  16. pos = 1
  17. elif pos == 1 and d1.sma_hist < d1.lma_hist and d0.sma_hist > d0.lma_hist:
  18. x_s.append(t1)
  19. y_s.append(d1.sma_hist)
  20. pos = 0
  21. t0, d0 = t1, d1
  22. import matplotlib as mpl
  23. mpl.rc('xtick', labelsize=8)
  24. fig,(ax, ax1) = plt.subplots(2,1,sharex=True)
  25. ax.plot(hs['close'])
  26. ax1.plot(macd.sma_hist, 'y')
  27. ax1.plot(macd.lma_hist, 'b')
  28. ax1.scatter(x_b, y_b, c='r', marker='^', linewidths=3)
  29. ax1.scatter(x_s, y_s, c='g', marker='v', linewidths=3)
  30. plt.show()

2
3. Divergence Index(Divergence)
长期动能上涨,短期动能下跌。


Enter when Divergence Index falls below –10 and mom40>0

  1. import talib as ta
  2. from fxdayu_data.data import MongoHandler
  3. import matplotlib.pyplot as plt
  4. mh = MongoHandler(db='Global_index')
  5. hs300 = mh.read('hs300.D')
  6. hs300['mom10'] = ta.abstract.MOM(hs300, 10, price='close')
  7. hs300['mom40'] = ta.abstract.MOM(hs300, 40, price='close')
  8. hs300['price_change'] = hs300.close.diff()
  9. hs300['Var40'] = ta.abstract.VAR(hs300, 40, price='price_change')
  10. hs300 = hs300.dropna()
  11. Div_index = hs300.mom10*hs300.mom40/hs300.Var40
  12. plt.subplot(3,1,1)
  13. plt.plot(hs300.close)
  14. plt.subplot(3,1,2)
  15. plt.plot(Div_index)
  16. plt.hlines(-10, hs300.index[0], hs300.index[-1], linestyles='dashed', alpha=0.5)
  17. plt.subplot(3,1,3)
  18. plt.plot(hs300.mom40)
  19. plt.hlines(0, hs300.index[0], hs300.index[-1], linestyles='dashed', alpha=0.5)
  20. plt.show()

1

  1. Normalized Envelope Indicator(Ranking)
    用排序来确定阻力与支撑位。
  1. import talib as ta
  2. from fxdayu_data.data import MongoHandler
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. mh = MongoHandler(db='Global_index')
  6. hs300 = mh.read('hs300.D')
  7. A = 25
  8. hs300['MA50'] = ta.abstract.MA(hs300,50)
  9. hs300['price_change'] = hs300.close.diff()
  10. hs300['NEI'] = A * (hs300.close-hs300.MA50)/ta.abstract.STDDEV(hs300, 50, price='price_change')
  11. hs300 = hs300.dropna()
  12. def rank(df, price='NEI', gap=50, rank1=10, rank2=40):
  13. s = df[price]
  14. rank1 -= 1
  15. rank2 -= 1
  16. def resort(index):
  17. series = s.iloc[index-gap:index]
  18. value = sorted(series.values)
  19. return series.index[-1], value[rank1], value[rank2]
  20. df[['lowerband', 'upperband']] = pd.DataFrame(map(resort, range(gap, len(s)+1))).set_index(0)
  21. return df
  22. hs300 = rank(hs300)
  23. middleband = ta.abstract.MA(hs300, timeperiod=50)
  24. upperband = middleband + hs300['upperband']
  25. lowerband = middleband + hs300['lowerband']
  26. data_B = pd.concat([middleband, upperband , lowerband], axis=1)
  27. data_B.columns = ['middleband', 'upperband', 'lowerband']
  28. print(data_B)
  29. plt.plot(hs300['close'])
  30. plt.plot(data_B['upperband'], 'g')
  31. plt.plot(data_B['lowerband'], 'r')
  32. plt.show()

4

  1. Volume Reversal Strategy(Comparison)
    用成交量减少作为反转信号。
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注