Guest User

Untitled

a guest
Jun 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. import talib
  2. import statsmodels.api as sm
  3. import pandas as pd
  4.  
  5. def initialize(context):
  6. context.security = symbol('AAPL')
  7. #set_universe(universe.DollarVolumeUniverse(floor_percentile=98.0,ceiling_percentile=100.0))
  8.  
  9. def bar_data(OHLC_type, bars_nr):
  10. bar_data_func = (history((bars_nr + 1), '1d', OHLC_type).iloc[0]).astype('float')
  11. return bar_data_func
  12.  
  13. def handle_data(context, data):
  14. ######### FEEDING DATA
  15. # Bar data
  16. bar_close = 'close_price'
  17. bar_open = 'open_price'
  18. bar_high = 'high'
  19. bar_low = 'low'
  20. bar_volume = 'volume'
  21.  
  22. #current_price = data[context.security].price
  23. price_history = history(bar_count=1, frequency='1d', field='price')
  24. for s in data:
  25. current_price = price_history[s][-1]
  26.  
  27. ######### ADVANCED DATA
  28. # Spread data
  29. yesterday_spread = bar_data(bar_high, 1) - bar_data(bar_low, 1)
  30. up_bar = bar_data(bar_close, 1) > bar_data(bar_close, 2)
  31. down_bar = bar_data(bar_close, 1) < bar_data(bar_close, 2)
  32.  
  33. two_days_ago_spread = bar_data(bar_high, 2) - bar_data(bar_low, 2)
  34. up_bar_2_bars_ago = bar_data(bar_close, 2) > bar_data(bar_close, 3)
  35. down_bar_2_bars_ago = bar_data(bar_close, 2) < bar_data(bar_close, 3)
  36.  
  37. # Average spread
  38. last_i_highs = history(30, '1d', 'high')
  39. last_i_lows = history(30, '1d', 'low')
  40. average_spread = last_i_highs - last_i_lows
  41. average_spread = average_spread.mean()
  42.  
  43. # Spread factors
  44. wide_spread_factor = 1.5
  45. narrow_spread_factor = 0.7
  46. wide_spread_bar = yesterday_spread > (wide_spread_factor * average_spread)
  47. wide_spread_bar_2_bars_ago = two_days_ago_spread > (wide_spread_factor * average_spread)
  48. narrow_spread_bar = yesterday_spread < (narrow_spread_factor * average_spread)
  49.  
  50. # Bar close range
  51. bar_range = yesterday_spread / (bar_data(bar_close, 1) - bar_data(bar_low, 1))
  52. very_high_close_bar = bar_range < 1.35
  53. high_close_bar = bar_range < 2
  54. mid_close_bar = (bar_range < 2.2) & (bar_range > 1.8)
  55. down_close_bar = bar_range > 2
  56.  
  57. # Volume data
  58. volume_history = history(bar_count=100, frequency='1d', field='volume')
  59. volume_series = volume_history[context.security]
  60. volume_average = talib.EMA(volume_series, timeperiod=30)
  61.  
  62. # Volume moving average
  63. last_i_volumes = history(30, '1d', 'volume')
  64. average_volume = last_i_volumes.mean()
  65.  
  66. # Trend definition - Linear Regressions
  67. long_term_history = history(bar_count=40, frequency='1d', field='price')
  68. medium_term_history = history(bar_count=15, frequency='1d', field='price')
  69. short_term_history = history(bar_count=5, frequency='1d', field='price')
  70.  
  71. for S in data:
  72. long_y = long_term_history[S].values/long_term_history[S].mean()
  73. long_x = range(len(long_term_history))
  74. long_a = sm.add_constant(long_x)
  75. long_results = sm.OLS(long_y, long_a).fit()
  76. long_interB, long_slopeM = long_results.params
  77.  
  78. medium_y = medium_term_history[S].values/medium_term_history[S].mean()
  79. medium_x = range(len(medium_term_history))
  80. medium_a = sm.add_constant(medium_x)
  81. medium_results = sm.OLS(medium_y, medium_a).fit()
  82. medium_interB, medium_slopeM = medium_results.params
  83.  
  84. short_y = short_term_history[S].values/short_term_history[S].mean()
  85. short_x = range(len(short_term_history))
  86. short_a = sm.add_constant(short_x)
  87. short_results = sm.OLS(short_y, short_a).fit()
  88. short_interB, short_slopeM = short_results.params
  89.  
  90.  
  91. # Returns true if yesterday's volume is lower than the 2 previous days
  92. two_days_lowest_volume = (bar_data(bar_volume, 1) < bar_data(bar_volume, 2)) & (bar_data(bar_volume, 1) < bar_data(bar_volume, 3))
  93.  
  94. # Calculate ATR (14)
  95. atr_highs = history(15, '1d', 'high')
  96. atr_lows = history(15, '1d', 'low')
  97. atr_closes = history(15, '1d', 'close_price')
  98. ATR = pd.DataFrame(index=atr_closes.index,columns=atr_closes.columns)
  99. for security in list(atr_closes.columns.values):
  100. ATR[security] = talib.ATR(atr_highs[security],atr_lows[security],atr_closes[security], timeperiod=14)
  101.  
  102. # Weakness signal 1
  103. weakness_signal_1 = wide_spread_bar & down_close_bar & (short_slopeM > 0)
  104.  
  105. # Strength signal 1
  106. strength_signal_1 = two_days_lowest_volume & (bar_data(bar_volume, 1) < bar_data(bar_volume, 2)) & (high_close_bar)
  107.  
  108. ######### TRADE MANAGEMENT
  109. position = context.portfolio.positions[context.security].amount
  110. stop_loss = bar_data(bar_open, 0) - ATR.tail(1)
  111. take_profit = bar_data(bar_open, 0) + 2*ATR.tail(1)
  112. stop_loss_hit = bar_data(bar_low, 0) < stop_loss
  113. take_profit_hit = bar_data(bar_low, 0) > take_profit
  114.  
  115. if strength_signal_1[0] and (position == 0):
  116. order_target_percent(context.security, 0.20)
  117. elif position != 0 or (stop_loss_hit is True) or (take_profit_hit is True):
  118. order_target_percent(context.security, 0)
Add Comment
Please, Sign In to add comment