/python/algo_local/Signal.py

https://bitbucket.org/vroomzel/semester2 · Python · 89 lines · 43 code · 18 blank · 28 comment · 11 complexity · 1622abf92df3826bd82cfae426a49505 MD5 · raw file

  1. import numpy as np
  2. import pandas
  3. def make(data_frame, symbols):
  4. return Signal(data_frame, symbols)
  5. class Signal(object):
  6. '''
  7. Class Signal
  8. '''
  9. def __init__(self, data_frame, symbols):
  10. '''
  11. Constructor
  12. '''
  13. self.data = data_frame
  14. self.symbols = symbols
  15. self.signal = []
  16. def _liquidity(self,window):
  17. '''
  18. calculate liquidity within a given window
  19. return the most liquid stock, set their corresponding position as 1
  20. '''
  21. liquidity = np.array([])
  22. #loop through the symbols
  23. for symbol in self.symbols:
  24. volumn = self.data[symbol]["vol"][-window:-1]
  25. price = self.data[symbol]["cp"][-window:-1]
  26. liquidity = np.append(liquidity, sum(volumn*price))
  27. sorted_liquidity = np.sort(liquidity)
  28. liquidity[liquidity < sorted_liquidity[-6]] = 0
  29. return liquidity
  30. def gen_idx(self, window, most_liqd_len, least_liqd_len):
  31. '''
  32. return two list of symbols,
  33. one contains the most liquid stock symbols,
  34. the other contains the least liquid stock symbols.
  35. window: the look back window to determin liquidity
  36. most_liad_len: the number of tops you want
  37. least_liqd_len: the number of bottoms you want
  38. '''
  39. liquidity = self._liquidity(window)
  40. sort_liquidity = np.sort(liquidity) #sort the liquidity
  41. dummy_index = np.array(range(0,len(self.symbols))) #get the index of the least liquid stocks
  42. self.least_liquid = [self.symbols[i] for i in dummy_index[liquidity < sort_liquidity[least_liqd_len]]]
  43. #get a list of the symbols that are least liquid
  44. self.most_liquid = [self.symbols[i] for i in dummy_index[liquidity > sort_liquidity[- most_liqd_len - 1]]]
  45. #get a list of the symbols that are most liquid
  46. def gen_signal(self, window):
  47. '''
  48. dummy rule right now
  49. '''
  50. signal = {};
  51. most_liquid_index_ts = None
  52. #print self.most_liquid
  53. #get average direction of the most liquid stock
  54. for symbol in self.most_liquid:
  55. if most_liquid_index_ts is None:
  56. most_liquid_index_ts = self.data[symbol]["cp"][-2* window:-1]
  57. else:
  58. most_liquid_index_ts = most_liquid_index_ts + self.data[symbol]["cp"][-2* window:-1]
  59. #calculate z-score
  60. z_score = (most_liquid_index_ts -pandas.rolling_mean(most_liquid_index_ts,10).shift(1))/pandas.rolling_std(most_liquid_index_ts,10).shift(1)
  61. if z_score[-1] > 0.1 :
  62. for symbol in self.least_liquid:
  63. signal[symbol] = 1.
  64. elif z_score[-1] < -0.1:
  65. for symbol in self.least_liquid:
  66. signal[symbol] = -1.
  67. vector_signal = np.zeros(len(self.symbols))
  68. for symbol in signal.keys():
  69. vector_signal[self.symbols.index(symbol)] = signal[symbol]
  70. return vector_signal