<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 IPython 与 RQAlpha
加载 RQAlpha magic
Step1:  查看 RQAlpha magic 帮助
我们可以通过 %%rqalpha 直接在 cell 中运行回测代码。 %%rqalpha 后面的参数等价于在 CLI 中后面的 rqalpha run 的参数
Step2:  使用 %%rqalpha 进行回测
Step3:  获取回测报告
运行完回测后，报告会自动存储到 report 变量中。可以直接通过 report 变量获取当次回测的结果。
另外 rqalpha 的 mod 的输出会自动存储在 results 变量中。
Step4:  使用 run_func 运行回测
<ASSISTANT_TASK:>
Python Code:
%load_ext rqalpha
Explanation: IPython 与 RQAlpha
加载 RQAlpha magic
End of explanation
%%rqalpha -h
""
Explanation: 查看 RQAlpha magic 帮助
我们可以通过 %%rqalpha 直接在 cell 中运行回测代码。 %%rqalpha 后面的参数等价于在 CLI 中后面的 rqalpha run 的参数
End of explanation
%%rqalpha -s 20100101 -e 20170505 -p -bm 000001.XSHG --account stock 100000
def init(context):
    context.stocks = ['000300.XSHG', '000905.XSHG', '000012.XSHG']
    
def handle_bar(context, bar_dict):
    [hs, zz, gz] = context.stocks
    hs_history20 = history_bars(hs, 20, '1d', 'close')
    zz_history20 = history_bars(zz, 20, '1d', 'close')
    
    hsIncrease = hs_history20[-1] - hs_history20[0]
    zzIncrease = zz_history20[-1] - zz_history20[0]
    
    positions = context.portfolio.positions
    [hsQuality, zzQuality, gzQuality] = [positions[hs].quantity, positions[zz].quantity, positions[gz].quantity]
    if hsIncrease < 0 and zzIncrease < 0:
        if hsQuality > 0: order_target_percent(hs, 0)
        if zzQuality > 0: order_target_percent(zz, 0)
        order_target_percent(gz, 1)
    elif hsIncrease < zzIncrease:
        if hsQuality > 0: order_target_percent(hs, 0)
        if gzQuality > 0: order_target_percent(gz, 0)
        order_target_percent(zz, 1)
    else:
        if zzQuality > 0: order_target_percent(zz, 0)
        if gzQuality > 0: order_target_percent(gz, 0)
        order_target_percent(hs, 1)
        #logger.info("positions hs300: " + str(hsQuality) + ", zz500: " + str(zzQuality) + ", gz: " + str(gzQuality))
Explanation: 使用 %%rqalpha 进行回测
End of explanation
results.keys()
report.keys()
report.trades[:5]
report.portfolio[:5]
report.stock_positions[:5]
Explanation: 获取回测报告
运行完回测后，报告会自动存储到 report 变量中。可以直接通过 report 变量获取当次回测的结果。
另外 rqalpha 的 mod 的输出会自动存储在 results 变量中。
End of explanation
config = {
  "base": {
    "start_date": "2010-01-01",
    "end_date": "2017-05-05",
    "benchmark": "000001.XSHG",
    "accounts": {
        "stock": 100000
    }
  },
  "extra": {
    "log_level": "info",
  },
  "mod": {
    "sys_analyser": {
      "enabled": True,
      "plot": True,
    },
  }
}
from rqalpha.api import *
from rqalpha import run_func
def init(context):
    context.stocks = ['000300.XSHG', '000905.XSHG', '000012.XSHG']
    
def handle_bar(context, bar_dict):
    [hs, zz, gz] = context.stocks
    hs_history20 = history_bars(hs, 20, '1d', 'close')
    zz_history20 = history_bars(zz, 20, '1d', 'close')
    
    hsIncrease = hs_history20[-1] - hs_history20[0]
    zzIncrease = zz_history20[-1] - zz_history20[0]
    
    positions = context.portfolio.positions
    [hsQuality, zzQuality, gzQuality] = [positions[hs].quantity, positions[zz].quantity, positions[gz].quantity]
    if hsIncrease < 0 and zzIncrease < 0:
        if hsQuality > 0: order_target_percent(hs, 0)
        if zzQuality > 0: order_target_percent(zz, 0)
        order_target_percent(gz, 1)
    elif hsIncrease < zzIncrease:
        if hsQuality > 0: order_target_percent(hs, 0)
        if gzQuality > 0: order_target_percent(gz, 0)
        order_target_percent(zz, 1)
    else:
        if zzQuality > 0: order_target_percent(zz, 0)
        if gzQuality > 0: order_target_percent(gz, 0)
        order_target_percent(hs, 1)
        
        
results = run_func(init=init, handle_bar=handle_bar, config=config)
report = results["sys_analyser"]
report["trades"][:5]
Explanation: 使用 run_func 运行回测
End of explanation
<END_TASK>