My previous post got error so I'm writing again. If you know both platforms Quantopian and QC already please let me know your experience with doing research and creating strategies. I believe your feedback can also help to improve QC platform.
I just started to learn Q and QC platform in the same time. I made my first simple monthly rebalance algo in Q. It was pretty easy to create the strategy in Q/Zipline. It took me 1h to learn the platform and 1h to create algo. After I moved to QC platform, were after few days of learning, I'm still failing to build the same alog. In the end I decided to use Q/Zipline/ platform as I found QC/Lean/Python very difficult to use, however I'm still interested to know if it is possible to build the same strategy in QC and how long it can takes.
As the algo can be build in Q in 5-10 min once learn basics. I assume it should take no more than 10min in QC. If it takes too long please at least give me at least an estimate how long it can takes to build the similar algo in QC.
Indicators used:
blend = m3 return% * 0.7 + m6 return% * 0.3
sma_88 is simple moving average over 88 days
Rules:
The strategy rebalance portfolio on the first day in every month at market open based on next rules:
1) Buy top 3 stocks, not included in portfolio, sorted by blend where close > sma_88(position size should be recalculated each time to be 100% invested in the step 1.
2) For stocks in portfolio where close < sma_88 transfer stocks to cash. (In the step 2 there is no position size recalculation so cash will stay in portfolio for one month and will be invested next month)
Below is the strategy in Q/Zipline:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
import quantopian.pipeline.filters as Filters
import quantopian.pipeline.factors as Factors
import pandas as pd
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import Returns, SimpleMovingAverage
def initialize(context):
attach_pipeline(pipe_definition(context), name='my_data')
schedule_function(rebalance, date_rules.month_start(), time_rules.market_open())
def pipe_definition(context):
context.stocks = symbols('DBA', 'DBC', 'DIA', 'EEM', 'EFA', 'EPP', 'EWA', 'EWJ', 'EWZ', 'FXF', 'FXI', 'GLD', 'IEV', 'ILF', 'IWM', 'IYR', 'MDY', 'QQQ', 'SPY', 'TLT', 'XLE', 'XLF')
universe = Filters.StaticAssets(context.stocks)
close_price = USEquityPricing.close.latest
m3 = Returns(inputs=[USEquityPricing.close], window_length=67)*100
m6 = Returns(inputs=[USEquityPricing.close], window_length=137)*100
blend = m3*0.7+m6*0.3
sma_88 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=89, mask=universe)
return Pipeline(
columns = {
'close_price' : close_price,
'blend': blend,
'sma_88': sma_88,
},
screen = universe,
)
def before_trading_start(context, data):
context.output = pipeline_output('my_data')
def rebalance(context, data):
buy_these = context.output.query('close_price > sma_88').sort_values(by='blend',ascending=False).iloc[:3].index.tolist()
new_order = []
for stock in buy_these:
if stock not in context.portfolio.positions and data.can_trade(stock):
new_order.append(stock)
WEIGHT = 1.00/ ( len(context.portfolio.positions) + len(new_order))
for stock in context.portfolio.positions:
order_target_percent(stock, WEIGHT)
for stock in new_order:
if stock not in context.portfolio.positions and data.can_trade(stock):
order_target_percent(stock, WEIGHT)
sell_these = context.output.query('close_price < sma_88').index.tolist()
for stock in sell_these:
if stock in context.portfolio.positions and data.can_trade(stock):
order_target_percent(stock, 0)
Don Q
Hi,
I got some very good advice how to move forward from Alex from QC, I believe it could help others.
1) Discussions about moving from Q to QC:
https://www.quantconnect.com/forum/discussion/2317/migrating-from-quantopian-to-quantconnect
https://www.quantconnect.com/forum/discussion/2572/converting-algos-from-quantopian
2) Examples of QC/Lean code that helps to build rebalance strategy:
Similar strategy full code example : EmaCrossUniverseSelectionAlgorithm,
Universe selection tutorial: Universes
To build rebalance algo similar I mentioned above SymbolData class should be addapted and selection rule should be set based on blend and price.
For the monthly execution it will be required to to add a condition on the top of CoarseSelectionFunction:
def CoarseSelectionFunction(self, coarse):
if self.month == self.Time.month:
return Universe.Unchanged
self.month = self.Time.month
Don Q
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!