Installing zipline module in pycharm?

Source: https://stackoverflow.com/questions/42358034/how-to-install-zipline-module-in-pycharm

 

To start, in PyCharm open Settings -> Project(XXX) -> Project Interpreter. Then click on the + icon in the top right of the screen, type Zipline in the search bar, then click on Install Packageto install Zipline.

You’ll need to download the sample Quandl data, by running this on the command line:

zipline ingest -b quantopian-quandl

To test if Zipline was installed successfully, create ‘dual_moving_average.py’ and paste in this sample application:

from zipline.api import (
history,
order_target,
record,
symbol,
)

def initialize(context):
    context.i = 0

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 = history(100, '1d', 'price').mean()
    long_mavg = history(300, '1d', 'price').mean()

    sym = symbol('AAPL')

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

    # Save values for later inspection
    record(AAPL=data[sym].price,
           short_mavg=short_mavg[sym],
           long_mavg=long_mavg[sym])

To run the algo using Zipline, execute the following on the command line (you can change the dates to a time-frame more to your liking of course):

zipline run -f dual_moving_average.py --start 2011-1-1 --end 2012-1-1 -o dma.pickle

If all this works without error, do a little happy dance! 🙂 Because, Zipline is now installed, and you’ve run your first algo.

 

 

Keywords: Quantopian, Robinhood, algotrading

References

  1. https://www.reddit.com/r/learnpython/comments/3g69lw/algorithmic_trading_and_finance_with_python/

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *