Splits Yahoo Bundle and Price

79 views
Skip to first unread message

ZiplineBeginner

unread,
Feb 1, 2017, 4:29:10 PM2/1/17
to Zipline Python Opensource Backtester
Good day,

I've been struggling heavily over the past 48 hours on this issue. 

I loaded a stock listed on the Toronto stock exchange called PAR-UN.TO using the yahoo bundle that comes with zipline, I haven't modified anything...

when I use the following code to order 100 stocks, the value of my portfolio gets all messes up on mid feb. 2012 which corresponds to a reverse stock split. The stock price went from ~$1.70 to ~$7


from zipline.api import order_target, record, symbol, history, order, get_datetime
import numpy as np
import matplotlib.pyplot as plt

def initialize(context):
    context.i = 0
    context.assets = []
    context.assets.append(symbol('PAR-un.to'))
    context.previous_t1 = 1
    
    
def handle_data(context, data):
    price = data.history(context.assets, "price", 1, "1d")
    exchange_time = get_datetime('US/Eastern')
    print('{:%Y-%m-%d}'.format(exchange_time))
    for stock in context.assets:
        print('stock price is now {} and portfolio value is now {}, with {} stocks owned'.format(price[stock][-1],context.portfolio.portfolio_value, context.portfolio.positions[stock].amount))
    
    if context.previous_t1 == 1:
        order(symbol('PAR-un.to'), 100)
    context.previous_t1 = 0
    
    
def analyze(context, perf):
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')
    plt.legend(loc=0)
    plt.show()



heres the output:

2012-02-08
stock price is now 1.881 and portfolio value is now 2007.7992439703153, with 100 stocks owned
2012-02-09
stock price is now 1.881 and portfolio value is now 2007.7992439703153, with 100 stocks owned
2012-02-10
stock price is now 1.871 and portfolio value is now 2006.7992439703153, with 100 stocks owned
2012-02-13
stock price is now 1.881 and portfolio value is now 2007.7992439703153, with 100 stocks owned
[2017-02-01 21:22:33.884557] INFO: Performance: after split: sid: Equity(36 [PAR-UN.TO]), amount: 25.0, cost_basis: 7.21, last_sale_price: 1.881
[2017-02-01 21:22:33.900157] INFO: Performance: returning cash: 0.0
2012-02-14
stock price is now 1.881 and portfolio value is now 1866.7242439703155, with 25.0 stocks owned
2012-02-15
stock price is now 1.871 and portfolio value is now 1866.4742439703155, with 25.0 stocks owned
2012-02-16
stock price is now 1.793 and portfolio value is now 1864.5242439703154, with 25.0 stocks owned
2012-02-17
stock price is now 1.773 and portfolio value is now 1864.0242439703154, with 25.0 stocks owned
2012-02-21
stock price is now 1.754 and portfolio value is now 1863.5492439703153, with 25.0 stocks owned
2012-02-22
stock price is now 1.783 and portfolio value is now 1864.2742439703154, with 25.0 stocks owned
2012-02-23
stock price is now 1.715 and portfolio value is now 1862.5742439703154, with 25.0 stocks owned
2012-02-24
stock price is now 1.754 and portfolio value is now 1863.5492439703153, with 25.0 stocks owned
2012-02-27
stock price is now 1.773 and portfolio value is now 1864.0242439703154, with 25.0 stocks owned
2012-02-28
stock price is now 1.744 and portfolio value is now 1863.2992439703153, with 25.0 stocks owned
2012-02-29
stock price is now 1.773 and portfolio value is now 1864.0242439703154, with 25.0 stocks owned
2012-03-01
stock price is now 6.968 and portfolio value is now 1993.8992439703154, with 25.0 stocks owned
2012-03-02
stock price is now 7.027 and portfolio value is now 1995.3742439703153, with 25.0 stocks owned
2012-03-05
stock price is now 7.056 and portfolio value is now 1996.0992439703155, with 25.0 stocks owned
2012-03-06
stock price is now 7.095 and portfolio value is now 1997.0742439703154, with 25.0 stocks owned


Basically, I lose approx $150 on feb 14, just because of the drop in the stock price...

What should I do ? Can I adjust my portfolio value manually (which would be a terrible solution if you ask me...) or can I tell the yahoo bundle to fetch the historical adjusted price .... anyone can help please ?

Thanks in advance,


Iván Lengyel

unread,
May 19, 2017, 10:51:37 AM5/19/17
to Zipline Python Opensource Backtester
Did you have any succeed? I am having the same problem.

Peter Harrington

unread,
May 22, 2017, 8:25:37 PM5/22/17
to Zipline Python Opensource Backtester
When did you download the data from Yahoo!  
Yahoo! recently changed their API.  You may not be getting the adjustments from Yahoo!
The code that gets that data from Yahoo! is in zipline/data/bundles/yahoo.py  
Checkout  lines 136-166, you could modify that file to see that you are getting any split data from Yahoo!. 
Reply all
Reply to author
Forward
0 new messages