[关闭]
@Channelchan 2017-06-19T09:48:28.000000Z 字数 2216 阅读 818

4. 指数对冲

Hedge


空仓操作

利用基本面选股,当大盘低于MA40空仓。

  1. # coding=utf-8
  2. import numpy as np
  3. import talib as ta
  4. import pandas as pd
  5. import os
  6. import rqalpha
  7. from rqalpha.api import *
  8. from fxdayu_data import DataAPI

DataAPI读取HS300指数

在init设置你需要的外部数据。

  1. def init(context):
  2. DataAPI.set_file('D:/PycharmProjects/Quant_Alpha/Data_Manager/Valuation_Selection/config.py')
  3. df = DataAPI.candle('sh000001', 'D')
  4. df['MA_hedge'] = ta.abstract.MA(df, 40)
  5. context.df = df
  6. #读取excel选股表
  7. codes = pd.read_excel('Value_Selection.xlsx')
  8. codes.index = codes.pop('date')
  9. context.codes = codes
  1. def find_pool(context, date):
  2. codes = context.codes.loc[date]
  3. stocks = codes.index[codes == True]
  4. return stocks
  5. def handle_bar(context, bar_dict):
  6. if can_trade(context):
  7. sell(context)
  8. buy(context, bar_dict)
  9. def buy(context, bar_dict):
  10. pool = find_pool(context, context.now)
  11. if pool is not None:
  12. stocks_len = len(pool)
  13. for stocks in context.portfolio.positions:
  14. if stocks not in pool:
  15. order_target_percent(stocks, 0)
  16. result = []
  17. for codes in pool:
  18. data_c = history_bars(codes, 25, '1d', 'close', adjust_type='post')
  19. ma = ta.MA(data_c, timeperiod=20)
  20. if len(ma) > 3 and context.df.close.loc[context.now] > context.df.MA100.loc[context.now]:
  21. if ma[-1] > ma[-2] and ma[-2] > ma[-3] and codes not in result and codes not in context.portfolio.positions:
  22. result.append(codes)
  23. if len(result):
  24. for r in result:
  25. order_target_percent(r, 1.0 / stocks_len)
  26. def sell(context):
  27. sell_list = []
  28. for stocks in context.portfolio.positions:
  29. data_c = history_bars(stocks, 25, '1d', 'close', adjust_type='post')
  30. ma = ta.MA(data_c, timeperiod=20)
  31. if len(ma) > 3:
  32. if (ma[-1] < ma[-2] and ma[-2] < ma[-3]):
  33. sell_list.append(stocks)
  34. # print('sell_list:', sell_list)
  35. if len(sell_list):
  36. for s in sell_list:
  37. order_target_percent(s, 0)
  38. def sell_all(context):
  39. for stock in context.portfolio.positions:
  40. order_target_percent(stock, 0)
  41. #设置可否交易的布尔开关
  42. def can_trade(context):
  43. if context.df.close.loc[context.now] < context.df.MA_hedge.loc[context.now]:
  44. sell_all(context)
  45. return False
  46. else:
  47. return True
  48. config = {
  49. "base": {
  50. "start_date": "2010-06-01",
  51. "end_date": "2017-05-30",
  52. "frequency": "1d",
  53. "securities": ['stock'],
  54. "stock_starting_cash": 10000000,
  55. "benchmark": "000300.XSHG",
  56. "strategy_file_path": os.path.abspath(__file__)
  57. },
  58. "extra": {
  59. "log_level": "verbose",
  60. },
  61. "mod": {
  62. "sys_analyser": {
  63. "report_save_path": ".",
  64. "enabled": True,
  65. "plot": True
  66. },
  67. 'sys_simulation': {'match_engine': 'next_bar'}
  68. }
  69. }
  70. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注