Quantopian live algorithm - How to?

665 Views Asked by At

The big question is :

How can I put live my quantopian algorithms, with all the strategies that I've set with alphas combination ?

I didn't find any answer.

I found Alpaca that works with zipeline but I can't use morningstar or Q1500US with alpaca, or I didn't find the way to do it.

I've took a lot of time to setup this bot, find good factor to have low alpha and good returns, I'm really disapointed and I don't really want to go back from scratch and do another bot.

Please help to find a solution and if it's not possible at all to use this bot, tell me which library as complete a quantopian I can use to do another bot in the same style.

Thanks all, my bot is jut down there !!

Here is my bot :

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)
1

There are 1 best solutions below

0
On BEST ANSWER

To trade a Quantopian strategy outside of Quantopian you need two things: the backtester and the data.

Zipline is the open-source backtesting library that powers the Quantopian backtester. The biggest problem is that out of the box, it does not support live trading. There have been some community-based attempts to adapt it for live trading, but they never gained much traction and seem to have fizzled.

The second thing you need is to obtain the data from somewhere and write custom code to load it into Zipline. Quantopian/Zipline requires 1-minute data stored in a columnar storage format called bcolz. If you're using fundamental data in Pipeline, loading that data is a separate step that requires writing custom code.

The bottom line is that adapting Zipline for live trading is not a trivial undertaking.

If you prefer a ready-to-use option, QuantRocket supports backtesting and live trading of Zipline strategies and provides built-in 1-minute US equities data. Fundamental data is available to use within Pipeline. Q1500US is specific to Quantopian and not part of the open-source library, but you can approximate it by filtering on dollar volume or market cap.

Disclaimer: I'm affiliated with QuantRocket.