首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Quantopian实时算法-如何实现?

Quantopian实时算法-如何实现?
EN

Stack Overflow用户
提问于 2020-05-05 05:26:39
回答 1查看 484关注 0票数 1

最大的问题是:

我如何将我的quantopian算法与我用alphas组合设置的所有策略放在一起?

我没有找到任何答案。

我发现羊驼可以与zipeline一起使用,但我不能在羊驼上使用morningstar或Q1500US,或者我找不到方法。

我花了很多时间来设置这个机器人,找到了低alpha和高回报的好因素,我真的很失望,我真的不想从头开始做另一个机器人。

请帮助找到一个解决方案,如果根本不可能使用这个机器人,告诉我哪个库作为完整的quantopian我可以用来做相同风格的另一个机器人。

谢谢大家,我的机器人就在下面!

这是我的机器人:

代码语言:javascript
复制
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.filters.morningstar import Q1500US
from quantopian.pipeline.data.sentdex import sentiment
from quantopian.pipeline.data.morningstar import operation_ratios


def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )

    # Create our dynamic stock selector.
    algo.attach_pipeline(make_pipeline(), 'pipeline')

    #set_commission(commission.PerTrade(cost=0.001))


def make_pipeline():
    # Yes : operation_ratios.revenue_growth.latest
    # Yes : operation_ratios.operation_margin.latest
    # Yes : sentiment

    testing_factor1=operation_ratios.operation_margin.latest
    testing_factor2=operation_ratios.revenue_growth.latest
    testing_factor3=sentiment.sentiment_signal.latest

    universe =(Q1500US() & 
              testing_factor1.notnull() &
              testing_factor2.notnull() &
              testing_factor3.notnull())

    testing_factor1=testing_factor1.rank(mask=universe, method='average')
    testing_factor2=testing_factor2.rank(mask=universe, method='average')
    testing_factor3=testing_factor3.rank(mask=universe, method='average')

    testing_factor= testing_factor1 + testing_factor2 + testing_factor3

    testing_quantiles = testing_factor.quantiles(2)

    pipe = Pipeline(columns={'testing_factor':testing_factor,'shorts':testing_quantiles.eq(0),'longs':testing_quantiles.eq(1)},screen=universe)
    return pipe    

def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    context.output = algo.pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    context.security_list = context.output.index


def rebalance(context, data):

    long_secs=context.output[context.output['longs']].index
    long_weight=0.5/len(long_secs)

    short_secs=context.output[context.output['shorts']].index
    short_weight=-0.5/len(short_secs)

    for security in long_secs:
        if data.can_trade(security):
                          order_target_percent(security, long_weight)
    for security in short_secs:
        if data.can_trade(security):
                          order_target_percent(security, short_weight)

    for security in context.portfolio.positions:
        if data.can_trade(security) and security not in long_secs and security not in short_secs:
            order_target_percent(security, 0)

def record_vars(context, data):
    long_count=0

    short_count=0

    for position in context.portfolio.positions.values():
        if position.amount>0:
            long_count+=1
        elif position.amount<0:
            short_count+=1
    record(num_longs=long_count, num_shorts=short_count, leverage=context.account.leverage)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-11 21:01:22

要在Quantopian之外交易Quantopian策略,您需要两样东西:后台测试人员和数据。

Zipline是支持Quantopian反向测试器的开源反向测试库。最大的问题是开箱即用,它不支持实时交易。已经有一些community-based attempts将其应用于实时交易,但它们从未获得太多吸引力,似乎已经失败了。

您需要做的第二件事是从某个地方获取数据,并编写自定义代码将其加载到Zipline中。Quantopian/Zipline要求以一种称为bcolz的列式存储格式存储1分钟的数据。如果您在Pipeline中使用基础数据,则加载该数据是一个单独的步骤,需要编写自定义代码。

底线是,让Zipline适应实时交易并不是一件微不足道的事情。

如果您更喜欢随时可用的选项,QuantRocket支持backtesting and live trading of Zipline strategies并提供内置的1分钟美国股票数据。可在Pipeline中使用基础数据。Q1500US是特定于Quantopian的,并不是开源库的一部分,但您可以通过过滤美元数量或市值来近似它。

免责声明:我隶属于QuantRocket。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61601917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档