--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Enter code here...
"""
This code is used to demonstrate how to run a Zipline backtest from within code. (not using the command line tool)
The reason for doing is using Python to drive the running of multiple backtests.
A good reference is the zipline code that actually runs the backtest:
_run() in zipline/utils/run_algo.py
"""
from zipline import TradingAlgorithm
from zipline.data.data_portal import DataPortal
from zipline.finance.trading import TradingEnvironment
from zipline.utils.factory import create_simulation_parameters
from zipline.utils.calendars import get_calendar
from zipline.pipeline.loaders import USEquityPricingLoader
from zipline.pipeline.data.equity_pricing import USEquityPricing
from zipline.data.bundles.core import load
from zipline.api import symbol, order # used in handle_data
import os
import re
from time import time
import pandas as pd
CAPITAL_BASE = 1.0e6
def makeTS(date_str):
"""creates a Pandas DT object from a string"""
return pd.Timestamp(date_str, tz='utc')
def parse_sqlite_connstr(db_URL):
"""parses out the db connection string (needed to make a TradingEnvironment"""
_, connstr = re.split(r'sqlite:///', str(db_URL), maxsplit=1,)
return connstr
def make_choose_loader(pl_loader):
def cl(column):
if column in USEquityPricing.columns:
return pipeline_loader
raise ValueError("No PipelineLoader registered for column %s." % column)
return cl
if __name__ == '__main__':
# load the bundle
bundle_data = load('quantopian-quandl', os.environ, None)
cal = bundle_data.equity_daily_bar_reader.trading_calendar.all_sessions
pipeline_loader = USEquityPricingLoader(bundle_data.equity_daily_bar_reader, bundle_data.adjustment_reader)
choose_loader = make_choose_loader(pipeline_loader)
env = TradingEnvironment(asset_db_path=parse_sqlite_connstr(bundle_data.asset_finder.engine.url))
data = DataPortal(
env.asset_finder, get_calendar("NYSE"),
first_trading_day=bundle_data.equity_minute_bar_reader.first_trading_day,
equity_minute_reader=bundle_data.equity_minute_bar_reader,
equity_daily_reader=bundle_data.equity_daily_bar_reader,
adjustment_reader=bundle_data.adjustment_reader,
)
start = makeTS("2015-11-01"); end = makeTS("2016-11-01") # this can go anywhere before the TradingAlgorithm
def initialize(context):
pass
def handle_data(context, data):
order(symbol('AAPL'), 10)
# the actual running of the backtest happens in the TradingAlgorithm object
bt_start = time()
perf = TradingAlgorithm(
env=env,
get_pipeline_loader=choose_loader,
sim_params=create_simulation_parameters(
start=start,
end=end,
capital_base=CAPITAL_BASE,
data_frequency='daily',
),
**{
'initialize': initialize,
'handle_data': handle_data,
'before_trading_start': None,
'analyze': None,
}
).run(data, overwrite_sim_params=False,)
bt_end = time()
print perf.columns
print perf['portfolio_value']
print "The backtest took %0.2f seconds to run." % (bt_end - bt_start)
print("all done boss")
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
Hi Ed,
I saw and was already taking a look around.
Very nice of you, thanks so much.
Cheers, Joe
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi Vitaly,Welcome to the project! I've just sent you an invitation. Please, create your own branch and contribute code there.Regards,Ed
2017-05-03 0:41 GMT+03:00 Vitaly Ermilov <vitaly....@gmail.com>:
Hi, EdI'm very interested in the topic, and I am able to invest quite a lot of my time and provide my skills to contribute to the project. I am good at deep understanding of code. Phd in Computer science.I'd be happy to join the project.Best RegardsVitaly Ermilov
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
I have the wiki pages up:I agree on all those things points. I don't see that you missed anything.On Fri, May 12, 2017 at 8:33 AM, Tibor Kiss <tibor...@gmail.com> wrote:Hello Peter,+1 on branching / PRs & Wiki, great idea!I read through on your design (analysis.txt) and it is pretty close what I have in mind.It is a good starting point for the design doc in the wiki. Could you please create an entry based on it?Before we can merge our work to the new branch we need to agree on some small differences:- I like your abstraction of BrokerClient / IBBrokerClient. I will split up my TWSConnection class accordingly- I think my RealtimeClock removes the need of TradingAlgorithm.on_dt_changed() caller?- TradingAlgorithmLive: Currently I have directly modified the original class. It is better to create a separate live-specific class (which builds on top of TradingAlgorithm). I'll move my changes into TradingAlgorithmLive.- Need of BlotterLive: I'm not completely sure about this. It might be enough to override TradingAlgorithm's order(), cancel(), etc.Did I miss anything?Thanks,Tibor
--
You received this message because you are subscribed to a topic in the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/zipline/KHKI1PZx08I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to zipline+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
A quick update from the last two weeks:
Zipline-Live is now able to connect to Interactive Broker's TWS, load account and portfolio informationand route normal orders (market, limit, stop, sl) to the broker.In other words: We're half way there, a simple buy-and-hold strategy could be executed on premise.Currently the following tasks are under development:- Store the algo state locally (Peter)- Get the windows build clean (Ed)- IB realtime data support (Tibor)All this work is accessible on our 'live' branch: https://github.com/zipline-live/zipline/tree/liveWe need your help:If you are interested please give it a try, test it & tinker with it. Any feedback is greatly appreciated!Join us on Slack if you have any questions / comments / requests:
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to zipline+unsubscribe@googlegroups.com.
Thanks!
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/zipline/KHKI1PZx08I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to zipline+unsubscribe@googlegroups.com.
Hi,Does anybody use zipline for live trading? Any ideas where to look in the code to implement this. Any points to the zipline code that supports this?It should be possible as Quantopian uses zipline for live trading. However, I was not able to find and points in the documentation regarding this except of the description: "Zipline is a Pythonic algorithmic trading library. It is an event-driven system that supports both backtesting and live-trading."
--BR,
Ed
--
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
--
You received this message because you are subscribed to a topic in the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/zipline/KHKI1PZx08I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to zipline+unsubscribe@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to zipline+u...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to zipline+unsubscribe@googlegroups.com.
print(get_datetime('US/Eastern').time())
Hi Ed,I can and would like to help out with this. I have live traded with IbPy in the past.The documentation for DataPortal is pretty good, Blotter could use a little more documentation.Scott gave some good advice on using those two classes, and then next question is after those get subclassed and the proper methods overridden, where would you plug them in? If you dig into the code Zipline uses to run an algorithm you will find that the heart of it is a TradingAlgorithm, most people will never see this if they are letting Zipline "run" the algorithm for them. The Zipline run function basically just sets up a TradingAlgorithm and runs it. (I've included complete code at the bottom of this post showing how to run a backtest on your own, it shows how TradingAlgorithm is set up.)TradingAlgorithm is where I would plug in the custom DataPortal, let's call it LiveDataPortal (see line #274 of algorithm.py), you can pass it as an argument. Now where to plug in the custom Blotter, (let's call it LiveBlotter)? Guess what? You also can pass it as an argument to TradingAlgorithm (see line #321 of algorithm.py).So now that those questions are answered the real work of getting LiveDataPortal and LiveBlotter to talk to IB would need to happen.Like I said I could and would like to help out with this, so think of a project name and we can get started.
Enter code here...
"""
This code is used to demonstrate how to run a Zipline backtest from within code. (not using the command line tool)
The reason for doing is using Python to drive the running of multiple backtests.
A good reference is the zipline code that actually runs the backtest:
_run() in zipline/utils/run_algo.py
"""
from zipline import TradingAlgorithm
from zipline.data.data_portal import DataPortal
from zipline.finance.trading import TradingEnvironment
from zipline.utils.factory import create_simulation_parameters
from zipline.utils.calendars import get_calendar
from zipline.pipeline.loaders import USEquityPricingLoader
from zipline.pipeline.data.equity_pricing import USEquityPricing
from zipline.data.bundles.core import load
from zipline.api import symbol, order # used in handle_data
import os
import re
from time import time
import pandas as pd
CAPITAL_BASE = 1.0e6
def makeTS(date_str):
"""creates a Pandas DT object from a string"""
return pd.Timestamp(date_str, tz='utc')
def parse_sqlite_connstr(db_URL):
"""parses out the db connection string (needed to make a TradingEnvironment"""
_, connstr = re.split(r'sqlite:///', str(db_URL), maxsplit=1,)
return connstr
def make_choose_loader(pl_loader):
def cl(column):
if column in USEquityPricing.columns:
return pipeline_loader
raise ValueError("No PipelineLoader registered for column %s." % column)
return cl
if __name__ == '__main__':
# load the bundle
bundle_data = load('quantopian-quandl', os.environ, None)
cal = bundle_data.equity_daily_bar_reader.trading_calendar.all_sessions
pipeline_loader = USEquityPricingLoader(bundle_data.equity_daily_bar_reader, bundle_data.adjustment_reader)
choose_loader = make_choose_loader(pipeline_loader)
env = TradingEnvironment(asset_db_path=parse_sqlite_connstr(bundle_data.asset_finder.engine.url))
data = DataPortal(
env.asset_finder, get_calendar("NYSE"),
first_trading_day=bundle_data.equity_minute_bar_reader.first_trading_day,
equity_minute_reader=bundle_data.equity_minute_bar_reader,
equity_daily_reader=bundle_data.equity_daily_bar_reader,
adjustment_reader=bundle_data.adjustment_reader,
)
start = makeTS("2015-11-01"); end = makeTS("2016-11-01") # this can go anywhere before the TradingAlgorithm
def initialize(context):
pass
def handle_data(context, data):
order(symbol('AAPL'), 10)
# the actual running of the backtest happens in the TradingAlgorithm object
bt_start = time()
perf = TradingAlgorithm(
env=env,
get_pipeline_loader=choose_loader,
sim_params=create_simulation_parameters(
start=start,
end=end,
capital_base=CAPITAL_BASE,
data_frequency='daily',
),
**{
'initialize': initialize,
'handle_data': handle_data,
'before_trading_start': None,
'analyze': None,
}
).run(data, overwrite_sim_params=False,)
bt_end = time()
print perf.columns
print perf['portfolio_value']
print "The backtest took %0.2f seconds to run." % (bt_end - bt_start)
print("all done boss")
On Thursday, February 16, 2017 at 1:31:30 PM UTC-8, Ed Bartosh wrote:Hi Scott,Thank you for the info! It helped a lot!There is one piece of similar work I've found on github: https://github.com/CarterBain/AlephNullAs far as I can see they modified blotter module. Can you tell if this can be used as a starting point or or it's better to start from scratch?I understand that the code there is outdated and zipline code changed a lot since then, but still it would be nice if it can be used.Is there any documentation about Blotter and DataPortal class? Can you provide more details on how you'd implement this?I've seen a lot of requests on this list to implement brokerage interface. If people are still interested it would be a nice community project to implement this. Anyone is interested to collaborate on this?Regards,Ed2017-02-14 16:18 GMT+00:00 Scott Sanderson <ssand...@quantopian.com>:Hi Ed,It is **possible** to use Zipline for live trading (and we do indeed use Zipline as the foundation for live trading on Quantopian), but the line you mentioned from the README is probably no longer an accurate reflection of how easy it is to use Zipline for live trading: running a Zipline live algorithm currently requires a fair amount of custom work. I wouldn't recommend taking on that task unless you're willing to dig into the Zipline source and write some nontrivial extensions.The main two classes that are relevant to live trading are the Blotter class, which manages placing and filling orders, and the DataPortal class, which provides access to minutely and daily pricing data to various subsystems of Zipline. Running a Zipline algorithm live essentially requires creating custom subclasses of Blotter and DataPortal. The Blotter subclass would be responsible for communicating with an brokerage API like the one provided by Interactive Brokers, and the DataPortal subclass would be responsible for providing access to up-to-the-minute pricing data (potentially with fallback to static historical data for history() calls).
In the long term, we'd for it to be easier for people to make these kinds of nontrivial extensions to Zipline (and as an engineer and user of open source software this is a frustrating reply for me to write), but it's hard work to prioritize against all the other things we work on to keep the business running.
Best,-Scott
On Sunday, February 12, 2017 at 6:12:41 AM UTC-5, Ed Bartosh wrote:Hi,Does anybody use zipline for live trading? Any ideas where to look in the code to implement this. Any points to the zipline code that supports this?It should be possible as Quantopian uses zipline for live trading. However, I was not able to find and points in the documentation regarding this except of the description: "Zipline is a Pythonic algorithmic trading library. It is an event-driven system that supports both backtesting and live-trading."
--BR,
Ed
--
You received this message because you are subscribed to the Google Groups "Zipline Python Opensource Backtester" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zipline+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--BR,
Ed
To unsubscribe from this group and all its topics, send an email to zipline+u...@googlegroups.com.