Beispiel #1
0
def run_algo(request):
    """
    运行算法,ajax方法
    :param request: code start end capital_base freq
    :return: 运行后的数据,{'data': {}, 'status': ''}
    annualized_return 年化收益率
    benchmark_annualized_return 基准年化收益率
    alpha 阿尔法
    beta 贝塔
    sharpe 夏普比率
    volatility 收益波动率
    information 信息比率
    max_drawdown 最大回撤
    cumulative_return: {'': 1} 策略累计收益率
    benchmark_ cumulative_return {'': 1} 基准累计收益率
    """
    if request.method == 'POST':
        account_info = request.session.get('account')

        code = request.POST.get('code')
        start_date = request.POST.get('start', '2013-02-01')
        end_date = request.POST.get('end', '2013-03-01')
        capital_base = request.POST.get('capital_base', 100000)
        freq = request.POST.get('freq', 'daily')
        strategy_id = request.POST.get('strategy_id', 0)
        #如果是登录用户,保存策略实例代码
        code_ins_path = None
        if account_info and strategy_id != 'demo':
            s_strategy(account_id=account_info['id'],
                       strategy_id=strategy_id,
                       code=code,
                       params={
                           'start': start_date,
                           'end': end_date,
                           'capital_base': capital_base,
                           'freq': freq
                       })

            code_ins_path = save_code_ins_file(account_info['id'], strategy_id,
                                               code)

        #运行代码
        to_return_info = execute_code(code=code,
                                      capital_base=capital_base,
                                      start=start_date,
                                      end=end_date,
                                      freq=freq,
                                      code_ins_path=code_ins_path)

        if account_info and strategy_id != 'demo':
            #运行实例保存
            save_strategy_ins(account_info['id'], strategy_id, code_ins_path,
                              to_return_info, to_return_info)

    return HttpResponse(json.dumps(to_return_info))
Beispiel #2
0
def new_strategy(request):
    """
    /strategy/new
    创建新的策略文件,默认直接返回初始代码,不可配置
    成功后直接跳转到编辑页面
    :param request: POST {'name': ''}
    :return:
    """
    if request.method == 'POST':
        name = request.POST.get('strategy_name')

        if len(Strategy.objects.filter(name=name)) > 0:
            return HttpResponse(
                json.dumps({
                    'status': 'error',
                    'data': u'策略名已存在!'
                }))

        start = (datetime.now(tz=pytz.UTC) +
                 timedelta(days=-14)).strftime('%Y-%m-%d')
        end = (datetime.now(tz=pytz.UTC) +
               timedelta(days=-7)).strftime('%Y-%m-%d')
        visit_id = md5(
            name.encode('utf-8') + str(request.session.get('account')['id']) +
            str(datetime.now().strftime("%Y%m%d%H%M%S"))).hexdigest()

        try:
            result_info = s_strategy(
                account_id=request.session.get('account')['id'],
                strategy_id=None,
                code=None,
                params={
                    'name': name,
                    'start': start,
                    'end': end,
                    'capital_base': 100000,
                    'freq': u'每天',
                    'visit_id': visit_id
                })
            return HttpResponse(json.dumps(result_info))
        except Exception, e:
            log.error('==============Quant New Strategy Error: ', e)
            return HttpResponse(
                json.dumps({
                    'status': 'error',
                    'data': u'系统出现错误,请稍后再试!'
                }))
Beispiel #3
0
def quant_template_save(request):
    """
    模板配置保存, POST方法,
    :param request: {'name': '', start: 'YYYY-mm-dd', end: '',
        'capital_base': 100000, 'freq': '', code: ''}
    :return:
    """
    if request.method == 'POST':
        name = request.POST.get('name', u'示例模板股票策略')
        start = request.POST.get('start')
        end = request.POST.get('end')
        capital_base = request.POST.get('capital_base')
        freq = request.POST.get('freq', 'daily')
        visit_id = md5(
            name.encode('utf-8') + str(request.session.get('account')['id']) +
            str(datetime.now().strftime("%Y%m%d%H%M%S"))).hexdigest()

        params = {
            'name': name,
            'start': start,
            'end': end,
            'capital_base': capital_base,
            'freq': freq,
            'visit_id': visit_id
        }

        try:
            result_info = s_strategy(
                account_id=request.session.get('account')['id'],
                strategy_id=None,
                code=request.POST.get('code', None),
                params=params)
            return HttpResponse(json.dumps(result_info))
        except Exception, e:
            log.error('===========Quant Template Save Error: ', e)
            return HttpResponse(
                json.dumps({
                    'status': 'error',
                    'data': u'生成策略失败'
                }))