Zipline: latest tutorial.ipynb has non working examples

Created on 5 Sep 2016  路  9Comments  路  Source: quantopian/zipline

Dear Zipline Maintainers,

Before I tell you about my issue, let me describe my environment:

Environment

  • Operating System: (MAC OS X El Capitan`)
  • Python Version: $ python --3.4
  • Python Bitness: $ python -c 'import math, sys;print(int(math.log(sys.maxsize + 1, 2) + 1))'
  • How did you install Zipline: (pip)
  • Python packages: $ pip freeze or $ conda list

Now that you know a little about me, let me tell you about the issue I am
having

Description of Issue

While going through the latest tutorial.ipynb it throws an error:
TypeError: a float is required

  • What did you expect to happen?
    I ran the notebook and expected to see the same results as in your notebook
  • What happened instead?
    An error:
    TypeError: a float is required

Here is how you can reproduce this issue on your machine:

Reproduction Steps

1.Run the last cell in the tutorial

...

What steps have you taken to resolve this already?

I was trying to identify where the errors belongs to by commenting the lines of code. I'm a beginner , so I don't know how to solve it yet. It seems like the error is thrown when accessing the line:
short_mavg = history(100, '1d', 'price').mean()
...

Anything else?

...

Sincerely,
$ whoami

Documentation

Most helpful comment

Here is working code for the ipynb tutorial :

%%zipline --start 2001-1-1 --end 2014-1-1 -o perf_dma


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

def initialize(context):
    context.i = 0
    context.stock = symbol('AAPL')



def handle_data(context, data):
    # Skip first 300 days to get full windows
    context.i += 1
    if context.i < 300:
        return

    # Compute averages
    # history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = data.history(context.stock, 'price', bar_count=100, frequency="1d").mean()
    long_mavg = data.history(context.stock, 'price', bar_count=300, frequency="1d").mean()

    # Trading logic
    if short_mavg > long_mavg:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(context.stock, 100)
    elif short_mavg < long_mavg:
        order_target(context.stock, 0)

    # Save values for later inspection
    record(AAPL=data.current(context.stock, 'price'),
           short_mavg=short_mavg,
           long_mavg=long_mavg)


def analyze(context, perf):
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')

    ax2 = fig.add_subplot(212)

    perf['AAPL'].plot(ax=ax2)
    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)

    perf_trans = perf.ix[[t != [] for t in perf.transactions]]
    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]
    sells = perf_trans.ix[[t[0]['amount'] < 0 for t in perf_trans.transactions]]
    ax2.plot(buys.index, perf.short_mavg.ix[buys.index], '^', markersize=10, color='m')
    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],'v', markersize=10, color='k')
    ax2.set_ylabel('price in $')
    plt.legend(loc=0)
    plt.show()

I can do a PR if you want, but I'm not sure on what branch or if on master ?

All 9 comments

Hey, where can I find the latest tutorial.ipynb? I was experiencing some issues with the zipline examples as well. Thanks

The latest version of that tutorial notebook is here, but it looks outdated. It's not a notebook, but this tutorial doc has been updated more recently.

What is the traceback for your TypeError? Hard to say what will fix it without knowing the cause.

Hi richfrank,
Thanks for the quick comeback. I tried the document you mention and ran the same last piece of code that sits in the last cell. When running it as-is, I got the same error. Please see the full error log below.

TypeError                                 Traceback (most recent call last)
<ipython-input-26-5e8074f8a863> in <module>()
----> 1 get_ipython().run_cell_magic('zipline', '--start 2000-1-1 --end 2014-1-1 -o perf_dma', "\n\nfrom zipline.api import order_target, record, symbol, history\nimport numpy as np\n\ndef initialize(context):\n    context.i = 0\n\n\ndef handle_data(context, data):\n    # Skip first 300 days to get full windows\n    context.i += 1\n    if context.i < 300:\n        return\n\n    # Compute averages\n    # history() has to be called with the same params\n    # from above and returns a pandas dataframe.\n    short_mavg = history(100, '1d', 'price').mean()\n    long_mavg = history(300, '1d', 'price').mean()\n\n    # Trading logic\n    if short_mavg[0] > long_mavg[0]:\n        # order_target orders as many shares as needed to\n        # achieve the desired number of shares.\n        order_target(symbol('AAPL'), 100)\n    elif short_mavg[0] < long_mavg[0]:\n        order_target(symbol('AAPL'), 0)\n\n    # Save values for later inspection\n    record(AAPL=data[symbol('AAPL')].price,\n           short_mavg=short_mavg[0],\n           long_mavg=long_mavg[0])\n\n\ndef analyze(context, perf):\n    fig = plt.figure()\n    ax1 = fig.add_subplot(211)\n    perf.portfolio_value.plot(ax=ax1)\n    ax1.set_ylabel('portfolio value in $')\n\n    ax2 = fig.add_subplot(212)\n    perf['AAPL'].plot(ax=ax2)\n    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)\n\n    perf_trans = perf.ix[[t != [] for t in perf.transactions]]\n    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]\n    sells = perf_trans.ix[\n        [t[0]['amount'] < 0 for t in perf_trans.transactions]]\n    ax2.plot(buys.index, perf.short_mavg.ix[buys.index],\n             '^', markersize=10, color='m')\n    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],\n             'v', markersize=10, color='k')\n    ax2.set_ylabel('price in $')\n    plt.legend(loc=0)\n    plt.show()")

/Users/yo/anaconda/lib/python3.4/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2113             magic_arg_s = self.var_expand(line, stack_depth)
   2114             with self.builtin_trap:
-> 2115                 result = fn(magic_arg_s, cell)
   2116             return result
   2117 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/__main__.py in zipline_magic(line, cell)
    267             '%s%%zipline' % ((cell or '') and '%'),
    268             # don't use system exit and propogate errors to the caller
--> 269             standalone_mode=False,
    270         )
    271     except SystemExit as e:

/Users/yo/anaconda/lib/python3.4/site-packages/click/core.py in main(self, args, prog_name, complete_var, standalone_mode, **extra)
    694             try:
    695                 with self.make_context(prog_name, args, **extra) as ctx:
--> 696                     rv = self.invoke(ctx)
    697                     if not standalone_mode:
    698                         return rv

/Users/yo/anaconda/lib/python3.4/site-packages/click/core.py in invoke(self, ctx)
    887         """
    888         if self.callback is not None:
--> 889             return ctx.invoke(self.callback, **ctx.params)
    890 
    891 

/Users/yo/anaconda/lib/python3.4/site-packages/click/core.py in invoke(*args, **kwargs)
    532         with augment_usage_errors(self):
    533             with ctx:
--> 534                 return callback(*args, **kwargs)
    535 
    536     def forward(*args, **kwargs):

/Users/yo/anaconda/lib/python3.4/site-packages/click/decorators.py in new_func(*args, **kwargs)
     15     """
     16     def new_func(*args, **kwargs):
---> 17         return f(get_current_context(), *args, **kwargs)
     18     return update_wrapper(new_func, f)
     19 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/__main__.py in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace)
    238         print_algo=print_algo,
    239         local_namespace=local_namespace,
--> 240         environ=os.environ,
    241     )
    242 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/utils/run_algo.py in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace, environ)
    166     ).run(
    167         data,
--> 168         overwrite_sim_params=False,
    169     )
    170 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/algorithm.py in run(self, data, overwrite_sim_params)
    634         try:
    635             perfs = []
--> 636             for perf in self.get_generator():
    637                 perfs.append(perf)
    638 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/gens/tradesimulation.py in transform(self)
    235             for dt, action in self.clock:
    236                 if action == BAR:
--> 237                     every_bar(dt)
    238                 elif action == DAY_START:
    239                     once_a_day(dt)

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/gens/tradesimulation.py in every_bar(dt_to_use, current_data, handle_data)
    130                     perf_tracker.process_commission(commission)
    131 
--> 132             handle_data(algo, current_data, dt_to_use)
    133 
    134             # grab any new orders from the blotter, then clear the list.

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt)
    205                     data,
    206                     dt,
--> 207                     context.trading_environment,
    208                 )
    209 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/utils/events.py in handle_data(self, context, data, dt, env)
    224         """
    225         if self.rule.should_trigger(dt, env):
--> 226             self.callback(context, data)
    227 
    228 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/algorithm.py in handle_data(self, data)
    438     def handle_data(self, data):
    439         if self._handle_data:
--> 440             self._handle_data(self, data)
    441 
    442         # Unlike trading controls which remain constant unless placing an

<algorithm> in handle_data(context, data)

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/utils/api_support.py in wrapped(*args, **kwargs)
     49     def wrapped(*args, **kwargs):
     50         # Get the instance and call the method
---> 51         return getattr(get_algo_instance(), f.__name__)(*args, **kwargs)
     52     # Add functor to zipline.api
     53     setattr(zipline.api, f.__name__, wrapped)

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/utils/api_support.py in wrapped_method(self, *args, **kwargs)
     96             if not self.initialized:
     97                 raise exception
---> 98             return method(self, *args, **kwargs)
     99         return wrapped_method
    100     return decorator

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/algorithm.py in history(self, bar_count, frequency, field, ffill)
   1904             self._calculate_universe(),
   1905             field,
-> 1906             ffill
   1907         )
   1908 

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/algorithm.py in get_history_window(self, bar_count, frequency, assets, field, ffill)
   1915                 frequency,
   1916                 field,
-> 1917                 ffill,
   1918             )
   1919         else:

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/data_portal.py in get_history_window(self, assets, end_dt, bar_count, frequency, field, ffill)
   1232             if field == "price":
   1233                 df = self._get_history_daily_window(assets, end_dt, bar_count,
-> 1234                                                     "close")
   1235             else:
   1236                 df = self._get_history_daily_window(assets, end_dt, bar_count,

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/data_portal.py in _get_history_daily_window(self, assets, end_dt, bar_count, field_to_use)
   1053                 eq_assets.append(asset)
   1054         eq_data = self._get_history_daily_window_equities(
-> 1055             eq_assets, days_for_window, end_dt, field_to_use
   1056         )
   1057         if future_data:

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/data_portal.py in _get_history_daily_window_equities(self, assets, days_for_window, end_dt, field_to_use)
   1136                 field_to_use,
   1137                 days_for_window,
-> 1138                 extra_slot=False
   1139             )
   1140         else:

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/data_portal.py in _get_daily_window_for_sids(self, assets, field, days_in_window, extra_slot)
   1458             data = self._equity_history_loader.history(assets,
   1459                                                        days_in_window,
-> 1460                                                        field)
   1461             if extra_slot:
   1462                 return_array[:len(return_array) - 1, :] = data

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/us_equity_loader.py in history(self, assets, dts, field)
    285         out : np.ndarray with shape(len(days between start, end), len(assets))
    286         """
--> 287         block = self._ensure_sliding_windows(assets, dts, field)
    288         end_ix = self._calendar.get_loc(dts[-1])
    289         return hstack([window.get(end_ix) for window in block])

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/us_equity_loader.py in _ensure_sliding_windows(self, assets, dts, field)
    246                 if self._adjustments_reader:
    247                     adjs = self._get_adjustments_in_range(
--> 248                         asset, prefetch_dts, field)
    249                 else:
    250                     adjs = {}

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/data/us_equity_loader.py in _get_adjustments_in_range(self, asset, dts, field)
    162                                            0,
    163                                            0,
--> 164                                            d[1])
    165                     try:
    166                         adjs[end_loc].append(mult)

/Users/yo/anaconda/lib/python3.4/site-packages/zipline/lib/adjustment.pyx in zipline.lib.adjustment.Float64Adjustment.__init__ (zipline/lib/adjustment.c:5172)()
    261                  Py_ssize_t first_col,
    262                  Py_ssize_t last_col,
--> 263                  float64_t value):
    264 
    265         super(Float64Adjustment, self).__init__(

TypeError: a float is required

here is my error log, looks identical to the log above

TypeError                                 Traceback (most recent call last)
<ipython-input-10-a5a1f0dbb47b> in <module>()
----> 1 get_ipython().run_cell_magic(u'zipline', u'--start 2000-1-1 --end 2014-1-1 -o perf_dma', u"\n\nfrom zipline.api import order_target, record, symbol, history\nimport numpy as np\n\ndef initialize(context):\n    context.i = 0\n\n\ndef handle_data(context, data):\n    # Skip first 300 days to get full windows\n    context.i += 1\n    if context.i < 300:\n        return\n\n    # Compute averages\n    # history() has to be called with the same params\n    # from above and returns a pandas dataframe.\n    short_mavg = history(100, '1d', 'price').mean()\n    long_mavg = history(300, '1d', 'price').mean()\n\n    # Trading logic\n    if short_mavg[0] > long_mavg[0]:\n        # order_target orders as many shares as needed to\n        # achieve the desired number of shares.\n        order_target(symbol('AAPL'), 100)\n    elif short_mavg[0] < long_mavg[0]:\n        order_target(symbol('AAPL'), 0)\n\n    # Save values for later inspection\n    record(AAPL=data[symbol('AAPL')].price,\n           short_mavg=short_mavg[0],\n           long_mavg=long_mavg[0])\n    \n    \ndef analyze(context, perf):\n    fig = plt.figure()\n    ax1 = fig.add_subplot(211)\n    perf.portfolio_value.plot(ax=ax1)\n    ax1.set_ylabel('portfolio value in $')\n\n    ax2 = fig.add_subplot(212)\n    perf['AAPL'].plot(ax=ax2)\n    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)\n\n    perf_trans = perf.ix[[t != [] for t in perf.transactions]]\n    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]\n    sells = perf_trans.ix[\n        [t[0]['amount'] < 0 for t in perf_trans.transactions]]\n    ax2.plot(buys.index, perf.short_mavg.ix[buys.index],\n             '^', markersize=10, color='m')\n    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],\n             'v', markersize=10, color='k')\n    ax2.set_ylabel('price in $')\n    plt.legend(loc=0)\n    plt.show()")

C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2118             magic_arg_s = self.var_expand(line, stack_depth)
   2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
   2121             return result
   2122 

C:\Anaconda2\lib\site-packages\zipline\__main__.pyc in zipline_magic(line, cell)
    267             '%s%%zipline' % ((cell or '') and '%'),
    268             # don't use system exit and propogate errors to the caller
--> 269             standalone_mode=False,
    270         )
    271     except SystemExit as e:

C:\Anaconda2\lib\site-packages\click\core.pyc in main(self, args, prog_name, complete_var, standalone_mode, **extra)
    694             try:
    695                 with self.make_context(prog_name, args, **extra) as ctx:
--> 696                     rv = self.invoke(ctx)
    697                     if not standalone_mode:
    698                         return rv

C:\Anaconda2\lib\site-packages\click\core.pyc in invoke(self, ctx)
    887         """
    888         if self.callback is not None:
--> 889             return ctx.invoke(self.callback, **ctx.params)
    890 
    891 

C:\Anaconda2\lib\site-packages\click\core.pyc in invoke(*args, **kwargs)
    532         with augment_usage_errors(self):
    533             with ctx:
--> 534                 return callback(*args, **kwargs)
    535 
    536     def forward(*args, **kwargs):

C:\Anaconda2\lib\site-packages\click\decorators.pyc in new_func(*args, **kwargs)
     15     """
     16     def new_func(*args, **kwargs):
---> 17         return f(get_current_context(), *args, **kwargs)
     18     return update_wrapper(new_func, f)
     19 

C:\Anaconda2\lib\site-packages\zipline\__main__.pyc in run(ctx, algofile, algotext, define, data_frequency, capital_base, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace)
    238         print_algo=print_algo,
    239         local_namespace=local_namespace,
--> 240         environ=os.environ,
    241     )
    242 

C:\Anaconda2\lib\site-packages\zipline\utils\run_algo.pyc in _run(handle_data, initialize, before_trading_start, analyze, algofile, algotext, defines, data_frequency, capital_base, data, bundle, bundle_timestamp, start, end, output, print_algo, local_namespace, environ)
    178     ).run(
    179         data,
--> 180         overwrite_sim_params=False,
    181     )
    182 

C:\Anaconda2\lib\site-packages\zipline\algorithm.pyc in run(self, data, overwrite_sim_params)
    686         try:
    687             perfs = []
--> 688             for perf in self.get_generator():
    689                 perfs.append(perf)
    690 

C:\Anaconda2\lib\site-packages\zipline\gens\tradesimulation.pyc in transform(self)
    218             for dt, action in self.clock:
    219                 if action == BAR:
--> 220                     for capital_change_packet in every_bar(dt):
    221                         yield capital_change_packet
    222                 elif action == SESSION_START:

C:\Anaconda2\lib\site-packages\zipline\gens\tradesimulation.pyc in every_bar(dt_to_use, current_data, handle_data)
    131                     perf_tracker.process_commission(commission)
    132 
--> 133             handle_data(algo, current_data, dt_to_use)
    134 
    135             # grab any new orders from the blotter, then clear the list.

C:\Anaconda2\lib\site-packages\zipline\utils\events.pyc in handle_data(self, context, data, dt)
    182                     context,
    183                     data,
--> 184                     dt,
    185                 )
    186 

C:\Anaconda2\lib\site-packages\zipline\utils\events.pyc in handle_data(self, context, data, dt)
    201         """
    202         if self.rule.should_trigger(dt):
--> 203             self.callback(context, data)
    204 
    205 

C:\Anaconda2\lib\site-packages\zipline\algorithm.pyc in handle_data(self, data)
    457     def handle_data(self, data):
    458         if self._handle_data:
--> 459             self._handle_data(self, data)
    460 
    461         # Unlike trading controls which remain constant unless placing an

<algorithm> in handle_data(context, data)

C:\Anaconda2\lib\site-packages\zipline\utils\api_support.pyc in wrapped(*args, **kwargs)
     49     def wrapped(*args, **kwargs):
     50         # Get the instance and call the method
---> 51         return getattr(get_algo_instance(), f.__name__)(*args, **kwargs)
     52     # Add functor to zipline.api
     53     setattr(zipline.api, f.__name__, wrapped)

C:\Anaconda2\lib\site-packages\zipline\utils\api_support.pyc in wrapped_method(self, *args, **kwargs)
     96             if not self.initialized:
     97                 raise exception
---> 98             return method(self, *args, **kwargs)
     99         return wrapped_method
    100     return decorator

C:\Anaconda2\lib\site-packages\zipline\algorithm.pyc in history(self, bar_count, frequency, field, ffill)
   2063             self._calculate_universe(),
   2064             field,
-> 2065             ffill
   2066         )
   2067 

C:\Anaconda2\lib\site-packages\zipline\algorithm.pyc in get_history_window(self, bar_count, frequency, assets, field, ffill)
   2074                 frequency,
   2075                 field,
-> 2076                 ffill,
   2077             )
   2078         else:

C:\Anaconda2\lib\site-packages\zipline\data\data_portal.pyc in get_history_window(self, assets, end_dt, bar_count, frequency, field, ffill)
    772             if field == "price":
    773                 df = self._get_history_daily_window(assets, end_dt, bar_count,
--> 774                                                     "close")
    775             else:
    776                 df = self._get_history_daily_window(assets, end_dt, bar_count,

C:\Anaconda2\lib\site-packages\zipline\data\data_portal.pyc in _get_history_daily_window(self, assets, end_dt, bar_count, field_to_use)
    644 
    645         data = self._get_history_daily_window_data(
--> 646             assets, days_for_window, end_dt, field_to_use
    647         )
    648         return pd.DataFrame(

C:\Anaconda2\lib\site-packages\zipline\data\data_portal.pyc in _get_history_daily_window_data(self, assets, days_for_window, end_dt, field_to_use)
    672                 assets,
    673                 field_to_use,
--> 674                 days_for_window[0:-1]
    675             )
    676 

C:\Anaconda2\lib\site-packages\zipline\data\data_portal.pyc in _get_daily_window_for_sids(self, assets, field, days_in_window, extra_slot)
    907                                                 days_in_window,
    908                                                 field,
--> 909                                                 extra_slot)
    910             if extra_slot:
    911                 return_array[:len(return_array) - 1, :] = data

C:\Anaconda2\lib\site-packages\zipline\data\history_loader.pyc in history(self, assets, dts, field, is_perspective_after)
    375                                              dts,
    376                                              field,
--> 377                                              is_perspective_after)
    378         end_ix = self._calendar.get_loc(dts[-1])
    379         return hstack([window.get(end_ix) for window in block])

C:\Anaconda2\lib\site-packages\zipline\data\history_loader.pyc in _ensure_sliding_windows(self, assets, dts, field, is_perspective_after)
    278                 if self._adjustments_reader:
    279                     adjs = self._get_adjustments_in_range(
--> 280                         asset, prefetch_dts, field, is_perspective_after)
    281                 else:
    282                     adjs = {}

C:\Anaconda2\lib\site-packages\zipline\data\history_loader.pyc in _get_adjustments_in_range(self, asset, dts, field, is_perspective_after)
    184                                            0,
    185                                            0,
--> 186                                            d[1])
    187                     try:
    188                         adjs[adj_loc].append(mult)

C:\Anaconda2\lib\site-packages\zipline\lib\adjustment.pyx in zipline.lib.adjustment.Float64Adjustment.__init__ (zipline/lib\adjustment.c:4833)()
    261                  Py_ssize_t first_col,
    262                  Py_ssize_t last_col,
--> 263                  float64_t value):
    264 
    265         super(Float64Adjustment, self).__init__(

TypeError: a float is required

This has also been referenced on StackOverflow

But the solution didn't work for me. The problem appears to be with history() which is deprecated.

Here is working code for the ipynb tutorial :

%%zipline --start 2001-1-1 --end 2014-1-1 -o perf_dma


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

def initialize(context):
    context.i = 0
    context.stock = symbol('AAPL')



def handle_data(context, data):
    # Skip first 300 days to get full windows
    context.i += 1
    if context.i < 300:
        return

    # Compute averages
    # history() has to be called with the same params
    # from above and returns a pandas dataframe.
    short_mavg = data.history(context.stock, 'price', bar_count=100, frequency="1d").mean()
    long_mavg = data.history(context.stock, 'price', bar_count=300, frequency="1d").mean()

    # Trading logic
    if short_mavg > long_mavg:
        # order_target orders as many shares as needed to
        # achieve the desired number of shares.
        order_target(context.stock, 100)
    elif short_mavg < long_mavg:
        order_target(context.stock, 0)

    # Save values for later inspection
    record(AAPL=data.current(context.stock, 'price'),
           short_mavg=short_mavg,
           long_mavg=long_mavg)


def analyze(context, perf):
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    perf.portfolio_value.plot(ax=ax1)
    ax1.set_ylabel('portfolio value in $')

    ax2 = fig.add_subplot(212)

    perf['AAPL'].plot(ax=ax2)
    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)

    perf_trans = perf.ix[[t != [] for t in perf.transactions]]
    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]
    sells = perf_trans.ix[[t[0]['amount'] < 0 for t in perf_trans.transactions]]
    ax2.plot(buys.index, perf.short_mavg.ix[buys.index], '^', markersize=10, color='m')
    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],'v', markersize=10, color='k')
    ax2.set_ylabel('price in $')
    plt.legend(loc=0)
    plt.show()

I can do a PR if you want, but I'm not sure on what branch or if on master ?

@mathvdh Sorry for the late response here; if you want to submit a PR you could make a branch for that, otherwise I'll try and get around to updating it at some point

I'm also experiencing this issue. I have a simple pipeline with RSI. This is a blocker...must be fixed asap. I suspect it's the data issue? Also quantopian-quandl downloads, but not Quandl data cannot be downloaded - strange.

@mathvdh Actually, I'm just going to open a PR and fix this, as it's been a while since this issue has been opened.

Was this page helpful?
0 / 5 - 0 ratings