Back

Migrating from Quantopian to QuantConnect

In this thread, we are going to cover the differences between Quantopian and QuantConnect APIs.

Basic Algorithm
In QuantConnect, all algorithms must have an Initialize method to setup your strategy. Once setup most algorithms have OnData event handlers to process market data and make trading decisions:

# Quantopian
def initialize(context):
    # Reference to AAPL
    context.aapl = sid(24)

def handle_data(context, data):
    # Position 100% of our portfolio to be long in AAPL
    order_target_percent(context.aapl, 1.00)
# QuantConnect

class MyAlgo(QCAlgorithm):
    def Initialize(self):
    # Reference to AAPL
        self.aapl = self.AddEquity("AAPL")

    def OnData(self, data):
        # Position 100% of our portfolio to be long in AAPL
        self.SetHoldings("AAPL", 1.00)

Please note that we must define a class with the QuantConnect API and it must inherit from QCAlgorithm.
self refers to the instance attributes of the algorithm class. It is used to access methods and members of QCAlgorithm like AddEquity in the example above.

Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Hi,

def OnData(self, slice):

history = self.History(self.security_list, 40, Resolution.Daily)
history_mean = history['close'].mean()
self.log(str(history_mean))

I tried the above and got 'Build Request Successful'. But failed to do the backtest. What went wrong? I just wanted to see logs of 'mean' of the securities for the past 40 days. Help me! Thanks.  

0

Hi,

self.Schedule.On(self.DateRules.EveryDay(self.security_list),
self.TimeRules.AfterMarketOpen(self.security_list, 60),
Action(self.OnData))

1). Can we do this? 'Action(self.OnData))' ?

2). Do we need to put all the trading orders under the 'OnData(self, data)'? or can we do so under anywhere we want (e.g. def rebalance(self), ...)?

Thanks.

0

HanByul P, could you please open a new thread with your question and share the algorithm instead of code snippets?
Answering the last two questions:
1. No. System.Action encapsulates a method that has no parameters and does not return a value. OnData has one parameter.
2. Also no. You can place orders in method of the algorithm. In OnData is "special" because it is a entry point of new data and the Slice object was designed to deliver a snapshot of the market at a given time. However, the Security object also has pricing data, so if we need the price for placing an order, we can use it:

# In OnData(self, slice)
price = slice["SPY"].Close

# In any method
price = self.Securities["SPY"].Price
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@Alexandre Catarino, Thanks a lot.

I will have many more questions: In Quantopian, we did this as below.

# Quantopian

# For checking our leverage and counting longs/shorts
def my_record_vars(context, data):

longs = shorts = 0
for position in context.portfolio.positions.itervalues():
if position.amount > 0:
longs += 1
if position.amount < 0:
shorts += 1

record(leverage=context.account.leverage, \
Long_Count=longs, Short_Count=shorts)
In Quantopian, the 'record' function plots what we want to see as above in the backtest screen. In the QC's backtest, I see a plot of 'Daily Performance' below the equity performance chart. Just like a 'Daily Performance', I want to see leverage and counting of all the numbers of longs and shorts in the backtest screen..Can we do this in QC? Thanks.
0

We have addressed Charting in QuantConnect in this thread. Here is the comment with a working example:

https://www.quantconnect.com/forum/discussion/2317/migrating-from-quantopian-to-quantconnect/p1/comment-7000

The account leverage for Equity can be calculated by the ratio between Portfolio.TotalAbsoluteHoldingsCost and Portfolio.TotalPortfolioValue:

account_leverage = self.Portfolio.TotalAbsoluteHoldingsCost / self.Portfolio.TotalPortfolioValue
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@HanByul P, (and others)

I spent MANY hours struggling with this until I figured out the trick: QC's historical data is given in a multiindex dataframe like this:

                    open    high    low    close   volume
spy 2017-1-1 00:00  101.00  105.00  99.00  100.00  123456
    2017-1-2 00:00  101.00  105.00  99.00  100.00  123456
    2017-1-3 00:00  101.00  105.00  99.00  100.00  123456
qqq 2017-1-1 00:00  201.00  205.00  199.00 200.00  123456
    2017-1-2 00:00  201.00  205.00  199.00 200.00  123456
    2017-1-3 00:00  201.00  205.00  199.00 200.00  123456

 

So when you use history["close"].mean() it gives you the mean of all the close prices of all the stocks averaged together. In my example it would give you a value of 150.00 which is useless to us. In order to get the data into quantopian format (for use with the quantopian code) You need to pick an OHLCV value then unstack the multiindex dataframe so that each stock becomes it's own column like this:

History = self.History(stocks, 20, Resolution.Daily)["close"].unstack(level=0)

                spy     qqq
2017-1-1 00:00  100.00  200.00
2017-1-2 00:00  100.00  200.00
2017-1-3 00:00  100.00  200.00

So now you can use .mean() which would give you values of SPY = 100.00 and QQQ = 200.00

I'm sure there's a better way to do it in a pure QC way but .unstack(level=0) pretty much gives you plug and play with quantopian code.

1

Great suggestion LukeI!

To keep this thread focused on specific migration tips I'll close it for now. If anyone has a migration tip contact us and we'll re-open to add your suggestion/addition so others reading the thread can get the most condensed information possible. 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed