Closed tdrobbin closed 5 years ago
Hi @tdrobbin, I can give you some advice on how to make sure to recreate the same performance in zipline, but I unfortunately cannot go through your code and make sure there are no bugs, that's up to you.
First thing is to understand how returns are computed in Alphalens. If you look at the documentation of alphalens.utils.get_clean_factor_and_forward_returns
you can see how the prices
information is used to compute entry and exit price for securities, those prices are used to compute the actual returns.
The pricing data must contain at least an entry for each timestamp/asset combination in the factor. This entry should reflect the buy price for the assets and usually it is the next available price after the factor is computed but it can also be a later price if the factor is meant to be traded later. The pricing data is also needed for the assets sell price, the asset price after period
timestamps will be considered the sell price for that asset when computing period
forward returns.
In you code you use the percentage change of close price as factor value. This means your factor value is available at market close and the earliest time you can trade the factor is the next day at market open. So every day, at market open, the portfolio will be rebalanced, entering new positions and exiting old ones. This means the prices
DataFrame passed to Alphalens should contain the open price of the securities: for each timestamp of factor index the price DataFrame must contain the open price of the following day.
If you want to replicate the same on zipline you need to write an algorithm that does the full portfolio rebalance only once per day, at market open. Also, during the testing phase, you need to set commission and slippage to 0. The slippage has to be set to FixedSlippage, because that's the only slippage that doesn't limit the amount of securities you can trade in a minute bar.
hope these help
I see, that explanation was very helpful. I believe my issue was with not trading all the securities at once with the correct slippage setting. Thank you very for your help!
Hello, I am testing a factor and having some difficulty recreating in zipline the simulated performance computed by alphalens. My factor is 50% long / 50% short and constructed such that when I pass it to
performance.utils.create_pyfolio_input
the positions returned are the same as the factor passed in.This might be more of a zipline issue but I would very much like to be able to take a factor I'm testing with alphalens and have some bioler plate in zipline to test, add tcosts, optimizations, etc. and be sure that it's trading the same factor as in alphalens. I've looked through the documentation, , examples, and past issues for alphalens and zipline but am still having difficulties.
There might be a timezone issue going on, but I'm not quite able to figure it out. Below are the versions of the libraries I'm using as well as some code I'm running to recreate the issue. I'm constructing a simple test factor for a 4 stock universe over 3 months and comparing the performance computed in alphalens vs zipline. I've also attached a pdf of the notebook (below is the exported markdown version of the attached notebook) Thanks!
alphalens_vs_zipline.pdf
alphalens=0.3.3 zipline=1.3.0