zipline ro access excel and calculate implement moving crossover

96 views
Skip to first unread message

priya...@quantinsti.com

unread,
Jul 29, 2016, 1:27:10 AM7/29/16
to Zipline Python Opensource Backtester
i m trying to read from a excel file and implement moving crossover. i m able to do it with zipline inbult function 

load_bars_from_yahoo but now i want to do it through excel. i have chaged the data excatly into panel but m not able to do it.


this is the code : 



import pytz

from datetime import datetime

from zipline.api import order, symbol, record, order_target

from zipline.algorithm import TradingAlgorithm

from zipline.utils.factory import load_bars_from_yahoo

import pyexcel

import zipline

import pandas as pd



# Load data manually from Yahoo! finance

start = datetime(2011, 1, 1, 0, 0, 0, 0, pytz.utc).date()

end = datetime(2012,1,1,0,0,0,0, pytz.utc).date()


d = zipline.data.load_prices_from_csv('/home/priyanka.s/work/algorithms/AAPL.csv', 'Date')

d = d.rename(columns=str.lower)

data = pd.Panel({zipline.assets.Equity(24, 'AAPL'): d})


def initialize(context):

context.security = symbol('AAPL')

def handle_data(context, data):

MA1 = data[context.security].mavg(50)

algo_obj = TradingAlgorithm(initialize=initialize,

handle_data=handle_data)


perf_manual = algo_obj.run(data)



i m getting this error : 

TypeError: Cannot compare type 'Timestamp' with type 'NoneType'


I want to calculate profit,cash, portfolio value also


AAPL.csv

Nathan Wolfe

unread,
Aug 4, 2016, 10:06:33 AM8/4/16
to Zipline Python Opensource Backtester
Hi Priyanka,

Unfortunately zipline.data.load_prices_from_csv doesn't work well with DD/MM/YYYY date format. To load prices from a CSV file with the dates in this format, you should use pd.read_csv:
d = pd.read_csv('/home/priyanka.s/work/algorithms/AAPL.csv', index_col='Date', parse_dates=True, dayfirst=True)
You specify dayfirst=True because of the DD/MM/YYYY format.

Your algo probably won't be able to run because it requires 50 days of data before its first day, as you're using mavg(50). Using .run() on Panel data automatically starts the backtest on the first day for which data is available, and there won't be enough pricing history at that point. To get around this you can use run_algorithm and specify a start date that is at least 50 trading days after the start of your data. (run_algorithm may not work with Panel data if your version of Zipline isn't up-to-date. If it doesn't work, try installing it from the GitHub repo.)

To get your current simulated cash and portfolio value you can do context.portfolio.cash and context.portfolio.portfolio_value, as documented here. You can take a look at the profit your algo made after a backtest by looking at the returned perf DataFrame.

Best,
Nathan

priya...@quantinsti.com

unread,
Aug 24, 2016, 2:27:47 AM8/24/16
to Zipline Python Opensource Backtester
i have figured it out, here is link of the blog that i wrote on this.

Peter Cherry

unread,
Sep 5, 2016, 11:36:24 AM9/5/16
to Zipline Python Opensource Backtester
Hello,
this is a very good tutorial how to import my CSV data for 1 Symbol having Date, Open, High, Low, Close, Volume, Price columns.

But what needs to be changed if I have CSV file with many symbols. It means 1 CSV file with the following columns: Symbol, Date, Open, High, Low, Close, Volume, Price?

Thanks for help
Peter

Peter Cherry

unread,
Sep 12, 2016, 9:37:34 AM9/12/16
to Zipline Python Opensource Backtester
Hello,
I found this advice, but I don't know how to write it in the code:

DataPanel with dates on the major axis, sids as items, and OHLCV as the minor axis.  You might take a look at test_yahoo_bars_to_panel_source (https://github.com/quantopian/zipline/blob/dc964a7e7da7e975b22fabfe962d2210704c1d6b/tests/test_sources.py#L67-L67) for an example.
- show quoted text -
Reply all
Reply to author
Forward
0 new messages